Java prerequisites
1. First, there are many (Oracle Installation Directory) under D: \ oracle \ ora92 \ JDBC \ Lib. Jar package file. You must copy the ojdbc14.jar file to your project, right-click the file, and choose "build path"> "import, now you can open and use the class files under the package. (Note: You can also place the driver in the classpath environment variable, and set other class packages similarly)
2. Continue loading and registering the driver
Class. forname ("oracle. JDBC. Driver. oracledriver ");
After the driver program is loaded, a driver object is generally created and automatically registered by calling drivermanager. registerdriver.
3. Establish a connection
Drivermanager. getconnection ("JDBC: oracle: thin: @ localhost: 1521: LC", name, pass );
The standard syntax of jdbc url is as follows:
::
At this point, we can find that there are three parts:
◆ Protocol: main communication protocols
◆ Subprotocol: the name of the driver of the secondary communication protocol.
◆ Data source identifier: Data Source
As shown in the preceding example:
"JDBC: oracle: thin" is the communication protocol, @ "is the valid host address, followed by the port number. The default value is 1521, followed by your data source ,, the user name and password can also be used in the following form:
Connection con = drivermanager. getconnection ("JDBC: oracle: thin: Name/
Pass @ localhost: 1521: LC ");
Returns the con object of a connection.
4. Create an SQL statement object
In this example, a premade statement is created.
Preparedstatement pstmt = con. preparestatement (SQL statement );
You can also use:
Statement stmt = con. createstatement ();
For details about the differences, see the help documentation.
5. Execute this statement
Executeupdate (), executequery (), execute (),
For usage instructions, see the help documentation.
6. Complete cleaning
Close the connection. Complete example:
This class completes loading and connection,
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. sqlexception;
Public class dbconnection {
Public static connection dbconn (string name, string pass ){
Connection c = NULL;
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
} Catch (classnotfoundexception e ){
E. printstacktrace ();
}
Try {
C = drivermanager. getconnection ("jdbcracle: thin: @ localhost: 1521: sumoonbest ",
Name, pass );
} Catch (sqlexception E1 ){
E1.printstacktrace ();
}
Return C;
}
}
Finally, this class completes the sending statement, execution, and cleaning.
Import java. SQL .*;
Public class dB extends dbconnection {
Private Static connection con = NULL;