首页 > 数据库技术 > 详细

JDBC之增删改查的代码实现

时间:2020-01-21 22:58:16      阅读:33      评论:0      收藏:0      [点我收藏+]

标签:资源   auto   tco   span   n!   close   

package com.jdbc.demo2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Test;

/**
 * JDBC的CRUD的操作
 * @author lyt
 *
 */

public class JDBCDemo2 {
    
    @Test
    /**
     * 查询单条记录
     */
    public void demo5() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获得连接
            conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle");
            //创建SQL语句对象
            stmt = conn.createStatement();
            //编写SQL
            String sql = "select * from user";
            //执行SQL语句
            rs = stmt.executeQuery(sql);
            if(rs.next()) {
                System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password"));
            }
            
            
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
            if(stmt!=null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                stmt = null;
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
        }
        
    }
    
    @Test
    /**
     * 查询多条记录
     */
    public void demo4() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获得连接
            conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle");
            //创建SQL语句对象
            stmt = conn.createStatement();
            //编写SQL
            String sql = "select * from user";
            //执行SQL语句
            rs = stmt.executeQuery(sql);
            while(rs.next()) {
                System.out.println(rs.getInt("id")+" "+rs.getString("username")+" "+rs.getString("password"));
            }
            
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
            if(stmt!=null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                stmt = null;
            }
            if(rs!=null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
        }
        
    }
    
    @Test
    /**
     * 删除操作的代码实现
     */
    public void demo3() {
        Connection conn = null;
        Statement stmt = null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获得连接
            conn = DriverManager.getConnection("jdbc:mysql:///test","root","oracle");
            //创建SQL语句对象
            stmt = conn.createStatement();
            //编写SQL语句
            String sql = "delete from user where id=6";
            //执行SQL语句
            int num = stmt.executeUpdate(sql);
            if(num>0) {
                System.out.println("ID为6的用户删除成功");
            }
            
        }catch(Exception e){
            e.printStackTrace();
            
        }finally {
            if(conn!= null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(stmt!= null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
                
                
            }
            
        }
    
    
    @Test
    /**
     * 修改操作代码实现
     */
    public void demo2() {
        Connection conn= null;
        Statement stmt =null;
        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获得连接
            conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle");
            //执行操作
            //创建执行SQL语句的对象
            stmt = conn.createStatement();
            //编写SQL语句
            String sql = "update user set password=‘abc‘ where id=4";
            //执行SQL
            int num = stmt.executeUpdate(sql);
            if(num>0) {
                System.out.println("修改密码成功!!");
            }
        }catch(Exception e) {
            e.printStackTrace();
            
        }finally {
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn =null;
            }
            
            if(stmt!=null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn =null;
            }
            
        }
    }
    
    @Test
    /**
     * 保存操作的代码实现
     */
    public void demo1() {
        Connection conn = null;
        Statement stmt = null;
        try {
            //注册驱动;
            Class.forName("com.mysql.jdbc.Driver");
            //获得连接
            conn = DriverManager.getConnection("jdbc:mysql:///test", "root", "oracle");
            //创建执行SQL语句对象
            stmt = conn.createStatement();
            //编写SQL语句
            String sql = "insert into user values(null,‘eee‘,‘123‘,‘小米‘,21)";
            //执行SQL
            int num = stmt.executeUpdate(sql);
            if(num>0) {
                System.out.println("保存用户成功!!");
            }

        }catch(Exception e) {
            
            e.printStackTrace();
            
        }finally {
            //资源释放
            if(stmt!=null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                stmt = null;
            }
            
            if(conn!=null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn = null;
            }
            
            
            
        }
    }
    

}

技术分享图片

配置文件如上图

JDBC之增删改查的代码实现

标签:资源   auto   tco   span   n!   close   

原文:https://www.cnblogs.com/lytuser/p/12227226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号