Jackyss

导航

 
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis","root","liao1024");
String sql = "select * from user";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()){
System.out.println(resultSet);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if (resultSet!=null){
try{
resultSet.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if (preparedStatement!=null){
try{
preparedStatement.close();
}catch (SQLException e){
e.printStackTrace();
}
}
if (connection!=null){
try{
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
}
缺点:
1.数据库链接创建,释放频繁造成系统资源浪费从而影响系统性能--->使用数据库连接池
2.sql语句的变动需要改变源代码, 不符合开发的开闭原则,系统不易维护
3.对结果集(ResultSet)查询列名,如果能将数据封装为实体类解析更加方便
posted on 2020-06-13 10:32  Jackyss  阅读(94)  评论(0)    收藏  举报