首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > 其他数据库 >

jdbc之运用占位符的增删改查

2013-01-23 
jdbc之使用占位符的增删改查package com.hanchao.jdbcimport java.sql.Connectionimport java.sql.Drive

jdbc之使用占位符的增删改查

  1. package com.hanchao.jdbc;
  2. import java.sql.Connection;import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;import java.sql.ResultSet;
  4. /**
  5. * jdbc学习总结二 * @author hanlw
  6. * 2012-07-09 */
  7. public class TestJdbcNew {
  8.     /**      * 上一篇文章,我们对JDBC有了初步的了解,并且知道了如何使用JDBC了。
  9.      * 但是,那只是JDBC的了解性操作实例。实际开发中,我们是不能那么玩的!!     *
  10.      * ★下面我们学习一下:JDBC的占位符的使用,以后写程序建议都要这么玩的!!     *
  11.      * 注意事项:我们的异常应该捕获;而不是抛出来啊!!     *
  12.      * SQLException是非运行时异常,必须要捕获或者向上抛出!!★     */
  13.          public staticvoid main(String[] args) throws Exception {
  14.         /**          * 1.jdbc对Mysql的insert操作
  15.          */ //      insert("cherry","shanghai");
  16.                  /**
  17.          * 2.jdbc对mysql的update操作         */
  18. //      update("update",12);        
  19.         /**          * 3.jdbc对mysql的delete操作
  20.          */ //      delete(12);
  21.                  /**
  22.          * 4.jdbc对mysql的retrieve 操作         */
  23.         retrieve(15);     }
  24.          /**
  25.      * jdbc对mysql的insert操作      *
  26.      * @param username 用户名      * @param address 地址
  27.      */     public staticvoid insert(String username,String address)throws Exception {
  28.         Class.forName("com.mysql.jdbc.Driver");        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
  29.                  //注意下面几行★
  30.         String sql = "insert into t_user(username,address) values(?,?)";//★         PreparedStatement sta = con.prepareStatement(sql);
  31.         sta.setString(1, username);        sta.setString(2, address);
  32.                  int rows = sta.executeUpdate();
  33.         if(rows > 0) {             System.out.println("operate successfully!");
  34.         }         sta.close();
  35.         con.close();     }
  36.          /**
  37.      * jdbc对mysql的update操作      *
  38.      * @param address 地址      * @param id 主键值
  39.      * @throws Exception      */
  40.     public staticvoid update(String address,int id)throws Exception {         Class.forName("com.mysql.jdbc.Driver");
  41.         Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");        
  42.         //★注意下面几行代码         String sql  = "update t_user set address=? where id=?";
  43.         PreparedStatement sta = con.prepareStatement(sql);         sta.setString(1, address);
  44.         sta.setInt(2, id);        
  45.         int rows = sta.executeUpdate();        if(rows > 0) {
  46.             System.out.println("operate successfully");        }
  47.         sta.close();         con.close();
  48.              }
  49.          /**
  50.      * jdbc对mysql的删除操作      *
  51.      * @param id      * @throws Exception
  52.      */     public staticvoid delete(int id)throws Exception {
  53.         Class.forName("com.mysql.jdbc.Driver");        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
  54.                  //★注意点
  55.         String sql = "delete from t_user where id=?";        PreparedStatement sta = con.prepareStatement(sql);
  56.         sta.setInt(1, id);        
  57.         int rows = sta.executeUpdate();        if(rows > 0) {
  58.             System.out.println("operate successfully!!");        }
  59.         sta.close();         con.close();
  60.     }     
  61.     /**      * jdbc对mysql的retrieve操作
  62.      *      * @param id
  63.      * @throws Exception      */
  64.     public staticvoid retrieve(int id)throws Exception {         Class.forName("com.mysql.jdbc.Driver");
  65.         Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");        
  66.         //注意★         String sql = "select id,username,address from t_user where id=?";
  67.         PreparedStatement sta = con.prepareStatement(sql);         sta.setInt(1, id);
  68.                  ResultSet rs = sta.executeQuery();
  69.         //注意:当现实一条记录时:while可以换成if。★        if(rs.next()) {
  70.             int did = rs.getInt("id");            String username = rs.getString("username");
  71.             String address = rs.getString("address");            System.out.println(did + "\t" + username +"\t" + address);
  72.         }         
  73.         rs.close();         sta.close();
  74.         con.close();     }
  75.      }

热点排行