Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
SQL With Groovy
Search
Roberto Guerra
August 10, 2013
Programming
73
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
SQL With Groovy
Roberto Guerra
August 10, 2013
More Decks by Roberto Guerra
See All by Roberto Guerra
Web Services Overview
uris77
0
53
Basic REST Concepts
uris77
2
76
Grails Overview
uris77
1
68
Other Decks in Programming
See All in Programming
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
590
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.2k
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
2
670
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.5k
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
190
APNsからLive Activityを開始する話
yumnumm
0
170
ChromeでローカルLLM
ryu1013
0
140
JIS keyboard support in Omarchy
lef237
0
120
ソニーのクラウド共通基盤の変遷とAI時代の開発スタイルに合わせた進化 / The Journey of Sony’s Common Cloud Platform and Its Evolution for AI-Native Development
kenjiyoneyama
0
270
dotNET_vulnclicker_intro.pdf
deka_morita
0
140
golang-bob.pdf
iwatsukayura
0
130
ActionController meets Protocol Buffers
sorah
0
530
Featured
See All Featured
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
800
A better future with KSS
kneath
240
18k
Facilitating Awesome Meetings
lara
57
7.2k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
540
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
300
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
5
820
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
440
Highjacked: Video Game Concept Design
rkendrick25
PRO
1
480
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Deep Space Network (abreviated)
tonyrice
0
370
Mobile First: as difficult as doing things right
swwweet
225
10k
Designing Experiences People Love
moore
143
24k
Transcript
Connecting to a Database with Groovy & JDBC
JDBC import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import
java.sql.Statement; public class JDBCExample1 { public static void main(String[] args) { Connection con = null; Statement stmt = null; ResultSet rs = null; try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/words", "words", "words"); stmt = con.createStatement(); rs = stmt.executeQuery("select * from word"); while (rs.next()) { System.out.println("word id: " + rs.getLong(1) + " spelling: " + rs.getString(2) + " part of speech: " + rs.getString(3)); } }catch(SQLException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); }finally{ try{rs.close();}catch(Exception e){} try{stmt.close();}catch(Exception e){} try{con.close();}catch(Exception e){} } } }
JDBC try{ Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost: 3306/words", "words", "words"); stmt
= con.createStatement(); rs = stmt.executeQuery("select * from word"); while (rs.next()) { System.out.println("word id: " + rs.getLong(1) + " spelling: " + rs.getString(2) + " part of speech: " + rs.getString(3)); } }
JDBC catch(SQLException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); }finally{ try{rs.close();}catch(Exception e){}
try{stmt.close();}catch(Exception e){} try{con.close();}catch(Exception e){} }
Groovy SQL import groovy.sql.Sql class GroovySqlExample1{ static void main(String[] args)
{ def sql = Sql.newInstance("jdbc:mysql: //localhost:3306/words", "words", "words", "com.mysql.jdbc.Driver") sql.eachRow("select * from word"){ row -> println row.word_id + " " + row.spelling + " " + row.part_of_speech } } }
Groovy SQL - Create a Connection Sql connection = Sql.newInstance(url,
user, password, driver) E.g. Sql.newInstance("jdbc:mysql://localhost: 3306/dbName", "aUser", "secretPassword", "com.mysql.jdbc.Driver")
Groovy Sql - Connection Pool Sql connection = new Sql(dataSource)
Example of a DataSource Type: http://commons.apache.org/proper/commons- dbcp/apidocs/org/apache/commons/dbcp/Basic DataSource.html
Groovy SQL - Query eachRow("Select ...."){row-> //Do something } The
fields will be properties of 'row'.
Groovy SQL - Query Named Parameters sql.eachRow('select * from PROJECT
where name=:foo', [foo:'Gradle']) {row-> // process row } 'select * from PROJECT where name=:foo', [foo:'Gradle']
Groovy SQL - Insert Rows sql.executeInsert("Insert into ....")
Groovy SQL - Updates & Deletes sql.executeUpdate("Update ....") sql.executeDelete("Delete ....")
Reference API Documentation: http://groovy.codehaus.org/gapi/groovy/sql/Sql. html