A, download mysql driver central.maven.org/maven2/mysq…
package testJdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class test2 {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
//1. Load the drive
String JDBCDriver = "com.mysql.jdbc.Driver";
Class.forName(JDBCDriver);
// connect to the database
String url="JDBC: mysql: / / 127.0.0.1:3306 / school"; // The url that links the data
String user="root"; // User name for logging in to the database
String password="123456"; // The user's password
Connection conn = DriverManager.getConnection(url, user, password);
Statement st = conn.createStatement();
// select * from 'SQL'
String sql="select * from student";
ResultSet rs = st.executeQuery(sql);
System.out.println("SId"+""+"Sname"+""+"Sage"+""+"Ssex");
while (rs.next()) {
System.out.println(rs.getString("SId") +""+rs.getString("Sname") +""+rs.getString("Sage") +""+rs.getString("Ssex"));
}
Int update = st.executeUpdate("update student set Sname='xiaolizi' where SId=2); System.out.println(" update success: "+update); * /
// Delete
//st.execute("delete from student where SId='2'");
// Add data
// String sqL1 = "insert into sc (SId,Sname,Sage) values('4',' 4',' 4',' 2');
// st.execute(sql1);
// Close the connectionconn.close(); st.close(); rs.close(); }}Copy the code