JDBC (Java database connectivity) is a Java database connection API. 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" and select the project as shown in:
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.
To connect to the data source, that is, to connect to the database, first establish a JDBC-ODBC bridge, the above has been said, 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 ");
To prevent connection exceptions, an exception is also required here. For details, refer to the Code:
========= Access. Java ==================
Program code
Import java. SQL .*;
Public class access {
Public static void main (string ARGs []) {
Connection con;
Statement SQL; // declare a statement object
Resultset RS;
Try {
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
}
Catch (classnotfoundexception e ){
System. Out. println ("" + E );
}
Try {
Con = drivermanager. getconnection ("JDBC: ODBC: redsun ","","");
SQL = con. createstatement ();
Rs= SQL .exe cutequery ("select * from member ");
While (Rs. Next ()){
String name = Rs. getstring (1); // obtain the first column of the database
String sex = Rs. getstring (2 );
System. Out. println ("name:" + name); // output information
System. Out. println ("Gender:" + sex );
}
Con. Close ();
}
Catch (sqlexception El ){}
}
}
Compile access. Java, switch to the directory of access. class after editing in the command prompt, and execute:
Java access press ENTER
Result:
The above example is just a simple connection to the database and displays data in a static cursor mode. It is easy to understand the SQL statements learned before updating, deleting, and adding a database. The syntax is:
String xx = "SQL statement ";
Statementobject .exe cuteupdate (XX );