流程

查看代码注解

实现

代码

package my;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.cj.protocol.Resultset;

public class jdbc {

public static void main(String[] args) throws ClassNotFoundException, SQLException {
	// 加载mysql驱动
	Class.forName("com.mysql.cj.jdbc.Driver");
	System.out.println("Success loading Mysql Drvier!");

	// 连接mysql数据库
	String url = "jdbc:mysql://localhost:3306/mysql?serverTimezone=GMT%2B8";
	String user = "root";
	String password = "@abc1234";
	Connection connet = DriverManager.getConnection(url, user, password);
	System.out.println("Successful connecting to mysql SERVER");

	// Statement来执行sql语句
	Statement Statement = connet.createStatement();
	String sql = "SELECT * FROM sys.user";

	// 滚动逐条读取mysql数据库
	ResultSet rs = Statement.executeQuery(sql);
	while (rs.next()) {
		String idUser = rs.getString("idUSER");
		String name = rs.getString("name");
		String age = rs.getString("age");
		String sex = rs.getString("sex");
		String mobile = rs.getString("mobile");
		System.out.println(idUser + "  " + name + "  " + age + "  " + sex + "  " + mobile);
	}

	// 插入一行到MYSQL
	String sqlInsert = "insert into sys.user(idUSER,name,age,sex,mobile,place) values (1005,'Lucy', '60','女','13999889900','北京');";
	Statement.executeUpdate(sqlInsert);

	//Lucy电话改成18866668888
	String sqlUpdate="update sys.user set mobile='19966668888' where idUSER=1005;";
	Statement.executeUpdate(sqlUpdate);

	//删除mysql某一行
	String sqlDelete ="delete from sys.user where idUSER='1005';";
	Statement.executeUpdate(sqlDelete);
}

}