JDBC Connection Database

Source: Internet
Author: User

1. Loading the JDBC Driver

1. Before connecting to the database, first load the driver of the database you want to connect to the JVM, which is implemented by the static method forname (String className) of the Java.lang.Class class;

2. After the load succeeds, the instance of the driver class is registered to the DriverManager class;

2. Provide a URL for the JDBC connection

1. Connection URL defines protocol, sub-protocol, data source indication (port) when connecting to a database

Writing form: Protocol: Sub-Protocol: port;

Protocol: Always start with JDBC in JDBC

Sub-Protocol: The name of a bridge-connected driver or database management system

Data source Indication (port): Tag find address of database source and connection port

3. Creating a connection to a database

To connect to a database, you need to request and obtain a Connection object from Java.sql.DriverManager that represents the connection to the database

Use the DriverManager getconnection (String url,string username,string Password) method to pass in the path of the specified database to be connected, the user name of the database, the database password to obtain

  

4. Create a statement

To execute an SQL statement, you must obtain a java.sql.Statement instance, and the Statement instance is divided into 3 types:

1. Execute static SQL statements: typically implemented with Statement instances

2. Execute dynamic SQL: typically using PreparedStatement instances

3. Executing stored procedures: typically using CallableStatement instances

5. Execute SQL statements

The Statement interface provides three ways to execute SQL: ExecuteQuery, executeupdate, execute

1. ResultSet executeQuery (String sqlString): Executes the SQL statement that queries the database, returning a result set (ResultSet) object.

2. int executeupdate (String sqlString): Execute INSERT, UPDATE, or DELETE statement

3. Execute (String sqlString): Used to perform statements that return multiple result sets, multiple update counts, or both;

6. Processing results

Two cases:

1. The execution of the update returns the number of records affected by this operation;

2. The result returned by the execution of the query is a ResultSet object;

ResultSet contains all rows that conform to the conditions in the SQL statement, and it provides access to the data in those rows through a set of Get methods.

Fetching data using the access method of the result set (ResultSet) object

7. Turn off JDBC

Once the operation is complete, turn off all JDBC objects used to release the JDBC resource, reverse the order and the Order of declaration

1. Close the result set

2. Closing the statement

3. Close the Connection object

1 //Database link Address2     Private Static FinalString Db_url = "JDBC:ORACLE:THIN:@192.168.1.249:1521:ORCL";3     //User name4     Private Static FinalString db_user = "sc1409";5     //Password6     Private Static FinalString db_pwd = "sc1409";7     //Database-driven8     Private Static FinalString db_driver = "Oracle.jdbc.driver.OracleDriver";9     Ten     /** One * Create a connection to a database A * The path to the specified database to connect to, the user name of the database, the database password -      * */ -      PublicConnection Getcon ()throwsClassNotFoundException, SQLException { the         //Load Driver - Class.forName (db_driver); -         //connecting to a database -Connection con =drivermanager.getconnection (Db_url, Db_user, db_pwd); +         returncon; -     } +      A     /** at * Turn off JDBC - * After the operation is complete, all JDBC objects used are turned off to release the JDBC resource, and the order of closing and declaration is reversed -      * */  -      Public voidCloseAll (Connection con, Statement stmt, ResultSet rs)throwsSQLException { -         //Close Result set -         if(rs! =NULL) Rs.close (); in         //Close Declaration -         if(stmt! =NULL) Stmt.close (); to         //Close the Connection object +         if(Con! =NULL&&!con.isclosed ()) con.close (); -     } the     

1. Common methods

1  Public intQuaryuser (user user)throwsexception{2         intresult = 0; 3Connection con =NULL;4PreparedStatement stmt =NULL;5ResultSet rs =NULL;6         Try{7String sql = "SELECT COUNT (1) from ccc_view WHERE cname =?";8Con =Getcon ();9stmt =con.preparestatement (SQL);TenStmt.setstring (1, User.getname ()); Oners =stmt.executequery (); A             if(Rs.next ()) { -result = Rs.getint (1); -             } the}Catch(Exception e) { - e.printstacktrace (); -}finally{ - CloseAll (Con, stmt, RS); +         } -         returnresult; +}

2. Stored Procedures

 Public intAddUser (user user)throwsexception{intresult = 0; Connection Con=NULL; CallableStatement CS=NULL; ResultSet RS=NULL; Try{con=Getcon (); CS= Con.preparecall ("{Call CCC_FFF (?,?,?)}"); Cs.setstring (1, User.getname ()); Cs.setstring (2, User.getpwd ()); Cs.registeroutparameter (3, types.float);        Cs.execute (); Result= Cs.getint (3); }Catch(Exception e) {e.printstacktrace (); }finally{CloseAll (Con, CS, RS); }    returnresult;}

3. Method functions

1  Public intLoginuser (user user)throwsexception{2         intresult = 0;3Connection con =NULL;4PreparedStatement stmt =NULL;5ResultSet rs =NULL;6String sql = "Select LOGIN_CCCF (?,?) From dual ";7         Try{8Con =Getcon ();9stmt =con.preparestatement (SQL); TenStmt.setstring (1, User.getname ()); OneStmt.setstring (2, User.getpwd ()); Ars =stmt.executequery (); -             if(Rs.next ()) { -result = Rs.getint (1); the System.out.print (result); -             } -}Catch(Exception e) { - e.printstacktrace (); +}finally{ - CloseAll (Con, stmt, RS); +         } A         returnresult; at}

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.