CS343 Embedded SQL Tutorial - cs. · PDF file4 loadDriver getConnection public static void...

15
CS343 Embedded SQL Tutorial

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Demo..