流程

Eclipse MySQL JDBC数据库连接:

1:注册驱动
Class.forName("com.mysql.jdbc.Driver");

2:获取连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称", "数据库用户名","数据密码");

3:声明执行sql语句的对象(Statement):
Statement stmt = conn.createStatement();

4:执行sql语句,并返回结果(ResultSet):
ResultSet rs = stmt.executeQuery("select id,name,password,email,birthday from t_user");

5:处理结果过程(如果有下一行进行循环):
while (rs.next()) {
              System.out.println(rs.getObject("id"));
              System.out.println(rs.getObject("name"));
              System.out.println(rs.getObject("password"));
              System.out.println(rs.getObject("email"));
              System.out.println(rs.getObject("birthday"));
              System.out.println("------------");
          }

5: 关闭MySQL数据库连接(Connection);
rs.close();
stmt.close();
conn.close();

运行结果

源码

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 (1006,'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);
	connet.close();
}