Download JDBC

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft Jet Database Engine wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

SQL wikipedia , lookup

PL/SQL wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Transcript
JDBC
CS-328
JDBC
• Java API for accessing RDBMS
• Allows use of SQL for RDBMS
programming
• Can be used for:
– embedded SQL
– execution of stored queries
SQL Statement Execution
• Four primary classes used to
– load a driver
• (java.sql.DriverManager)
– connect to the DB
• (java.sql.Connection)
– create a SQL statement
• (java.sql.Statement)
– execute a SQL statement
• (java.sql.ResultSet)
Example
try {
Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);
String url = “jdbc:odbc:msaccessdb”;
Connection con = DriverManager.getConnection(url,””,””);
// create sql statement
String qs = “select * from loadtest”;
Statement stmt = con.createStatement();
// execute the statement
ResultSet rs = stmt.executeQuery(qs);
// process the result set
boolean more = rs.next;
while (more) {
System.out.println ............}}
Driver Manager Object
• Loads the proper driver into your java
object
– multiple drivers may be loaded to allow
connections to multiple databases
• Provides a common interface to RDBMSs
for JDBC Drivers
Driver Manager Object
JAVA Application
Driver Manager
Driver ‘A’
Driver ‘B’
Connection
Connection
Statement
Statement
Result Set
Result Set
Driver Manager Class Methods
•
•
•
•
•
•
•
•
•
getConnection(String url)
getDriver
registerDriver
deregisterDriver
getDrivers
setLoginTimeout
getLoginTimeout
setLogStream
getLogStream
Connection Object
• Establish link between the JAVA application
and RDBMS
• allows application to select proper driver
when it needs to
• uses the database URL to make the
connection
– jdbc:<subprotocol>:<subname)
• jdbc:odbc:Mydatabase
• jdbc:db2:157.287.43.11/Mydatabse
Connection Class Methods
•
•
•
•
•
•
•
•
•
•
createStatement
prepareStatement
prepareCall
NativeSQL
setAutoCommit
GetAutoCommit
commit
rollback
close
isclosed
Statement Object
• Wrapper of a string containing a SQL
statement
• allows JDBC to decompose the SQL into a
set of steps to present to the database via the
driver
• the connection object forwards the
statement object to the database to obtain a
results set object
Statement Class Methods
• executeQuery
• executeUpdate
– insert, update, delete
• close
• getResultSet
ResultSet Object
• A container for the rows and columns (a
table) acquired as a result of presenting a
statement object to the RDBMs using the
“executeQuery” statement method
ResultSet Class Methods
•
•
•
•
•
•
•
next
close
wasNull
getString
getBoolean
getByte
getShort
ResultSet Class Methods(cont.)
•
•
•
•
•
•
•
getInt
getLong
getFloat
getDouble
getNumeric
getBytes
getDate
ResultSet Class Methods (cont.)
•
•
•
•
•
•
•
getTime
getTimeStamp
getAsciiStream
getUnicodeStream
getBinaryStream
getMetaData
etc...
JDBC Type Mapping
•
•
•
•
Char - String
Varchar - String
Longvarchar - String
Numeric java.sql.Numeric
• Decimal java.sql.Numeric
• Bit - boolean
•
•
•
•
•
•
•
•
Tinyint - byte
Smallint = short
Integer - int
Bigint - long
Real - float
Float - double
Double - double
Binary - byte[ ]
JDBC Type Mapping (cont.)
•
•
•
•
•
Varbinary - byte[ ]
Longvarbinary - byte[ ]
Date - java.sql.Date
Time - java.sql.Time
Timestamp java.sql.Timestamp