CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void...
Transcript of CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void...
![Page 1: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/1.jpg)
CS343 Embedded SQL Tutorial
![Page 2: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/2.jpg)
JDBC Architecture
![Page 3: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/3.jpg)
3
loadDriver
getConnection
createStatement
execute(SQL)
Result handling
More results ?
close Statment
close Connection
no
yes
close ResultSet
Steps:
1- Load the driver and register it with the driver manager
(provided you’ve already downloaded the driver “jar”
file)
2- Connect to a database
3- Create a statement
4- Execute a query and retrieve the results, or make
changes to the database
5- Disconnect from the database
JDBC - Simple Example
![Page 4: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/4.jpg)
4
loadDriver
getConnection
createStatement
execute(SQL)
Result handling
More results ?
close Statment
close Connection
no
yes
close ResultSet
import java.sql.*;
public class jdbctest {
public static void main(String args[]){
try{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost:5432/csc343h-user", "user", "pass");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery ("select name, number
from mytable where number < 2");
while( rs.next() )
System.out.println(rs.getString(1) + " (" + rs.getInt(2) + ")");
rs.close();
stmt.close()
con.close();
} catch(Exception e){
System.err.println(e); }
} }
JDBC - Simple Example
API for accessing and processing DB data
Java launcher looks for “main” method
“catch” an Exception here (could be an SQLException)
![Page 5: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/5.jpg)
5
What do I need to run this code?
Download the driver
http://jdbc.postgresql.org/download.html
Compile the code javac JDBCExample.java
Run your code (with CLASSPATH option) java -cp /path-to-jdbc-dir/postgresql-9.1-903.jdbc4.jar:. JDBCExample
import java.sql.*;
public class jdbctest {
public static void main(String args[]){
try{
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection
("jdbc:postgresql://localhost:5432/csc343h-user", "user", "pass");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery
("select name, number from pcmtable where number < 2");
while( rs.next() )
System.out.println(rs.getString(1) + " (" + rs.getInt(2) + ")");
rs.close();
stmt.close()
con.close();
} catch(Exception e){
System.err.println(e); }
} }
JDBCExample.java
![Page 6: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/6.jpg)
6
JDBCExample.java: General Instructions
![Page 7: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/7.jpg)
7
JDBCExample.java: Import SQL Package, Prepare DB Objects
![Page 8: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/8.jpg)
8
JDBCExample.java: Load the driver!
Notice that in A2 you need to have this line in your constructor
method: Assignment2()
![Page 9: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/9.jpg)
9
JDBCExample.java: Make the connection to the DB..
![Page 10: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/10.jpg)
10
JDBCExample.java: Create + Execute a Statement!
![Page 11: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/11.jpg)
11
JDBCExample.java: INSERTs, UPDATEs..
![Page 12: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/12.jpg)
12
JDBCExample.java: Use Prepared Statement for Insertion!
![Page 13: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/13.jpg)
13
JDBCExample.java: Get and Process a Result Set..
![Page 14: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/14.jpg)
14
JDBCExample.java: DROP a table and close connection..
![Page 15: CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void main(String createStatement execute(SQL) Result handling More results ? close Statment close](https://reader034.fdocuments.us/reader034/viewer/2022051104/5a7823a67f8b9aa3688e7ae3/html5/thumbnails/15.jpg)
Demo..