To connect to the data source, that is, to connect to the database tutorial, first you need to establish a JDBC-ODBC Bridge, the last will be described, the method is:
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
Class is a Class in the Java. lang package. You can create a bridge by calling its static method forName. However, exceptions may occur when a bridge is created. This is what we don't want to see, so we need to catch this exception:
Try {
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
}
Catch (ClassNotFoundException e) {exception capture}
The Connection class in the java. SQL package is used to declare an object, and the class DriverManager is used to call its static method getConnection to create the Connection.
Connection con = DriverManager. getConnection ("jdbc: odbc: Data Source Name", "data source username", "data source password ");
Try {
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ");
}
Catch (ClassNotFoundException e ){
System. out. println ("" + e );
}
// The following sentence focuses on
String path = this. getClass (). getClassLoader (). getResource ("db1.mdb"). getPath (). substring (1 );
// Db1.mdb is the created Access database file
// This database file path is in the same path as the java file
String url = "jdbc: odbc: Driver = {Microsoft Access Driver (*. mdb)}; DBQ =" + path;
Connection conn = DriverManager. getConnection (url );
Statement st = conn. createStatement ();
Instance
Package com. manager. common;
Import java. SQL. Connection;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;
Import com. mchange. v2.c3p0. ComboPooledDataSource;
/**
* Jdbc connection pool of c3p0
*
* @ Author Sensen
*
*/
Public class DBConnectionPool {
Protected DBConnectionPool (){
}
Private static ComboPooledDataSource ds = null;
Static {
Try {
Ds = new ComboPooledDataSource ();
// Set the jdbc Driver class
Ds. setDriverClass ("sun. jdbc. odbc. JdbcOdbcDriver ");
// Set the jdbc url
Ds
. SetJdbcUrl ("jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ = H:/gyt_web/Database/SiteWeaver. mdb ");
//// Set the login user name for the database
// Ds. setUser ("admin ");
//// Set the login user name for the database
// Ds. setPassword ("admin ");
// Set the maximum number of connections in the connection pool
Ds. setMaxPoolSize (200 );
// Sets the minimum number of connections in the connection pool.
Ds. setMinPoolSize (20 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
Protected static synchronized Connection getConnection (){
Connection con = null;
Try {
Con = ds. getConnection ();
} Catch (SQLException e ){
E. printStackTrace ();
}
Return con;
}
/**
* Common query methods
*
* @ Param SQL
* @ Param params
* @ Return
*/
Public ResultSet query (String SQL, Object [] params ){
Connection conn = null;
PreparedStatement pstutorial tmt = null;
ResultSet rs = null;
Conn = this. getConnection ();
Try {
Pstmt = conn. prepareStatement (SQL );
If (params! = Null ){
For (int I = 0; I <params. length; I ++ ){
Pstmt. setObject (I + 1, params [I]);
}
}
Rs = pstmt.exe cuteQuery ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return rs;
}
/**
* Added a general deletion method.
*
* @ Param SQL
* @ Param params
* @ Return
*/
Public int executeSQL (String SQL, Object [] params ){
Connection conn = null;
PreparedStatement pstmt = null;
Int result = 0;
Conn = this. getConnection ();
Try {
Pstmt = conn. prepareStatement (SQL );
If (params! = Null ){
For (int I = 0; I <params. length; I ++ ){
Pstmt. setObject (I + 1, params [I]);
}
}
Result = pstmt.exe cuteUpdate ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return result;
}
}
Instance
"Import java. SQL .*;
Public class ConnectAccess {
/**
* Notes for beginners:
* 1: Create an access file a1.mdb and put it under D;
* 2: create a table Table1 in the database file a1.mdb;
* 3: add a column for Table1 and insert at least one record;
* 4: This article is a complete class, which can be run directly.
*/
Public static void main (String args []) throws Exception {
ConnectAccess ca = new ConnectAccess ();
Ca. ConnectAccessFile ();
Ca. ConnectAccessDataSource ();
}
Public void ConnectAccessFile () throws Exception
{
Class. forName ("" sun. jdbc. odbc. JdbcOdbcDriver "");
/**
* Directly connect to the access file.
*/
String dbur1 = "" jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ = d: a1.mdb "";
Connection conn = DriverManager. getConnection (dbur1, "" username "", "" password "");
Statement stmt = conn. createStatement ();
ResultSet rs = stmt.exe cuteQuery ("" select * from Table1 "");
While (rs. next ()){
System. out. println (rs. getString (1 ));
}
Rs. close ();
Stmt. close ();
Conn. close ();
"
Note:
JDBC can establish a connection with a database, send SQL statements to the database, and then process the results returned by the database.
JDBC is similar to ODBC in design. One way to establish a connection between JDBC and a database is to first build a JDBC-ODBC Bridge.
First install office2000, and Access is enabled to create a new database, mess. mdb: creates a table that contains two text fields: name and sex. The table name IS member. You can enter some strings to the member table and store them in any Directory, such as drive C. Choose Control Panel> Administrative Tools> data source (ODBC) and select "system DSN". You can also select the "user DSN" tab, the system DSN applies to each user to avoid permission issues during debugging. Click "add ",
Click "finish". In the displayed dialog box, set "data source name" to your favorite name, such as redsun. Click "select" and find the "C: mess. mdb" file, so that an ODBC data source is configured.