import java.sql.*;
public class work {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 1.加载驱动
Class.forName("com.mysql.jdbc.Driver");//加载驱动【掌握】
// DriverManager.registerDriver(new Driver());//注册驱动【了解】
// 2.通过DriverManager获取数据库连接Connection
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
// 3.通过连接Connection获取执行语句对象Statement
Statement stmt = connection.createStatement();
// 4.通过执行语句对象Statement去执行SQL(操作数据库:1.更新—executeUpdate (insert,update,delete)
// 2. 查询—executeQuery(select))
// 4.查询所有学生信息
String sql = "select * from work";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()){
// System.out.println(rs.getString(2));//打印出第二列
System.out.println(rs.getString("name"));
}
// 5.释放资源
stmt.close();
connection.close();
}
}