DriverManger
java.sql.Drivermanager 类:注册驱动和创建连接
a. 注册驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建议使用
Class.forName("com.mysql.jdbc.Driver"); 建议使用
b.创建连接
connection
接口:通过connection实现程序与数据库的连接
String url=" jdbc:mysql://localhost:3306/test?user= &password= ";
Connection conn = DriverManager.getConnection(url);
c.statement接口
操作sql语句,并返回相应结果的对象
Statement stmt = comm.createStatement();
d.执行sql语句,得到结果
ResultSet resultSet = statement.executeQuery("select * from user;");
输出结果
while (resultSet.next()){
System.out.println(resultSet.getObject(1));
System.out.println(resultSet.getObject(2));
System.out.println(resultSet.getObject(3));
System.out.println(resultSet.getObject(4));
System.out.println(resultSet.getObject(5));
System.out.println();
}
e.关闭资源
resultSet.close();
statement.close();
connent.close();