Sponsored Link •
|
This lecture gives an introductory overview of JDBC.
DriverManager
, JDBC URLs, Connection
,
Statement
java.sql
) allows
you to use SQL to talk to databases from a Java program.
java.sql
calls go through a JDBC driver.
DatabaseMetaData
to adapt to particular underlying database
DriverManager
class:
Driver
classes have a static initializer that
creates an instance and calls DriverManager.registerDriver()
DriverManager
attempts to load any drivers specified in the
java.lang.System
property jdbc.drivers
jdbc.drivers=sun.jdbc.odbc.JdbcOdbcDriver;
DriverManager.getConnection()
, passing in:
DriverManager
tests passes the URL to each registered driver
(in order of registration)
jdbc:<subprotocol>:<subname>
<subprotocol>
- name of a driver or a database connectivity
mechanism
<subname>
- a way to identify the database
jdbc:odbc:mydb jdbc:dcenaming:accounts-receivable jdbc:netdb://vladimir:1066/conquer jdbc:odbc:mydb;UID=wtc;PWD=hastings
DriverManager.getConnection()
returns a reference to
a Connection
object
Connection
object represents one connection to a database
Class | Connection method | About |
---|---|---|
Statement
|
createStatement()
|
Use for simple SQL statements with no input parameters |
PreparedStatement
|
prepareStatement()
|
Pre-compiled; Can take input parameters Use for simple SQL statements executed often, statements with input parameters |
CallableStatement
|
prepareCall()
|
Use to execute stored procedures |
Statement
is the superclass of PreparedStatement
,
which is the superclass of CallableStatement
DriverManager
:Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver");
Connection
from the DriverManager
:Connection conn = DriverManager.getConnection( "jdbc:odbc:mydb", "", "");
Statement
object from the Connection
:Statement stmt = conn.createStatement();
Statement
object, get back a
ResultSet
object:ResultSet rs = stmt.executeQuery( "SELECT LAST, FIRST, TOTAL " + "FROM students.csv mydb " + "ORDER BY TOTAL DESC");
ResultSet
:while (rs.next()) { String s = rs.getString("FIRST") + " " + rs.getString("LAST") + ": " + rs.getString("TOTAL"); System.out.println(s); }
commit()
or rollback()
on
the Connection
commit()
or rollback()
is invoked, the current
transaction ends and a new one begins on that Connection
commit()
automatically called after
each statement
commit()
or
rollback()
to complete the transaction
Connection
s operate at a "transaction isolation level" that
defines how conflicts between concurrent transactions will be resolved
Connection
object starts out at a level that is most likely
the default for the underlying database
setIsolationLevel()
on the Connection
jdbc.subprotocol.name
students.csv
:
LAST,FIRST,HW1,HW2,HW3,HW4,TOTAL Busy,Lizzy,50,50,50,50,200 Java,Jane,100,100,100,100,400 Java,Joe,100,100,100,100,400 Slow,Joe,0,0,0,0,0 Smart,Bart,90,90,90,90,360
// In file jdbc/ex1/StudentSorter.java import java.sql.*; public class StudentSorter { public static void main(String[] args) { try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection( "jdbc:odbc:mydb", "", ""); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery( "SELECT LAST, FIRST, TOTAL " + "FROM students.csv mydb " + "ORDER BY TOTAL DESC"); while (rs.next()) { String s = rs.getString("FIRST") + " " + rs.getString("LAST") + ": " + rs.getString("TOTAL"); System.out.println(s); } stmt.close(); } catch(Exception e) { e.printStackTrace(); } } }Here's the the output of
StudentSorter
:
Joe Java: 400 Jane Java: 400 Bart Smart: 360 Lizzy Busy: 200 Joe Slow: 0
Sponsored Links
|