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

  1. package com.hanchao.jdbc;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. /**
  7. * jdbc学习总结二
  8. * @author hanlw
  9. * 2012-07-09
  10. */
  11. public class TestJdbcNew {
  12.     /**
  13.      * 上一篇文章,我们对JDBC有了初步的了解,并且知道了如何使用JDBC了。
  14.      * 但是,那只是JDBC的了解性操作实例。实际开发中,我们是不能那么玩的!!
  15.      *
  16.      * ★下面我们学习一下:JDBC的占位符的使用,以后写程序建议都要这么玩的!!
  17.      *
  18.      * 注意事项:我们的异常应该捕获;而不是抛出来啊!!
  19.      *
  20.      * SQLException是非运行时异常,必须要捕获或者向上抛出!!★
  21.      */
  22.     
  23.     public static void main(String[] args) throws Exception {
  24.         /**
  25.          * 1.jdbc对Mysql的insert操作
  26.          */
  27. //      insert("cherry","shanghai");
  28.         
  29.         /**
  30.          * 2.jdbc对mysql的update操作
  31.          */
  32. //      update("update",12);
  33.         
  34.         /**
  35.          * 3.jdbc对mysql的delete操作
  36.          */
  37. //      delete(12);
  38.         
  39.         /**
  40.          * 4.jdbc对mysql的retrieve 操作
  41.          */
  42.         retrieve(15);
  43.     }
  44.     
  45.     /**
  46.      * jdbc对mysql的insert操作
  47.      *
  48.      * @param username 用户名
  49.      * @param address 地址
  50.      */
  51.     public static void insert(String username,String address) throws Exception {
  52.         Class.forName("com.mysql.jdbc.Driver");
  53.         Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
  54.         
  55.         //注意下面几行★
  56.         String sql = "insert into t_user(username,address) values(?,?)"; //★
  57.         PreparedStatement sta = con.prepareStatement(sql);
  58.         sta.setString(1, username);
  59.         sta.setString(2, address);
  60.         
  61.         int rows = sta.executeUpdate();
  62.         if(rows > 0) {
  63.             System.out.println("operate successfully!");
  64.         }
  65.         sta.close();
  66.         con.close();
  67.     }
  68.     
  69.     /**
  70.      * jdbc对mysql的update操作
  71.      *
  72.      * @param address 地址
  73.      * @param id 主键值
  74.      * @throws Exception
  75.      */
  76.     public static void update(String address,int id) throws Exception {
  77.         Class.forName("com.mysql.jdbc.Driver");
  78.         Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
  79.         
  80.         //★注意下面几行代码
  81.         String sql  = "update t_user set address=? where id=?";
  82.         PreparedStatement sta = con.prepareStatement(sql);
  83.         sta.setString(1, address);
  84.         sta.setInt(2, id);
  85.         
  86.         int rows = sta.executeUpdate();
  87.         if(rows > 0) {
  88.             System.out.println("operate successfully");
  89.         }
  90.         sta.close();
  91.         con.close();
  92.         
  93.     }
  94.     
  95.     /**
  96.      * jdbc对mysql的删除操作
  97.      *
  98.      * @param id
  99.      * @throws Exception
  100.      */
  101.     public static void delete(int id) throws Exception {
  102.         Class.forName("com.mysql.jdbc.Driver");
  103.         Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
  104.         
  105.         //★注意点
  106.         String sql = "delete from t_user where id=?";
  107.         PreparedStatement sta = con.prepareStatement(sql);
  108.         sta.setInt(1, id);
  109.         
  110.         int rows = sta.executeUpdate();
  111.         if(rows > 0) {
  112.             System.out.println("operate successfully!!");
  113.         }
  114.         sta.close();
  115.         con.close();
  116.     }
  117.     
  118.     /**
  119.      * jdbc对mysql的retrieve操作
  120.      *
  121.      * @param id
  122.      * @throws Exception
  123.      */
  124.     public static void retrieve(int id) throws Exception {
  125.         Class.forName("com.mysql.jdbc.Driver");
  126.         Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");
  127.         
  128.         //注意★
  129.         String sql = "select id,username,address from t_user where id=?";
  130.         PreparedStatement sta = con.prepareStatement(sql);
  131.         sta.setInt(1, id);
  132.         
  133.         ResultSet rs = sta.executeQuery();
  134.         //注意:当现实一条记录时:while可以换成if。★
  135.         if(rs.next()) {
  136.             int did = rs.getInt("id");
  137.             String username = rs.getString("username");
  138.             String address = rs.getString("address");
  139.             System.out.println(did + "\t" + username + "\t" + address);
  140.         }
  141.         
  142.         rs.close();
  143.         sta.close();
  144.         con.close();
  145.     }
  146.     
  147. }
  • 8
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值