Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<h1>Groovy SQL</h1> <p>이 섹션에서 설명할 내용 중 일부는 Andrew Glover가 쓴 <a href="http://www-106.ibm.com/developerworks/java/library/j-pg01115.html?ca=dgr-lnxw07">GroovySQL 글</a> 을 참고하였습니다. JDBC에 대한 몇몇 참조들이 이해되지 않더라도 너무 걱정하지 마시기 바랍니다. 아래에 예제에서는 Groovy의 새로운 기능을 사용하고 있는데, 이 기능을 이용하면 문자열 정의에 기존 변수의 값을 포함시키는 것이 가능합니다. 다음 예제를 시도해보세요:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> piEstimate = 3; println("Pi is about ${piEstimate}"); println("Pi is closer to ${22/7}"); </pre></td></tr></table> <p>위에서 보인 바와 같이 Groovy에서는 문자열 상수의 ${} 부분 내부에 쓰인 문자열을 Groovy 표현식으로 해석합니다.</p> <p>이 기능은 아래에서 적극적으로 쓰일 것입니다.</p> <h2>간단한 쿼리 실행하기</h2> <p>첫번째 Groovy SQL 코드는 세 줄로 구성됩니다:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> import groovy.sql.Sql sql = Sql.newInstance("jdbc:jtds:sqlserver://serverName/dbName-CLASS;domain=domainName", "username", "password", "net.sourceforge.jtds.jdbc.Driver") sql.eachRow("select * from tableName", { println it.id + " -- ${it.firstName} --"} ); </pre></td></tr></table> <p>첫번째 줄은 자바 임포트 기능입니다. Groovy에게 Sql 클래스의 전체 이름을 알려주는 기능을 하지요. 두번째 줄에서는 SQL 데이터베이스에 대한 새로운 연결(connection)을 생성하고 이를 sql 이라는 변수에 할당하고 있습니다.</p> <p>이 코드는 jTDS를 이용하여 MS-SQL 데이터베이스에 연결하고 있습니다. 여러분은 Sql.newInstance의 인자 부분, 특히 <strong>username</strong> 그리고 <strong>password</strong> 를 수정해야 합니다.</p> <p>마지막으로, 세번째 줄에서는 sql의 eachRow 메서드를 실행하면서 두 개의 인자를 넘기고 있습니다. 첫번째 인자는 쿼리 문자열이고, 두번째 인자는 결과를 출력하기 위한 클로저 입니다.</p> <p>클로저에서 "it"의 필드에 접근하기 위해 두 가지 방법을 사용하고 있다는 점에 주목하세요. 첫번째 방법은 일반적인 참조이고, 두번째 방법은 위에서 설명한 것 처럼 문자열 안에 Groovy 표현식을 포함시키는 방식을 취합니다.</p> <p>결과는 다음과 비슷할 것입니다:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> 001 -- Lane -- </pre></td></tr></table> <h2>더 복잡한 쿼리 실행하기</h2> <p>위 예제는 꽤 간단한 편입니다. 하지만 GroovySql은 입력(insert), 갱신(update), 삭제(delete) 등이 포함된 복잡한 데이터 조작도 충분히 다룰 수 있습니다. 입력, 갱신, 삭제에 대해서는 클로저를 만들 필요가 없고, Groovy의 Sql 클래스가 제공하는 execute와 executeUpdate 메서드를 사용하면 됩니다. 이 메서드들은 JDBC Statement 클래스의 execute와 executeUpdate 메서드와 같습니다.</p> <p>다음 예제는 데이터베이스에 자료를 입력하는 간단한 예제입니다. ${} 문법을 또 사용하고 있습니다:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> firstName = "yue" lastName = "wu" sql.execute("insert into people (firstName, lastName) "+ " values ('${firstName}', ${lastName})") </pre></td></tr></table> <p>물론 이 방법은 추천할만한 방법이 아닙니다. 변수 안에 따옴표 같은 문자가 들어있다면 어떻게 될지 상상해보세요. 더 좋은 방법은 PreparedStatement를 사용하는 것입니다:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> firstName = "yue" lastName = "wu" sql.execute("insert into people (firstName, lastName) "+ " values (?,?)", [firstName, lastName]) </pre></td></tr></table> <p>입력하고자 하는 데이터가 "?" 자리에 치환되어 들어가고, 값들은 데이터 항목의 배열로써 전달됩니다. 갱신(update)도 이와 비슷합니다. 이번에도 역시 executeUpdate 메서드가 데이터 항목의 배열을 인자로 받고 있습니다. 물론 이 인자들은 SQL 문의 "?" 부분에 채워집니다.</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> comment = "Lazy bum" sql.executeUpdate("update people set comment = ? where id=002", [comment]) </pre></td></tr></table> <p>삭제(delete) 또한 마찬가지입니다:</p> <table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> sql.execute("delete from word where word_id = ?" , [5]) </pre></td></tr></table>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced