After downloading and configuring the JDBC driver, we can use it.
First, connect the database
There are three steps to follow when using JDBC to connect to a database to access data:
- Loading and registering the appropriate JDBC driver
Load using
Class.forName ();
After loading, the driver builds a driver object and causes Drivermanager.rigisterdriver () to register the object automatically.
Using URLs to identify the database in JDBC,
jdbc:< Sub-Protocol >:< sub-name >
When using MySQL, the syntax is as follows
Jdbc:mysql://hostname:port/databasename?param=value1
Sub-protocol: MySQL
Sub-name://hostname:port/databasename?param=value1, where param can have more than one.
For example:
jdbc:mysql://localhost:3306/sample?user=root&password=root;
- Establishes a connection object to the specified database
There are two ways of doing this:
1.
String url = "Jdbc:mysql://localhost:3306/sample?user=root&password=root";
Connection con = drivermanager.getconnection (URL);
2.
String url = "Jdbc:mysql://localhost:3306/sample";
String user = "root";
String password= "root";
Connection con = drivermanager.getconnection (Url,user,password);
- Submit database queries and get query objects
After acquiring the connection object for the database, it is possible to create a declarative object that is used primarily to transfer SQL statements to the database server and execute SQL statements.
As follows:
Statement stmt= con.createstatement ();
There are three ways to execute statements on statement objects:
ExecuteQuery (): Executes the query.
Executeupdate (): Perform the update.
Execute (): Do not know what is going to be done.
After execution, the database returns a ResultSet collection object into JDBC. The principle is as follows:
Connection------>preparedstatement-------->resultset
|------>statement----------------->resultset
|-------->callablestatement------>RESULTSET
Ii. using code to connect to a database
We first set up a Testmysql database with a student table in it.
Create Databse testmysql;//View Show databases;//build table user Testmysql;create table student (id int (3), name char, sex Boolean);//Add data insert into student (' 1 ', ' lishengjing ', ' 1 ');//View Data select * from student;
Then we load the JDBC, right-click Project in this project and select MySQL in the user library in Build path.
and finally the code. As follows
Package Chuiyuan;import Java.sql.connection;import Java.sql.drivermanager;import java.sql.resultset;import Java.sql.sqlexception;import Java.sql.statement;public class Jdbcexample {public static void main (String args[]) {try { Class.forName ("Com.mysql.jdbc.Driver"); String url = "Jdbc:mysql://localhost:3306/testmysql?user=root&password=chuiyuan"; Connection Connection = drivermanager.getconnection (URL); Statement stmt = Connection.createstatement (); ResultSet rs = stmt.executequery ("SELECT * from Student"), while (Rs.next ()) {System.out.println ("Student Number:" + Rs.getint (1)); System.out.println ("Student Name:" +rs.getstring (2)); System.out.println ("stutent sex:" + rs.getboolean (3) + "\ n");} Rs.close (); Stmt.close (); Connection.close ();} catch (ClassNotFoundException e) {e.printstacktrace ();} catch (SQLException e) {e.printstacktrace ();}}}
Basic application of JDBC