欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用JDBC從數(shù)據(jù)庫中查詢數(shù)據(jù)的方法

 更新時間:2016年08月31日 10:26:59   投稿:jingxian  
下面小編就為大家?guī)硪黄褂肑DBC從數(shù)據(jù)庫中查詢數(shù)據(jù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

* ResultSet 結(jié)果集:封裝了使用JDBC 進(jìn)行查詢的結(jié)果

* 1. 調(diào)用Statement 對象的 executeQuery(sql) 方法可以得到結(jié)果集

* 2. ResultSet 返回的實際上就是一張數(shù)據(jù)表,有一個指針指向數(shù)據(jù)表的第一行的前面,

* 可以調(diào)用next()方法檢測下一行是否有效,若有效,返回true,且指針下移,

* 相當(dāng)于iterator 對象的 hasNext() 和 next()方法的結(jié)合體

* 3. 當(dāng)指針定位到一行時,可以通過調(diào)用getXxx(index) 方法或 getXxx(columnName) 方法獲取

* 每一列的值。例如:getInt(1)獲取第一列的值,getString("name")獲取列名為“name”的那一列的值

@Test
  public void testResultSet(){
    //獲取id=2的customers數(shù)據(jù)表的記錄,并打印
    Connection connection = null;
    Statement statement = null;
    ResultSet rs = null;
    try {
      //1.獲取Connection
      connection = JDBCTools.getConnection();
      //2.獲取Statement
      statement = connection.createStatement();
      //3.準(zhǔn)備Sql
      String sql = "SELECT * FROM CUSTOMERS WHERE ID=2";
      //4.執(zhí)行查詢,得到ResultSet
      rs = statement.executeQuery(sql);
      //5.處理ResultSet
      while(rs.next()){
        //rs.get+數(shù)據(jù)庫中對應(yīng)的類型+(數(shù)據(jù)庫中對應(yīng)的列別名)
        int id = rs.getInt("ID");
        String name = rs.getString("name");
        String email = rs.getString("email");
        Date birth = rs.getDate("birth");
        
        System.out.println(id);
        System.out.println(name);
        System.out.println(email);
        System.out.println(birth);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }finally{
      //6.關(guān)閉數(shù)據(jù)庫相應(yīng)的資源
      JDBCTools.release(rs, statement, connection);
    }
  }

關(guān)閉的那個方法在Tools里寫了一個重載的

這個只是最最最基礎(chǔ)的用JDBC進(jìn)行查詢的操作,日后不一定完善,看心情~

以上這篇使用JDBC從數(shù)據(jù)庫中查詢數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論