用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

使用JDBC执行 select insert update delete

2014-08-06 作者: jun举报

[java]代码库

package demo.mysql;
 
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
import org.junit.Test;
 
import demo.utils.JDBCUtils;
 
/*
 使用JDBC执行 select insert update delete
 */
public class Demo1 {
 
    @Test
    public void testSelect() throws Exception{
        //注册驱动(数据库的类型)
        /*
         * 这种方式存在的问题:
         * 1. 依赖于jar包
         * 2. 导致驱动com.mysql.jdbc.Driver注册两次
         */
        //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
        //实际开发,应该采用下面的方式注册驱动  --> Java反射
        Class.forName("com.mysql.jdbc.Driver");
 
        //获取连接
        String url = "jdbc:mysql://localhost:3306/jdbc";
        String user = "root";
        String password = "password";
        Connection conn = DriverManager.getConnection(url, user, password);
 
        //得到运行环境
        Statement st = conn.createStatement();
 
        //执行SQL
        String sql = "select * from myuser";
        ResultSet rs = st.executeQuery(sql);
 
        //处理结果
        while(rs.next()){
            //每次循环,都取出一条记录
            int userID = rs.getInt("userID");
            String name = rs.getString("userName");
            String userPass = rs.getString("userPass");
            String email = rs.getString("email");
            Date birthday = rs.getDate("birthday");
             
            System.out.println(userID);
            System.out.println(name);
            System.out.println(userPass);
            System.out.println(email);
            System.out.println(birthday);
            System.out.println("*********************");
        }
 
        //释放资源
        rs.close();
        st.close();
        conn.close();
    }
 
    @Test
    public void testInsert(){
        String url = "jdbc:mysql://localhost:3306/jdbc";
        String user = "root";
        String password = "password";
         
        Connection conn = null;
        Statement st = null;
         
        try{
            //注册驱动(数据库的类型)
            Class.forName("com.mysql.jdbc.Driver");
     
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
             
            //得到运行环境
            st = conn.createStatement();
     
            //执行SQL
            String sql = "insert into myuser(userID,userName) values("+5+",'Tom'"+")";
            System.out.println("sql="+sql);
            int result = st.executeUpdate(sql);
     
            //处理结果
            if(result>0){
                System.out.println("操作成功");
            }else{
                System.out.println("操作失败");
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            //释放资源
            if(st != null){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    st = null;//--> 让他迅速成为java gc的对象
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    conn = null;//--> 让他迅速成为java gc的对象
                }
            }
        }
 
    }
     
    @Test
    public void testUpate(){
        String sql = "update myuser set username='Tom_AB' where userID=4";
         
        Connection conn = null;
        Statement st = null;
        try {
            //从工具类中获取连接
            conn = JDBCUtils.getConnection();
            st = conn.createStatement();
            int result = st.executeUpdate(sql);
            //处理结果
            if(result>0){
                System.out.println("操作成功");
            }else{
                System.out.println("操作失败");
            }
             
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            //调用工具类释放资源
            JDBCUtils.release(conn, st, null);
        }
 
    }
     
    @Test
    public void testDelete(){
        String url = "jdbc:mysql://localhost:3306/jdbc";
        String user = "root";
        String password = "password";
         
        Connection conn = null;
        Statement st = null;
         
        try{
            //注册驱动(数据库的类型)
            Class.forName("com.mysql.jdbc.Driver");
     
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
             
            //得到运行环境
            st = conn.createStatement();
     
            //执行SQL
            String sql = "delete from myuser where userID=5";
            System.out.println("sql="+sql);
            int result = st.executeUpdate(sql);
     
            //处理结果
            if(result>0){
                System.out.println("操作成功");
            }else{
                System.out.println("操作失败");
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }finally{
            //释放资源
            if(st != null){
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    st = null;//--> 让他迅速成为java gc的对象
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    conn = null;//--> 让他迅速成为java gc的对象
                }
            }
        }
 
    }
}


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...