当前位置:Gxlcms > 数据库问题 > 数据库链接JDBC

数据库链接JDBC

时间:2021-07-01 10:21:17 帮助过:19人阅读

//代码演示: 2 //我们封装一个工具类来提高代码的扩展性 3 public class JDBCUtils { 4 private static final String url="jdbc:mysql://localhost:3306/test"; 5 private static final String username="root"; 6 private static final String password = "root"; 7 /** 8 * 注册驱动 9 */ 10 static { 11 try { 12 Class.forName("com.mysql.jdbc.Driver"); 13 } catch (ClassNotFoundException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 } 17 } 18 /** 19 * 获取链接 20 * @return 21 */ 22 public static Connection getConnention() { 23 Connection connection=null; 24 try { 25 connection = DriverManager.getConnection(url,username,password); 26 } catch (SQLException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } 30 return connection; 31 } 32 /** 33 * 通过可变参关闭连接 34 * @param objects 35 */ 36 public static void colse(Object ...objects) { 37 try { 38 if (objects!=null && objects.length>0) { 39 for(int i = 0;i<objects.length;i++) { 40 if(objects[i] instanceof Statement) { 41 ((Statement)objects[i]).close(); 42 }else if(objects[i] instanceof Connection) { 43 ((Connection)objects[i]).close(); 44 } 45 } 46 } 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } 50 } 51 }
 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   

人气教程排行