jdbc学习总结2---使用占位符的增删改查

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值