JDBC (Java Data Base Connectivity) is a Java API for executing SQL statements that can provide unified access to a variety of relational databases It consists of a set of classes and interfaces written in the Java language.
JDBC is an interface model for Java programmers that are APIs for service providers that implement database connections. As a APIJDBC provides a standard interface for program development.
Loading the JDBC Driver
Before connecting to the database, you first load the driver of the database you want to connect to the Jvmjava virtual machine, which is implemented by the static method forname (String className) of the Java.lang.Class class.
For example
try{
Class.forName ("Com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e) {
System.out.println ("Failed to find driver class load driver");
E.printstacktrace ();
}
After a successful load, an instance of the driver class is registered to the Drivermanger class.
Provide a URL for the JDBC connection
The connection URL defines the protocol sub-Protocol data source identity when the database is connected.
Written form protocol: sub-Protocol: Data source identification
The protocol always starts with JDBC in JDBC
The sub-protocol is the driver of the bridge connection or the name of the database management system
The data source identification token locates the address of the database source and the connection port.
For example, the connection URL for MySQL
jdbc:mysql://localhost:3306/test?useunicode=true&characterencoding=gdk;
Where Useunicode=true: Indicates the use of the Unicode character set. This parameter must be set to True if Characterencoding is set to gb2312 or GBK. CHARACTERENCODING=GBK: The character encoding method.
Create a database connection
To connect to a database you want to request to Java.sql.DriverManager and get the Connection object This object represents a connection to a database.
Use the DriverManager getconnection (string URL, string username, string password) method to obtain the user name and password for the path database of the specified database to connect to.
For example
String url = "Jdbc:mysql://localhost:3306/test";
String username = "root";
String password = "root";
try{
Connection con = drivermanager.getconnection (URL, username, password);
}catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("Database connection failed");
E.printstacktrace ();
}
Create a statement
To execute an SQL statement you must obtain the Java.sql.Statement instance statement instance into the following 3 types
1. Execute the static SQL statement. Typically implemented through statement instances.
2. Execute the dynamic SQL statement. Typically implemented through PreparedStatement instances.
3. Execute the database stored procedure. Typically implemented through CallableStatement instances.
Specific implementation methods
Statement stmt = Con.createstatement ();
PreparedStatement pstmt = con.preparestatement (sql);
CallableStatement Cstmt=con.preparecall ("{Call DEMOSP (?,?)}";
Execute SQL statement
The statement interface provides three ways to execute SQL statements executequery, executeupdate, and execute
1. ResultSet executeQuery (String sqlString): The SQL statement that executes the query database returns a result set Resutltset object.
2. int executeupdate (String sqlString): Used to perform INSERT, UPDATE or DELETE statements, and SQL DDL statements such as CREATE table and drop table.
3. Execute (sqlString): Used to perform statements that return multiple update counts or combinations of multiple result sets.
Implementation-specific code
ResultSet rs = stmt.executequery ("SELECT * from ...");
int rows = Stmt.executeupdate ("INSERT into ...");
Boolean flag = Stmt.execute (String sql);
Processing results
Two cases
1. The update is performed to return the number of records affected by this operation.
2. The result returned by the execution 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 result set ResultSet object access method
while (Rs.next ()) {
String name=rs.getstring ("name");
String pass=rs.getstring ("1");
}//Columns are numbered from left to right and start with column 1
Close the JDBC Object
After the operation is complete, all the JDBC objects used are closed to release the JDBC resource close order and the declaration order is reversed
1. Closing a recordset
2. Closing the statement
3. Close the Connection object
if (rs!=null) {//close record set
try{
Step1. Create a MAVEN project.
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6F/04/wKioL1WPwt-QDzr7AAI7EjhHTs0341.jpg "title=" Open Eclipse "alt=" wkiol1wpwt-qdzr7aai7ejhhts0341.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6F/07/wKiom1WPwiGgfKn3AANceK5ceBg699.jpg "title=" 2.png " alt= "Wkiom1wpwiggfkn3aancek5cebg699.jpg"/>
This article is from the "Practitioners" blog, please be sure to keep this source http://infra.blog.51cto.com/9150766/1668759