JDBC Programming steps
1: Load driver has two modes of loading drive (1) Driver driver=new com.mysql.jdbc.Driver;
(2) Class.forName ("Com.mysql.jdbc.Driver");
2: The link database corresponds to two methods of linking the database (1) Connection connection=driver.connection ();
(2) Connection connection=drivermanage.getconnection ();
3: Execute SQL statement
4: Store the contents of the database in a container
5: Access the contents of the container in turn
6: Off, last Open first close
Method One:
Import com.mysql.jdbc.Statement;
Import java.sql.Connection;
Import Java.sql.Driver;
Import Java.sql.ResultSet;
Import java.util.Properties;
/**
* Created by I am master on 2016/9/29.
*/
public class Finallyjdbc {
public static void Main (string[] args) throws exception{
Driver driver=new com.mysql.jdbc.Driver ();
String url= "Jdbc:mysql://localhost:3306/student";
Properties Properties=new properties ();//properties is a configuration file that is primarily used to express configuration information. File Format key: value
Properties.put ("User", "root");
Properties.put ("Password", "root");
Connection Connection=driver.connect (url,properties);
Statement statement= (Statement) connection.createstatement ();
String query= "SELECT * from Studentinfo";
/* Put the data in the database into a container */
ResultSet rs=statement.executequery (query);
/* Access the data in the database in turn */
while (Rs.next ()) {
System.out.print (rs.getstring ("id"));
System.out.print (rs.getstring ("name"));
System.out.print (rs.getstring ("Age"));
System.out.println (rs.getstring ("class"));
}
Rs.close ();
Statement.close ();
Connection.close ();
}
}
Method One:
JDBC Connection Database