J2ee--jdbc

Source: Internet
Author: User

    JDBC (Java Data Base Connectivity,java database connection) is a Java API for executing SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. In short, JDBC can do three things: establish a connection to a database, send an SQL statement, and process the result (which acts like a SqlHelper class in a SQL Server database we used to use).
One, the JDBC driver:
Second, the use of JDBC steps:
1. Registration driver (only once) class.forname ("Com.mysql.jdbc.Driver");
2. Establish a connection (Connection) 1) use DriverManager to get the link, you need to pass in three parameters: the number of data is the URL, user name, password. as follows:
Connection conn = drivermanager.getconnection (URL, user, password);

2) through Connecton Object Creation Statement object, there are three ways of


Createstatement () Creates a basic statement object.

Preparestatement (Stringsql): Creates a precompiled statement object based on the incoming SQL statement.

Preparecall (stringsql): Create CallableStatement objects based on incoming SQL statements

3. Create a statement that executes SQL (Statement) 1) Execute the SQL statement code as follows:
    1. statement st = conn.createstatement ();   
    2. st.executequery (sql);   
    3. PREPAREDSTATEMENT  
    4. string sql =  "select * from table_name  Where col_name=? ";   
    5. preparedstatement ps =  Conn.preparedstatement (SQL);   
    6. ps.setstring (1,  "Col _value ");   
    7. ps.executequery ();  
2) Statement execute SQL statement, there are three methods to execute

Execute: Can execute any SQL statement, single more troublesome

Executeupdate: DML, DDL statements can be executed. The execution DML returns the number of rows affected by the SQL statement, and the execution DDL returns 0;

executequery: Only query statements are executed, and ResultSet objects that represent the results of the query are returned after execution.

4. Processing execution Results (RESULTSET)
  1. resultset rs = statement.executequery (sql);   
  2. while (Rs.next ()) {  
  3. Rs.getstring ("Col_name");   
  4. rs.getint ("Col_name");   
  5. //...   
  6. }  
5. Releasing Resources release resultset, Statement,connection.

Summary: Through understanding and analysis, in essence, like ADO, three things are done: Establish a connection with the database, send SQL statements, and process the results.

J2ee--jdbc

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.