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:
- statement st = conn.createstatement ();
- st.executequery (sql);
- PREPAREDSTATEMENT  
- string sql = "select * from table_name Where col_name=? ";
- preparedstatement ps = Conn.preparedstatement (SQL);
- ps.setstring (1, "Col _value ");
- 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)
- resultset rs = statement.executequery (sql);
- while (Rs.next ()) {
- Rs.getstring ("Col_name");
- rs.getint ("Col_name");
- //...
- }
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