Java JDBC Connection Database

Source: Internet
Author: User

I. Native JDBC Connection steps:
//1. Registering the database driver//com. MySQL.jdbc.DriverClass.forName ("database-driven full class name");//2. Get a database connectionConnection conn = Drivermanager.getconnection ("Connection URL","User name","Password" );//3. Create a sessionPreparedStatement PS =NULL;/4Execute SQL statements, add, delete, modify Records ps.executeupdate ("Add, delete, modify a recorded SQL statement"); //or query StatementsResultSet rs = ps.executequery ("SQL statement for querying Records"); //5. Working with query result sets while(Rs.next ()) {//operations on Records}    //6. Close the connectionRs.close ();  Ps.close (); Conn.close ();

2. using C3P0

Resource file

Driverclass = com.mysql.jdbc.Driver      = jdbc:mysql://localhost:3306/test     Username = root      123456

Establish a connection

Combopooleddatasource CPDs =NewCombopooleddatasource (); //Load Database driverTry{Cpds.setdriverclass ("Com.ibm.db2.jcc.DB2Driver"); } Catch(propertyvetoexception E1) {e1.printstacktrace (); }        //set the address, user name, and password to access the databaseCpds.setjdbcurl ("jdbc:db2://10.10.38.138:50000/malltest"); Cpds.setuser ("Db2inst1"); Cpds.setpassword ("Db2inst1"); //set some configuration of C3P0, use default if not setCpds.setminpoolsize (5); Cpds.setacquireincrement (5); Cpds.setmaxpoolsize ( -); Cpds.setmaxstatements ( the); Connection Conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try {        //Create a database connectionconn =cpds.getconnection (); //Get database Operations Objectstmt =conn.createstatement (); //manipulating databases to get result setsrs = Stmt.executequery ("SELECT * from Ly.t_merinf where merid= ' M0000178 '"); //working with result sets     while(Rs.next ()) {System. out. println (Rs.getstring ("Mername")); }    } Catch(SQLException e) {e.printstacktrace (); } finally {        //Close Result set    if(rs! =NULL) {            Try{Rs.close ();}Catch(SQLException e) {}}//Close Database Operations Object    if(stmt! =NULL) {            Try{Stmt.close ();}Catch(SQLException e) {}}//To close a database connection    if(Conn! =NULL) {            Try{Conn.close ();}Catch(SQLException e) {}}Try{Datasources.destroy (CPDs); } Catch(SQLException e) {e.printstacktrace (); }    }

3. using DBCP

Configuration file

driverclassname=com.ibm.db2.jcc.DB2Driver    url=jdbc:db2://10.10.38.138:50000/malltest     username=db2inst1    Password=db2inst1    initialsize=3     Maxactive=5    maxidle=3    minidle=1     maxwait=30000

Connecting to a database

//1. Create a connection poolDataSource ds =NULL; Try{Properties prop=NewProperties (); //to load a property file through a classpathProp.load (dbcptest.class. getClassLoader (). getResourceAsStream ("database/dbcp/dbcp.properties")); //Get Data sourceDS =Basicdatasourcefactory.createdatasource (prop); } Catch(IOException e) {e.printstacktrace (); } Catch(Exception e) {e.printstacktrace (); } Connection Conn=NULL; Statement stmt=NULL; ResultSet RS=NULL; Try {        //2. Get a database connectionconn =ds.getconnection (); //3. Create a Database Action objectstmt =conn.createstatement (); //4. Manipulating the database to get the result setrs = Stmt.executequery ("SELECT * from Ly.t_merinf where merid= ' M0000178 '"); //5. Working with result sets     while(Rs.next ()) {System. out. println (Rs.getstring ("Mername")); }    } Catch(SQLException e) {e.printstacktrace (); } finally {        //Close Result set    if(rs! =NULL) {            Try{Rs.close ();}Catch(SQLException e) {}}//Close Database Operations Object    if(stmt! =NULL) {            Try{Stmt.close ();}Catch(SQLException e) {}}//To close a database connection    if(Conn! =NULL) {            Try{Conn.close ();}Catch(SQLException e) {} }}

Java JDBC Connection Database

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.