码迷,mamicode.com
首页 > 数据库 > 详细

JDBC之增删改查的代码实现

时间:2020-01-21 23:26:08      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:增删改   use   java   sql   src   sql语句   query   行操作   drive   

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之增删改查的代码实现

标签:增删改   use   java   sql   src   sql语句   query   行操作   drive   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!