(1) Loading and registering the JDBC driver for the database
Load JDBC Driver:
Class.forName ("Oracle.jdbc.driver.OracleDriver");
Register JDBC Driver:
Java.sql.DriverManager.registerDriver (New Oracle.jdbc.driver.OracleDriver ());
(2) Establish a connection with the database
To establish a connection to a database, you first create a URL for the specified database. The URL object that connects the database is established using the DriverManager getconnection method. The database URL object is similar to the Uniform Resource location of network resources, and its form is as follows:
Jdbc:subprotocol:subname://hostname:port;databasename=ⅹⅹⅹ
which
JDBC indicates that database access is currently performed through a Java database connection;
Subprotocol represents the database connectivity mechanism supported by some driver;
SubName represents the specific name under the current connection mechanism;
Hostname represents the host name;
Port indicates the corresponding connection ports;
DatabaseName represents the name of the database to connect to.
Here for an example of a connection to an Oracle database:
Connect Oracle 8/8i/9i Database (in thin mode)
url = jdbc:oracle:thin: @hostip: 1521:oraclesid;
Note: HostIP refers to the IP address of the host, Oraclesid refers to the SID of the database.
Furthermore, determine the username and password of the connection database, that is, the user and password values:
user = "Ⅹⅹⅹ";
Password = "Ⅹⅹⅹ";
Finally, use the following statement:
Connection con=java.sql.drivermanager.getconnection (Url,user,password);
(3) Create statement objects
For example:
Statement stmt = Con.createstatement ();
(4) Calling and executing SQL statements
For example:
String sql = "Select a,b,c from table1";//table1 for the name of the table you want to query a,b,c for the field you want to query
ResultSet rs = stmt.executequery (SQL);
(5) Accessing the recordset in ResultSet and fetching records from it
For example:
Rs.next ();
Rs.absolute (4);
String col1=rs.getstring (1);
........
(6) Turn off ResultSet, statement and connection objects in turn
For example:
Rs.close ();
Stmt.close ();
Con.close ();