The following uses JDBC to connect to Oracle 10 Gb as an example. Java can connect to the database through JDBC. Download the JDBC jar package. Details are as follows:
AD:
The following uses JDBC to connect to Oracle 10 Gb as an example.
Java can connect to the database through JDBC. Download the JDBC jar package.
Official Website:
Http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html
You can download the ojdbc14.jar version. (Jdk1.4, jdk1.5, and jdk1.6 are supported)
This version is: (you may need to log on to the Oracle website)
Http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-10201-088211.html
The instance code is as follows:
-
- Import java. SQL. connection;
- Import java. SQL. preparedstatement;
- Import java. SQL. resultset;
- Import java. util. arraylist;
- Import java. util. List;
-
- Import javax. SQL. pooledconnection;
-
- Import oracle. JDBC. Pool. oracleconnectionpooldatasource;
-
- Public class jdbctest {
- Private string url = NULL;
-
- /*
- * Connecting to the Oracle database
- */
- Public jdbctest (string shostname, string sportnumber, string SSID ){
- Url = "JDBC: oracle: thin: @" + shostname + ":" + sportnumber + ":" + SSID;
- // If you also use jdk1.6
- // URL = "JDBC: oracle: thin: @" + shostname + ":" + sportnumber + "/" + SSID;
- }
-
- Public list <string> getlist (string susrname, string spassword, string SQL ){
- List <string> resultlist = new arraylist <string> ();
- Try {
- Oracleconnectionpooldatasource ocpds = new oracleconnectionpooldatasource ();
- String url1 = system. getproperty ("jdbc_url ");
- If (url1! = NULL)
- Url = url1;
- Ocpds. seturl (URL );
- Ocpds. setuser (susrname );
- Ocpds. setpassword (spassword );
- Pooledconnection Pc = ocpps. getpooledconnection ();
- Connection conn = pc. getconnection ();
- Preparedstatement pstmt = conn. preparestatement (SQL );
- Resultset rset = pstmt.exe cutequery ();
- While (rset. Next ()){
- Resultlist. Add (rset. getstring (1 ));
- }
- Rset. Close ();
- Pstmt. Close ();
- Conn. Close ();
- PC. Close ();
- } Catch (exception e ){
-
- }
- Return resultlist;
- }
-
- /**
- * Test the connection.
- * @ Param ARGs
- */
- Public static void main (string [] ARGs ){
- // Use you real info
- String susrname = "";
- String spassword = "";
- String SQL = "";
-
- Jdbctest = new jdbctest ("localhost", "1521", "orcl ");
- List <string> List = jdbctest. getlist (susrname, spassword, SQL );
- System. Out. println (list. Size ());
- }
-
- }
Note the following:
Url = "JDBC: oracle: thin: @" + shostname + ":" + sportnumber + "/" + SSID;
In jdk1.6, you can use the above URL. That is, the port and Sid can be separated. In JDK 1.5, ":" is the only option ":".
In addition to using the connection pool, you can also use the traditional connection pool.
- Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
- Connection conn = DriverManager.getConnection(url, suUserName,passWord);
The weather is hot. Please eat watermelon.