No more headaches since then. Summary of database connection methods

Source: Internet
Author: User
Data | database | database connection

1. There are two JDBC driver registration methods:------------------

Import Java.lang.Class; Method One
Class.forName ("Oracle.jdbc.OracleDriver"); Actually, no introduction.
Import Java.sql.DriverManager; Method Two:
Drivermanager.registerdriver (New Oracle.jdbc.OracleDriver ());

2. Open database connection, method two:------------------
Drivermanager.getconnection (Url,username,password); Method One:
Example:
Connection Myconnection=drivermanager.getconnection (
"Jdbc:oracle:thin@localhost:1521:myoracle",
"Name",
"Password");
Import Oracle.jdbc.pool.OracleDataSource; Method Two: Using a data source
Oracledatasource mydatasource=new Oracledatasource (); Create a data source
Mydatasource.setservername ("localhost");
Mydatasource.setdatabasename ("MyOracle");
Mydatasource.setdrivertype ("OCI");
Mydatasource.setnetworkprotocol ("TCP"); Setting properties
Mydatasource.setportnumber (1521);
Mydatasource.setuser ("name");
Mydatasource.setpassword ("password");
Connect the database, you can reassign the user name and password, to the different identity connection query;
Connection myconnection=mydatasource.getconnection ("username", "password");

3. Create a JDBC Statement object----------------
You can prohibit automatic submission of myconnection.setautocommit (false);
Statement mystatement=myconnection.createstatement ();
4. Access to information from the database, that is, the use of statement objects
ResultSet Myresultset=mystatement.executequery (
"Select Userid,username from table"); Get all the information
String result=myresultset.getstring ("userid"); Get a message
if (Myresultset.wasnull ()) {
};
Resultset.next ();
Myresultset.close (); Shut down
Submit Myconnection.commit () here;
5. Close the JDBC object-------------
Mystatement.close ();
Myconnection.close ();
/* Generally used in the following format:
finally{
if (mystatement!=null) {
Mystatement.close ();}
if (myconnection!=null{
Myconnection.close ();}
}
*/
6. Use JDBC PreparedStatement object-------------
PreparedStatement Myprepstatement=myconnection.preparedstatement (
"INSERT into the products" +
"(Id,type_id,name) VALUES (" +
"?,?,?"
")" //
);
Myprepstatement.setint (1,100);
Myprepstatement.setint (2,1100);
Myprepstatement.setstring (3, "username"); Using the Set method
Myprepstatement.excute (); Submit
/* Two, connecting to the database using connection caching and connection pooling
You can add a connection to the connection buffer pool, if necessary, by establishing a connection cache.
*/
The connection buffer pool is discussed first------------
Import javax.sql.*;
Import oracle.jdbc.pool.*;
Oracleconnectionpooldatasource Myoracle=mew Oracleconnectionpooldatasource ();
To create an object Oracleconnectionpooldatasource the connection buffer pool data source
Implements the ConnectionPoolDataSource interface
2. Use this object to set properties for a physical database connection
Myoracle.setservername ("localhost");
Myoracle.setdatabasename ("MyOracle");
Myoracle.setportnumber (1521);
Myoracle.serdrivertupe ("thin");
Myoracle.setuser ("user");
Myoracle.setpassword ("password");
3. Create a buffered Connection object that represents a physical database connection that you can use to request a database connection instance
Pooledconnection mypooledconnection= myoracle.getpooledconnection ();
Pooledconnection mypooledconnection= myoracle.getpooledconnection ("User", "password");
Establish a connection, you can request the connection instance multiple times, and then close the Connection object
4. Request, use, and finally close the connection instance
Connection myconnection=mypooledconnection.getconnection ();
Use
Myconnection.close ();
5. Close the Pooledconnection object before the end of the program
Mypooledconnection.close ();
/* Use connection Cache-------------------
*/
/*1. Create the Oracleconnectioncacheimpl object, he implements the Oracleconnectioncacheclass interface
This interface extends the Oracledatasource class, and you can use all of the properties and methods of this class
The easiest way to create this is to use the default constructor.
Oracleconnectioncacheimpl myocci=new Oracleconnectioncacheimpl ();
Myocci.setservername ("localhost");
Myocci.setdatabasename ("MyOracle");
Myocci.setportnumber (1521);
Myocci.setdrivertype ("thin");
Myocci.setuser ("name");
Myocci.setpassword ("password");
You can also set properties for it by using an existing Oraclepooldatasource object
Oracleconnectioncacheimpl myocci=new Oracleconnectioncacheimpl (myoracle);
Or use the following methods
Myocci.setconnectionpooldatasource (myoracle);
2. Request, use, and close connection instances
Connection myconnection=myocci.getconnection ();
The/*pooledconnection object represents a physical database connection, and by default, when the Myocci.getconnection () method is invoked,
Myocci Check the cache for the presence of Pooledconnection objects, without creating a
Then, check to see if the Pooledconnection object has an idle connection instance, without creating
and returns the connection instance of the Pooledconnection object
*/
You can request another connection instance here
Connection myconnection2=myocci.getconnection ();
Close Connection Instance
Myconnection.close ();
The Oracleconnectioncacheimpl object should be closed before the program ends, and all Pooledconnection objects will be closed
Myocci.close ();
/* Use the OCI driver to implement the connection buffer pool------------------------
Advantages: The number of physical connections can be dynamically configured
Each connection instance can have a different user name password and can use a buffer pool to access different user modes
Ability to have multiple buffering connections, thin only one database connection
*/
Oracleociconnectionpool myoocp=new Oracleociconnectionpool ();
/* Myoocp.set ...
......
......
*/
The following optional properties that can be set dynamically when the program is run
/*
Properties Myproperties=new properties ();
Myproperties.put (Oracleociconnectionpool.connpool_min_limit, "5");
My ..... Connpool_max_limit, "10");
... _increment, "2", etc......
_timeout, "30");
_nowait, "true");
Myoocp.setpoolconfig (myproperties);
*/
To establish a connection instance
Oracleociconnection myconnection= (oracleociconnection) myoocp.getconnection ();
Myconnection.close ();
Myoocp.close ();



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.