Title: COMPE 438 JAVA PROGRAMMING
1COMPE 438JAVA PROGRAMMING
2Java DataBase Connectivity(JDBC)
3JDBC
- The JDBC API is a Java API that can access any
kind of tabular data - JDBC helps you to write java applications that
manage three programming activities - Connect to a data source, like a database
- Send queries and update statements to the
database - Retrieve and process the results received from
the database in answer to your query
4Example
Connection con DriverManager.getConnection
( "jdbcmyDrivermyDB,"myLogin","myPassw
ord") Statement stmt con.createStatement(
) ResultSet rs stmt.executeQuery("SELECT
a, b, c FROM Table1") while (rs.next())
int x rs.getInt("a") String s
rs.getString("b") float f rs.getFloat("c")
5JDBC Drivers
- JDBC consists of two parts
- JDBC API, a purely Java-based API
- JDBC Driver Manager,which communicates with
vendor-specific drivers that perform the real
communication with the database.
6JDBC Data Types
77 Steps to use JDBC
- Load the driver
- Define the Connection URL
- Establish the Connection
- Create a Statement object
- Execute a query and
- Process the results
- Close the connection
87 Steps to use JDBC(with example)
- Load the driver
- Before a connection to a database can be
established, load the JDBC driver
try driverName sun.jdbc.odbc.JdbcOdbcDrive
r Class.forName(driverName) catch
(ClassNotFoundException e) // Could not find
the driver
try // Load the JDBC driver String (MySQL)
driverName "org.gjt.mm.mysql.Driver"
Class.forName(driverName) catch
(ClassNotFoundException e) // Could not find
the driver
97 Steps to use JDBC(with example)
- Define the Connection URL
- jdbcsubprotocolsubname
- protocol name i.e. jdbc
- subprotocol represents the database you want to
connect to e.g. mysql, oracle, odbc etc. - subname provides additional information on how
and where to connect
String DATABASE_URL "jdbcodbcphonebook"
String DATABASE_URL "jdbcmysql//localhost80/p
honebook"
107 Steps to use JDBC(with example)
String username admin" String password
sa" Connection connection DriverManager.getCo
nnection(DATABASE_URL, username, password)
117 Steps to use JDBC(with example)
- Create a Statement object
- Statement object for invocation
- Statement stmt
- connection.createStatement()
- Execute a Query and 6. Process Results
- ResultSet object for result processing
- ResultSet rset stmt.executeQuery("SELECT
address,script,type FROM worklist") - Close the connection
- connection.close()
127 Steps to use JDBC(with example)
- Create a Statement object
- Statement object for invocation
- Statement stmt
- connection.createStatement()
- Execute a Query and 6. Process Results
- ResultSet object for result processing
- ResultSet rset stmt.executeQuery("SELECT
address,script,type FROM worklist") - Close the connection
- connection.close()
13Online Resources
14Online Resources
- Suns JDBC Site
- http//java.sun.com/products/jdbc/
- JDBC Tutorial
- http//java.sun.com/docs/books/tutorial/jdbc/
- List of Available JDBC Drivers
- http//industry.java.sun.com/products/jdbc/drivers
/ - API for java.sql
- http//java.sun.com/j2se/1.4/docs/api/java/sql/pac
kage-summary.html