Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

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

$
0
0
  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. }
作者:communicate_ 发表于2013-1-10 0:36:13 原文链接
阅读:78 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>