The following describes the general process of using JDBC:
1. Load the driver class of a database, usually using class. forname ("Driver Class Name");
2. Connect to the database connection con = drivermanager. getconnection (URL, username, password );
3. Get the session-statement stmt = con. createstatement ();
4. execute the operation-result rs = stmt.exe cutequery ("SQL query statement ");
5. processing result ――
While (Rs. Next ()){
String col1 = Rs. getstring (1 );
......
}
The general changes in JDBC are:
1. Three types of statement can be created by the connection object: Statement, preparedstatement, and callablestatement.
2. multiple types of resultset can be created: one-way movement and free movement are supported; updatable and unupdatable; transactions of different levels are supported .....
3. batch processing of data input.
4. Operations on special types of columns (blob, clob, arrary, ref, and struct) in the result set.
5. These special types of input databases.
6. Special result set (cachedrowset, jdbcrowset, and webrowset) operations in the javax. SQL package.
7. The others are a datasource, a managed object in J2EE.
Import java. SQL .*;
Public class JDBC {
String dburl = "JDBC: oracle: thin: @ 127.0.0.1: 1521: orcl ";
String theuser = "admin ";
String thepw = "manager ";
Connection c = NULL;
Statement conn;
Resultset rs = NULL;
Public JDBC (){
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
C = drivermanager. getconnection (dburl, theuser, thepw );
Conn = C. createstatement ();
} Catch (exception e ){
E. printstacktrace ();
}}
Public Boolean executeupdate (string SQL ){
Try {
Conn.exe cuteupdate (SQL );
Return true;
} Catch (sqlexception e ){
E. printstacktrace ();
Return false;
}}
Public resultset executequery (string SQL ){
Rs = NULL;
Try {
Rsw.conn.exe cutequery (SQL );
} Catch (sqlexception e ){
E. printstacktrace ();
}
Return Rs;
}
Public void close (){
Try {
Conn. Close ();
C. Close ();
} Catch (exception e ){
E. printstacktrace ();
}}
Public static void main (string [] ARGs ){
Resultset RS;
JDBC conn = new JDBC ();
Rsw.conn.exe cutequery ("select * from test ");
Try {
While (Rs. Next ()){
System. Out. println (Rs. getstring ("ID "));
System. Out. println (Rs. getstring ("name "));
}
} Catch (exception e ){
E. printstacktrace ();
}}}