---restore content starts---
1. First describe what is JDBC
Before I saw this noun at first glance, I felt it necessary to know what its English name was, Java Database Connectivity, literally interpreted as
A connected Java database, that is, to provide a set of intermediate links, to connect Java code and database, so that it can directly manipulate Java code to control the database.
The essence of 2.JDBC
It is a Java Access database solution, hoping to access different databases in the same way, in order to achieve a specific database-independent Java interface.
JDBC is essentially a set of standardized interfaces, different database vendors according to their own database characteristics to implement the interface, the implementation of the specific methods do not need us to care.
The main interfaces implemented by the database vendors in JDBC are as follows:
DriverManager: Drive Management
Connection,
DatabaseMetaData: Connection interface
Statement,
PreparedStatement
CallableStatement: Statement Object interface
ResultSet,
ResultSetMetaData: Result set interface
How the 3.JDBC works
The programmer calls the part of the underlying database vendor implementation,
That is: 1) The first step connection is achieved via the connection interface
2) Transferring SQL statements via statement results
3) database return results are saved to the ResultSet result collection connector
The main process can be summarized as:
1) Load driver, create connection
2) Create statement object
3) Execute SQL statement
4) Send back the result set
5) Close the connection
Let me take a look at the Oracle database that I've learned. Eclips as an example, step through how to implement JDBC
1) Load Driver
The method used is: Class.forName ("Drive");
The way I know how to get to this driver is: Expand the JDBC jar package, as I'm using Ojdbc6.jar, Find Oracle.jdbc.driver inside, find Oracledriver in the inside and then right click Qualitycopy, paste into quotation marks, such as: Class.forName (" Oracle.jdbc.driver.OracleDriver ");
2) Create a connection
The method used is: Conn=drivermanager.getconnection ("Jdbc:oracle:thin: @IP Address: 1521:ORCL", "Database Account", "Database Password");
such as: Conn=drivermanager.getconnection ("Jdbc:oracle:thin:@172.16.3.8:1521:orcl", "jsd1601", "jsd1601");
3) Create statement object
The method used is: Statement stmt=conn.createstatement ();
It is important to note that the connection that was created on the second step is used to invoke the method
Call the Executeupdate method to pass SQL statements into it to execute the SQL statement you write, and note that the type of SQL that the Executeupdate method can execute is insert,update,delete
such as: Statement stmt=conn.createstatement ();
String sql= "INSERT into Emp_jiawenzhe (Empno,ename,sal,deptno) VALUES (1000, ' Jia Wenji ', 1500,10)";
int i=stmt.executeupdate (SQL);
The return value I in this is the number of rows affected, and we can determine whether the operation was successful based on the number of rows affected.
4) and for the returned result set mainly refers to the select operation (not mentioned here)
5) finally close the connection
such as: Conn.close ();
List a whole code, including comments:
1 Packagejdbc_day01;2 ImportJava.sql.*;3 /**4 * Procedure for demonstrating JDBC5 * 1. Load driver6 * 2. Create a connection7 * 3. Create statement Object8 * 4. Send SQL statements9 * 5. If a SELECT statement is sent, the result set is processedTen * 6, close the connection One * @authorJiawenzhe A * - */ - Public classJDBCDOME01 { the Public Static voidMain (string[] args)throwsSQLException { - //Requirements: Create an employee, employee number, employee name, salary, department number - //1. -Connection conn=NULL; + Try { -Class.forName ("Oracle.jdbc.driver.OracleDriver"); +SYSTEM.OUT.PRINTLN ("Driver loading succeeded"); A //2. atconn= -Drivermanager.getconnection ("Jdbc:oracle:thin:@172.16.3.8:1521:orcl", "jsd1601", "jsd1601"); - System.out.println (Conn.getclass (). GetName ()); - //3. - //The statment statement object. Sending and executing SQL statements - /* in * int excuteupdate (String sql); - * Send Insert,update,delete statement to * return value int indicates the number of rows affecting the database table + */ -Statement stmt=conn.createstatement (); theString sql= "INSERT into Emp_jiawenzhe (EMPNO,ENAME,SAL,DEPTNO)" *+ "VALUES (1000, ' Wangxiao ', 1500,10)"; $ intI=stmt.executeupdate (SQL);Panax Notoginseng if(i>0){ -SYSTEM.OUT.PRINTLN ("Saved successfully! "); the } +}Catch(ClassNotFoundException e) { A e.printstacktrace (); the //1. Record Log + //2. Notify Callers - Throw NewRuntimeException ("Load driver error", e); $}finally{ $ //Close Connection - if(conn!=NULL){ - Try { the conn.close (); -}Catch(SQLException e) {Wuyi e.printstacktrace (); the } - } Wu } - About $ - - } -}
Note: The jar package that can be used by the Oracle database: Ojdbc14.jar/ojdbc6.jar (oracle12c with this)
The jar package that the MySQL database can use: Mysql-connector-java-5.0.4-bin.jar
Load Driver class:
Class.forName ("Com.mysql.jdbc.Driver");
Not to be continued!
---restore content ends---
Java--jdbc Summary