|
Java Database Connectivity What is JDBC? Based on the same ideas as ODBC, JDBC is used to interact with the underlying databases for Java programers. JDBC API consists of a user-level API that is used to processe SQL statements and deal with the communication with JDBC Driver Manager, and a JDBC Driver API that database vendors use to interface database drivers to Java. Using JDBC is quite symple, just refer to two things: driver and url. JDBC Driver There are four types JDBC drivers. 1. JDBC-ODBC bridge. 2. Native-API partly Java driver (Java wrapper to a vendor driver). 3. JDBC-Net pure Java driver (network plus type 1 or type 2). 4. Native-protocol pure Java driver (better performance). When accessing small databases, such as Access and FoxPro, type 1 may be the only choice to use. JDBC URL Uniform Resource Locator provides the location of a resource, which JDBC drivers make use to talk to their target databases. The syntax for URLs is: jdbc:<subprotocol>:<subname>.JDBC Web Sites JDBC Tutorial: JDBC-ODBC: //loading driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//making connection
Connection db=DriverManager.getConnection(jdbc:odbc:dsn,uid,pwd);
JDBC: // mySQL Jdbc driver
// add the jar file to the CLASSPATH,e.g. <mysqlPath>/mysql..jar
// for linux, add it to the tomcat.sh file.
Class.forName("org.gjt.mm.mysql.Driver");
Connection dbCon=DriverManager.getConnection(jdbc:mysql:///test);
// Oracle Jdbc OCI driver
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection dbCon=DriverManager.getConnection
("jdbc:oracle:oci7:@mydatabase","scott","tiger");
// Oracle Jdbc Thin driver
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection dbCon = DriverManager.getConnection
("jdbc:oracle:thin:@myhost:1521:orcl","scott","tiger");
SQL statement or stored procedure public ResultSet getResults(String sproc) throws Exception {
// Don't forget to clean up! |