

import java.sql.*; /** * Created with IntelliJ IDEA. * User: @别慌 * Date: 2019-06-09 * Time: 16:54 * Description: */ public class JDBC { public static void main(String arg[]) throws SQLException { //1.注册驱动 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //2.获取连接 Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); //3.获取操作数据库的预处理对象 PreparedStatement preparedStatement=connection.prepareStatement("select * from account"); //4.执行sql,得到结果集 ResultSet resultSet=preparedStatement.executeQuery(); //5.遍历结果集 while(resultSet.next()){ System.out.print(resultSet.getString("id")); System.out.print(" "); System.out.println(resultSet.getString("name")); } //6.释放资源 resultSet.close(); preparedStatement.close(); connection.close(); } }