如何通過使用JDBC的statement進(jìn)行數(shù)據(jù)操作

小編給大家分享一下如何通過使用JDBC的statement進(jìn)行數(shù)據(jù)操作,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)專注于企業(yè)營銷型網(wǎng)站、網(wǎng)站重做改版、道里網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5網(wǎng)站設(shè)計(jì)商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為道里等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

使用JDBC的statement進(jìn)行數(shù)據(jù)的查詢,基本步驟如下:

1. 初始化simpleDbSource對(duì)象

2. 獲得getconnection

3. createStatement 獲得查詢語句

4. executeUpdate, 執(zhí)行更新語句

5. 關(guān)閉使用的statement, connection, 注意次序不要弄錯(cuò)

注意:更新語句,執(zhí)行過一次后,column需要遞增,否則報(bào)錯(cuò)

Java代碼

/**     *      */    package db;         import java.io.FileNotFoundException;     import java.io.IOException;     import java.sql.Connection;     import java.sql.ResultSet;     import java.sql.SQLException;         /**     * @author sean     *      * 1. 初始化simpleDbSource對(duì)象     * 2. 獲得getconnection     * 3. createStatement 獲得查詢語句     * 4. executeUpdate, 執(zhí)行更新語句     * 5. 關(guān)閉使用的statement, connection, 注意次序不要弄錯(cuò)     *      * 注意:更新語句,執(zhí)行過一次后,column需要遞增,否則報(bào)錯(cuò)     */    public class StatementDemo {             private static String insertSql="insert into user values('7','sean','sean@mail.com','hellofromsean')";         private static String querySql ="select * from user";             /**         * @param args         */        public static void main(String[] args) {             // TODO Auto-generated method stub             DBSource dbSource;             Connection conn = null;             java.sql.Statement stmt = null;                          try {                 dbSource = new SimpleDBSource();                 conn = dbSource.getConnect();                 stmt = conn.createStatement();                                  //數(shù)據(jù)庫更新工作,包括create, drop, update, insert etc.                 stmt.executeUpdate(insertSql);                 System.out.println("執(zhí)行成功"+ insertSql);                                  //進(jìn)行數(shù)據(jù)庫查詢                 ResultSet rs = stmt.executeQuery(querySql);                                  //進(jìn)行遍歷                 while(rs.next()){                     System.out.println(rs.getInt(1)+ "\t");                     System.out.println(rs.getString(2)+ "\t");                     System.out.println(rs.getString(3)+ "\t");                     System.out.println(rs.getString(4)+ "\t");                     System.out.println("**********************");                 }                                                                } catch (FileNotFoundException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (IOException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (ClassNotFoundException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             } catch (SQLException e) {                 // TODO Auto-generated catch block                 e.printStackTrace();             }                          //依次關(guān)閉statement和conn數(shù)據(jù)庫連接對(duì)象,清空資源             finally{                 if(stmt!= null){                     try {                         stmt.close();                     } catch (SQLException e) {                         // TODO Auto-generated catch block                         e.printStackTrace();                     }                     stmt= null;                 }                 if(conn!=null){                     try {                         conn.close();                     } catch (SQLException e) {                         // TODO Auto-generated catch block                         e.printStackTrace();                     }                     conn= null;                 }             }         }     }     /**   *    */  package db;   import java.io.FileNotFoundException;  import java.io.IOException;  import java.sql.Connection;  import java.sql.ResultSet;  import java.sql.SQLException;   /**   * @author sean   *    * 1. 初始化simpleDbSource對(duì)象   * 2. 獲得getconnection   * 3. createStatement 獲得查詢語句   * 4. executeUpdate, 執(zhí)行更新語句   * 5. 關(guān)閉使用的statement, connection, 注意次序不要弄錯(cuò)   *    * 注意:更新語句,執(zhí)行過一次后,column需要遞增,否則報(bào)錯(cuò)   */  public class StatementDemo {    private static String insertSql="insert into user values('7','sean','sean@mail.com','hellofromsean')";   private static String querySql ="select * from user";    /**    * @param args    */   public static void main(String[] args) {    // TODO Auto-generated method stub    DBSource dbSource;    Connection conn = null;    java.sql.Statement stmt = null;        try {     dbSource = new SimpleDBSource();     conn = dbSource.getConnect();     stmt = conn.createStatement();          //數(shù)據(jù)庫更新工作,包括create, drop, update, insert etc.     stmt.executeUpdate(insertSql);     System.out.println("執(zhí)行成功"+ insertSql);          //進(jìn)行數(shù)據(jù)庫查詢     ResultSet rs = stmt.executeQuery(querySql);          //進(jìn)行遍歷     while(rs.next()){      System.out.println(rs.getInt(1)+ "\t");      System.out.println(rs.getString(2)+ "\t");      System.out.println(rs.getString(3)+ "\t");      System.out.println(rs.getString(4)+ "\t");      System.out.println("**********************");     }                   } catch (FileNotFoundException e) {     // TODO Auto-generated catch block     e.printStackTrace();    } catch (IOException e) {     // TODO Auto-generated catch block     e.printStackTrace();    } catch (ClassNotFoundException e) {     // TODO Auto-generated catch block     e.printStackTrace();    } catch (SQLException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }        //依次關(guān)閉statement和conn數(shù)據(jù)庫連接對(duì)象,清空資源    finally{     if(stmt!= null){      try {       stmt.close();      } catch (SQLException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }      stmt= null;     }     if(conn!=null){      try {       conn.close();      } catch (SQLException e) {       // TODO Auto-generated catch block       e.printStackTrace();      }      conn= null;     }    }   }  }    /**   *   */   package db;    import java.io.FileNotFoundException;   import java.io.IOException;   import java.sql.Connection;   import java.sql.ResultSet;   import java.sql.SQLException;   import java.sql.Statement;    /**   * @author sean   *   * 1. 初始化simpleDbSource對(duì)象   * 2. 獲得getconnection   * 3. createPreparedStatement 獲得查詢語句   * 4. 設(shè)置具體更新內(nèi)容,setInt(colIndex, value), setString(colIndex,value)   * 4. executeUpdate, 執(zhí)行更新語句   * 5. 關(guān)閉使用的PreparedStatementstatement, connection, 注意次序不要弄錯(cuò)   *   * 注意:更新語句,執(zhí)行過一次后,column需要遞增,否則報(bào)錯(cuò)   */   public class PreparedStatementDemo {    private static String querySql ="select * from user";   private static String pstmtSql = "insert into user values(?,?,?,?)";    Connection conn1;   static Statement stmt;   /**   * @param args   */   public static void main(String[] args) {   // TODO Auto-generated method stub   DBSource dbSource;   Connection conn = null;   java.sql.PreparedStatement pstmt = null;    try {   dbSource = new SimpleDBSource();   conn = dbSource.getConnect();   pstmt = conn.prepareStatement(pstmtSql);    pstmt.setInt(1, 9);   pstmt.setString(2, "sean");   pstmt.setString(3, "my@hotmail.com");   pstmt.setString(4, "add some comments");    //數(shù)據(jù)庫更新工作,包括create, drop, update, insert etc.   pstmt.executeUpdate();    //清空設(shè)置的參數(shù),為后續(xù)更新準(zhǔn)備   pstmt.clearParameters();    System.out.println("執(zhí)行成功"+ pstmtSql);    //進(jìn)行數(shù)據(jù)庫查詢   Connection conn1 = dbSource.getConnect();   Statement stmt = conn1.createStatement();   ResultSet rs = stmt.executeQuery(querySql);    //進(jìn)行遍歷   while(rs.next()){   System.out.println(rs.getInt(1)+ "\t");   System.out.println(rs.getString(2)+ "\t");   System.out.println(rs.getString(3)+ "\t");   System.out.println(rs.getString(4)+ "\t");   System.out.println("**********************");   }   } catch (FileNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();   } catch (IOException e) {   // TODO Auto-generated catch block   e.printStackTrace();   } catch (ClassNotFoundException e) {   // TODO Auto-generated catch block   e.printStackTrace();   } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();   }    //依次關(guān)閉jdbc的statement和conn數(shù)據(jù)庫連接對(duì)象,清空資源   finally{   if(stmt!= null){   try {   stmt.close();   } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();   }   stmt= null;   }    if(pstmt!= null){   try {   pstmt.close();   } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();   }   pstmt= null;   }    if(conn!=null){   try {   conn.close();   } catch (SQLException e) {   // TODO Auto-generated catch block   e.printStackTrace();   }   conn= null;   }   }   }   }

以上是“如何通過使用JDBC的statement進(jìn)行數(shù)據(jù)操作”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

標(biāo)題名稱:如何通過使用JDBC的statement進(jìn)行數(shù)據(jù)操作
當(dāng)前網(wǎng)址:http://bm7419.com/article38/igehsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、網(wǎng)站建設(shè)、定制開發(fā)、ChatGPT、Google、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作