@Test
public void testQueryAll() {
// 1.注册驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.设置参数
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
String username = "root";
String password = "root";
// 3.获取连接对象
Connection conn = DriverManager.getConnection(url, username, password);
// 4.获得执行sql语句的对象
Statement stmt = conn.createStatement();
// 5.编写SQL语句
String sql = "select * from product";
// 6.执行sql语句
ResultSet rs = stmt.executeQuery(sql);
// 7.处理结果集
while (rs.next()) {
String pname = rs.getString(2);
Object price = rs.getObject(3);
System.out.println("商品名称:" + pname + ",商品价格:" + price);
}
//8.释放资源
if(rs!=null)rs.close();
if(stmt!=null)stmt.close();
if(conn!=null)conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
在eclipse中不用导junit-4.7.jar 但是在idea中要导入
注意要导入mysql驱动包