zoukankan      html  css  js  c++  java
  • JDBC 数据库连接

    三个类:

    Connection、Statement(PreparedStatement)、ResultSet 
     1         Connection conn = null ;
     2         Statement stm = null ;
     3         ResultSet res = null ;
     4         String sql = " select * from table1 " ;
     5         try{
     6             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
     7             conn = DriverManager.getConnection(conPro.get("jdbc:sqlserver://localhost:1433; DatabaseName=20170706_TestDb"),conPro.get("sa"),conPro.get("123456"));
     8             stm = conn.createStatement() ; 
    9 res = stm.executeQuery(sql); 10 while(res.next()){ 11 System.out.println(res.getString("Contract_code")+" "+res.getString("Contract_name")+" "+res.getFloat("Contract_money")); 12 } 13 }catch(Exception e){ 14 e.printStackTrace(); 15 }finally{ 16 try{ 17 if(res!=null) 18 res.close(); 19 if(stm!=null) 20 stm.close(); 21 if(conn!=null) 22 conn.close(); 23 }catch(SQLException e){ 24 e.printStackTrace(); 25 } 26 }

    使用 PreparedStatement 时,参数下标是从 1 开始的。

    1 String mySQL = "SELECT * FROM TABLE WHERE ID=?";
    2 conn = DriverManager.getConnection(Url, UserName, Password);
    3 prestmt = conn.prepareStatement(mySQL);
    4 prestmt.setString(1, "");
    5 res = prestmt.executeQuery();
  • 相关阅读:
    python函数
    python3基础4
    布尔值常识
    字典常用魔法方法
    字典相关常识
    元组相关常识
    列表及其魔法方法(list类中提供的方法)
    列表相关常识
    day11练习题
    字符串相关常识
  • 原文地址:https://www.cnblogs.com/crazytrip/p/7170304.html
Copyright © 2011-2022 走看看