//1.加载驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //2.用户信息和URL String url="jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8"; String username="root"; String password="123456"; //3.连接成功,数据库对象,Connection 代表数据库 Connection connection = DriverManager.getConnection(url, username, password); //4.执行SQL的对象 statement 执行sql的对象 Statement statement = connection.createStatement(); //5.执行SQL的对象 去 执行SQL String sql="SELECT * FROM student"; ResultSet resultSet = statement.executeQuery(sql);//返回的结果集 while(resultSet.next()){ System.out.println("id="+resultSet.getObject("id")); System.out.println("姓名="+resultSet.getObject("name")); System.out.println("年龄="+resultSet.getObject("age")); System.out.println("学号="+resultSet.getObject("stunum")); System.out.println("***********************************************"); } //6.释放连接 resultSet.close(); statement.close(); connection.close();