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


 
  
  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. publicclass TestJdbcNew {

  12. /**

  13.     * 上一篇文章,我们对JDBC有了初步的了解,并且知道了如何使用JDBC了。

  14.     * 但是,那只是JDBC的了解性操作实例。实际开发中,我们是不能那么玩的!!

  15.     *

  16.     * 下面我们学习一下:JDBC的占位符的使用,以后写程序建议都要这么玩的!!

  17.     *

  18.     * 注意事项:我们的异常应该捕获;而不是抛出来啊!!

  19.     *

  20.     * SQLException是非运行时异常,必须要捕获或者向上抛出!!

  21.     */

  22. publicstaticvoid main(String[] args) throws Exception {

  23. /**

  24.         * 1.jdbc对Mysql的insert操作

  25.         */

  26. //      insert("cherry","shanghai");

  27. /**

  28.         * 2.jdbc对mysql的update操作

  29.         */

  30. //      update("update",12);

  31. /**

  32.         * 3.jdbc对mysql的delete操作

  33.         */

  34. //      delete(12);

  35. /**

  36.         * 4.jdbc对mysql的retrieve 操作

  37.         */

  38.        retrieve(15);

  39.    }

  40. /**

  41.     * jdbc对mysql的insert操作

  42.     *

  43.     * @param username 用户名

  44.     * @param address 地址

  45.     */

  46. publicstaticvoid insert(String username,String address) throws Exception {

  47.        Class.forName("com.mysql.jdbc.Driver");

  48.        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");

  49. //注意下面几行

  50.        String sql = "insert into t_user(username,address) values(?,?)"; //

  51.        PreparedStatement sta = con.prepareStatement(sql);

  52.        sta.setString(1, username);

  53.        sta.setString(2, address);

  54. int rows = sta.executeUpdate();

  55. if(rows > 0) {

  56.            System.out.println("operate successfully!");

  57.        }

  58.        sta.close();

  59.        con.close();

  60.    }

  61. /**

  62.     * jdbc对mysql的update操作

  63.     *

  64.     * @param address 地址

  65.     * @param id 主键值

  66.     * @throws Exception

  67.     */

  68. publicstaticvoid update(String address,int id) throws Exception {

  69.        Class.forName("com.mysql.jdbc.Driver");

  70.        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");

  71. //注意下面几行代码

  72.        String sql  = "update t_user set address=? where id=?";

  73.        PreparedStatement sta = con.prepareStatement(sql);

  74.        sta.setString(1, address);

  75.        sta.setInt(2, id);

  76. int rows = sta.executeUpdate();

  77. if(rows > 0) {

  78.            System.out.println("operate successfully");

  79.        }

  80.        sta.close();

  81.        con.close();

  82.    }

  83. /**

  84.     * jdbc对mysql的删除操作

  85.     *

  86.     * @param id

  87.     * @throws Exception

  88.     */

  89. publicstaticvoid delete(int id) throws Exception {

  90.        Class.forName("com.mysql.jdbc.Driver");

  91.        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");

  92. //注意点

  93.        String sql = "delete from t_user where id=?";

  94.        PreparedStatement sta = con.prepareStatement(sql);

  95.        sta.setInt(1, id);

  96. int rows = sta.executeUpdate();

  97. if(rows > 0) {

  98.            System.out.println("operate successfully!!");

  99.        }

  100.        sta.close();

  101.        con.close();

  102.    }

  103. /**

  104.     * jdbc对mysql的retrieve操作

  105.     *

  106.     * @param id

  107.     * @throws Exception

  108.     */

  109. publicstaticvoid retrieve(int id) throws Exception {

  110.        Class.forName("com.mysql.jdbc.Driver");

  111.        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root");

  112. //注意

  113.        String sql = "select id,username,address from t_user where id=?";

  114.        PreparedStatement sta = con.prepareStatement(sql);

  115.        sta.setInt(1, id);

  116.        ResultSet rs = sta.executeQuery();

  117. //注意:当现实一条记录时:while可以换成if。

  118. if(rs.next()) {

  119. int did = rs.getInt("id");

  120.            String username = rs.getString("username");

  121.            String address = rs.getString("address");

  122.            System.out.println(did + "\t" + username + "\t" + address);

  123.        }

  124.        rs.close();

  125.        sta.close();

  126.        con.close();

  127.    }

  128. }

下一篇文章,我们将学习javaBean+DAO,这是开发的基础。上面两篇学习总结是基础的基础。要理解,熟练掌握!


看下下面这篇文章也不错!

JDBC为什么要使用PreparedStatement而不是Statement 





     本文转自韩立伟 51CTO博客,原文链接:http://blog.51cto.com/hanchaohan/925308,如需转载请自行联系原作者



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值