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.
Objective: To learn the JDBC interface without having to learn the driver of each database.
Here I would like to illustrate a small demo, connect MySQL database, query to the table data:
First of all, the data in my MySQL database:
With JDBC, we're going to introduce Mysql-connector-java-5.1.41-bin.jar
/** * */package cn.snowing;import java.sql.connection;import java.sql.drivermanager;import java.sql.ResultSet;import Java.sql.sqlexception;import java.sql.statement;/** * @author: Snowing * @date: April 27, 2017 * */public class JdbcDemo1 {p Ublic static void Main (string[] args) throws SQLException, classnotfoundexception {String url = "Jdbc:mysql://localhost:3 306/MYDB1 ";//This is OK, the default port//string url =" JDBC:MYSQL:///MYDB1 "; String user = "root"; String Password = "1";//1. The load driver registered two drive//drivermanager.registerdriver (new Com.mysql.jdbc.Driver ());//2. Load driver Common mode, Only one driver Class.forName ("Com.mysql.jdbc.Driver") was registered;//2. Get the connection connection Conne = drivermanager.getconnection (URL, user, password);//3. Gets the Statament object that sends the SQL statement like the database statement st = Conne.createstatement ();//4. Send SQL to the database to get the result set returned by the database resultset rs = St.executequery ("select * from User;"); /5. Get data from an interface set while (Rs.next ()) {System.out.println ("" + Rs.getobject ("id")); System.out.println (Rs.getobject ("username")); System.out.println (Rs.getobject ("Birthday")); System.Out.println (Rs.getobject ("entry_date")); System.out.println (Rs.getobject ("job")); System.out.println (Rs.getobject ("salary")); System.out.println (Rs.getobject ("Resume")); System.out.println (Rs.getobject ("image"));} 6. Release the connection, it is important ah, do not forget!! Rs.close (); St.close (); Conne.close ();}}
Results:
SQL---->MYSQL Database 1------JDBC Quick Start