您的位置:首页 > 数据库 > MySQL

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

2013-03-24 14:41 393 查看
 
package com.hanchao.jdbc; 
 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
 
/** 
 * jdbc学习总结二 
 * @author hanlw 
 * 2012-07-09 
 */ 
public class TestJdbcNew { 
 
    /** 
     * 上一篇文章,我们对JDBC有了初步的了解,并且知道了如何使用JDBC了。 
     * 但是,那只是JDBC的了解性操作实例。实际开发中,我们是不能那么玩的!! 
     *  
     * ★下面我们学习一下:JDBC的占位符的使用,以后写程序建议都要这么玩的!!  
     *  
     * 注意事项:我们的异常应该捕获;而不是抛出来啊!! 
     *  
     * SQLException是非运行时异常,必须要捕获或者向上抛出!!★ 
     */ 
     
    public static void main(String[] args) throws Exception { 
        /** 
         * 1.jdbc对Mysql的insert操作 
         */ 
//      insert("cherry","shanghai"); 
         
        /** 
         * 2.jdbc对mysql的update操作 
         */ 
//      update("update",12); 
         
        /** 
         * 3.jdbc对mysql的delete操作 
         */ 
//      delete(12); 
         
        /** 
         * 4.jdbc对mysql的retrieve 操作 
         */ 
        retrieve(15); 
    } 
     
    /** 
     * jdbc对mysql的insert操作 
     *  
     * @param username 用户名 
     * @param address 地址 
     */ 
    public static void insert(String username,String address) throws Exception { 
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
         
        //注意下面几行★ 
        String sql = "insert into t_user(username,address) values(?,?)"; //★ 
        PreparedStatement sta = con.prepareStatement(sql); 
        sta.setString(1, username); 
        sta.setString(2, address); 
         
        int rows = sta.executeUpdate(); 
        if(rows > 0) { 
            System.out.println("operate successfully!"); 
        } 
        sta.close(); 
        con.close(); 
    } 
     
    /** 
     * jdbc对mysql的update操作 
     *  
     * @param address 地址 
     * @param id 主键值 
     * @throws Exception  
     */ 
    public static void update(String address,int id) throws Exception { 
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
         
        //★注意下面几行代码 
        String sql  = "update t_user set address=? where id=?"; 
        PreparedStatement sta = con.prepareStatement(sql); 
        sta.setString(1, address); 
        sta.setInt(2, id); 
         
        int rows = sta.executeUpdate(); 
        if(rows > 0) { 
            System.out.println("operate successfully"); 
        } 
        sta.close(); 
        con.close(); 
         
    } 
     
    /** 
     * jdbc对mysql的删除操作 
     *  
     * @param id 
     * @throws Exception  
     */ 
    public static void delete(int id) throws Exception { 
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
         
        //★注意点 
        String sql = "delete from t_user where id=?"; 
        PreparedStatement sta = con.prepareStatement(sql); 
        sta.setInt(1, id); 
         
        int rows = sta.executeUpdate(); 
        if(rows > 0) { 
            System.out.println("operate successfully!!"); 
        } 
        sta.close(); 
        con.close(); 
    } 
     
    /** 
     * jdbc对mysql的retrieve操作 
     *  
     * @param id 
     * @throws Exception 
     */ 
    public static void retrieve(int id) throws Exception { 
        Class.forName("com.mysql.jdbc.Driver"); 
        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydb","root","root"); 
         
        //注意★ 
        String sql = "select id,username,address from t_user where id=?"; 
        PreparedStatement sta = con.prepareStatement(sql); 
        sta.setInt(1, id); 
         
        ResultSet rs = sta.executeQuery(); 
        //注意:当现实一条记录时:while可以换成if。★ 
        if(rs.next()) { 
            int did = rs.getInt("id"); 
            String username = rs.getString("username"); 
            String address = rs.getString("address"); 
            System.out.println(did + "\t" + username + "\t" + address); 
        } 
         
        rs.close(); 
        sta.close(); 
        con.close(); 
    } 
     


来自:http://hanchaohan.blog.51cto.com/2996417/925308
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql