时间:2021-07-01 10:21:17 帮助过:19人阅读
1 // 对工具类的使用 2 public class JDBCDemo { 3 public static void main(String[] args) { 4 //获取链接 5 Connection connection = JDBCUtils.getConnention(); 6 //获取执行者 7 ResultSet resultSet=null; 8 PreparedStatement pStatement=null; 9 10 //定义将要执行的sql语句 11 String sql = "select * from products where cno=?"; 12 //存放结果集对象 13 List<Product> list = new ArrayList<Product>(); 14 try { 15 //获取语句执行者对象 16 pStatement = connection.prepareStatement(sql); 17 //设置参数 18 pStatement.setInt(1, 1); 19 //执行查询操作 20 resultSet = pStatement.executeQuery(); 21 //遍历结果集 22 while (resultSet.next()) { 23 Product product = new Product(); 24 product.setId(resultSet.getInt("pid")); 25 product.setName(resultSet.getString("pname")); 26 product.setPrice(resultSet.getInt("price")); 27 product.setNum(resultSet.getInt("pnum")); 28 product.setCno(resultSet.getInt("cno")); 29 product.setTimestamp(resultSet.getTimestamp("pdate")); 30 list.add(product); 31 } 32 } catch (SQLException e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 }finally { 36 //关闭连接 37 JDBCUtils.colse(pStatement,connection); 38 } 39 for (Product product : list) { 40 System.out.println(product); 41 } 42 } 43 }
数据库链接JDBC
标签:array arraylist public driver 指定 throw rgs 厂商 exception