Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CS343 Embedded SQL
Tutorial
JDBC Architecture
JDBC - Simple Example
loadDriver
getConnection
createStatement
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
execute(SQL)
Result handling
4- Execute a query and retrieve the results, or make
changes to the database
5- Disconnect from the database
yes
More
results ?
no
close ResultSet
close Statment
close Connection
3
JDBC - Simple Example
loadDriver
getConnection
createStatement
execute(SQL)
Result handling
yes
More
results ?
no
close ResultSet
close Statment
close Connection
import java.sql.*;
API for accessing and processing DB data
public class jdbctest {
Java launcher looks
public static void main(String args[]){
for “main” method
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); }
} }
“catch” an Exception here
(could be an SQLException)
4
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.4.1212.jre6.jar:. JDBCExample
JDBCExample.java
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); }
} }
5
JDBCExample.java: General Instructions
// Instructions:
// 1) Connect to mathlab "ssh <UTorID>@mathlab.utsc.utoronto.ca" using your mathlab account
// 2) Download the JDBC driver from http://jdbc.postgresql.org/download.html and
// copy jdbc jar file (using sftp) to the working folder.
// 3) On line 60 connection =
DriverManager.getConnection("jdbc:postgresql://localhost:5432/username", "username", "");
// -leave as is the host and port number ("localhost:5432");
// -replace "username" with your mathlab username
// - you may need to set the password field. The default one is set to empty ("");
// 4) Compile the code:
//
javac JDBCExample.java
// 5) Run the code:
//
java -cp /*****path-to-jdbc-directory*****/postgresql-9.4.1212.jre6.jar:. JDBCExample
// where postgresql-9.1-903.jdbc4.jar is jdbc jar file downloaded in step 2
6
JDBCExample.java: Import SQL Package, Prepare DB Objects
7
JDBCExample.java: Load the driver!
Notice that in A2 you need to have
this line in your constructor
method: Assignment2()
8
JDBCExample.java: Make the connection to the DB..
9
JDBCExample.java: Create + Execute a Statement!
10
JDBCExample.java: INSERTs, UPDATEs..
11
JDBCExample.java: Use Prepared Statement for Insertion!
12
JDBCExample.java: Get and Process a Result Set..
13
JDBCExample.java: DROP a table and close connection..
14
Demo..