使用Statement接口实现增、删、改操作

一、Statement接口

  Statement 是 Java 执行数据库操作的一个重要接口,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。Statement对象,用于执行不带参数的简单SQL语句。

  int executeUpdate(String sql)  执行给定的SQL语句,该语句可能为INSERT,UPDATE或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQLDDL 语句)。

  voidclose() 立即释放此 Statement 对象的数据库和 JDBC 资源,而不是等待该对象自动关闭时发生此操作。

      连接与加载数据库

        // 数据库地址
    private static String dbUrl = "jdbc:mysql://localhost:3306/db_bank";
    // 用户名
    private static String dbUserName = "root";
    // 密码
    private static String dbPassvord = "3306";
    // 驱动名称
    private static String jdbcName = "com.mysql.jdbc.Driver";
    /**
     * 获取数据库连接    
     * @return
     * @throws Exception
     */
    public Connection getCon() throws Exception {
        Class.forName(jdbcName);
        Connection con=DriverManager.getConnection(dbUrl,dbUserName, dbPassvord);
        return con;
        
    }    

连接关闭:

/**
     * 关闭连接
     * @param con
     * @throws Exception
     */
    public void close(Statement stmt,Connection con) throws Exception {
        if (stmt!=null) {
            stmt.close();
            if (con!=null) {
                con.close();
            }
        }
    }

 

1、实现添加数据操作

在另一个类中调用方法,获取连接并插入数据

public static void main(String[] args) throws Exception {
        DbUtil dbUtil=new DbUtil();
        String sql="insert into t_book values(null,'java1','哥',888,1)";
        //获取数据库连接
        Connection con=dbUtil.getCon();
        //获取Statement
        Statement stmt=con.createStatement();
        int result=stmt.executeUpdate(sql);
        System.out.println("操作的结果:"+result+"数据");
        //关闭statement
        stmt.close();
        //关闭连接
        con.close();
    }

查看可发现数据插入成功。

2、实现更新数据操作

/**
     * 更新数据
     * @param book
     * @return
     * @throws Exception
     */
    private static int updateBook(Book book) throws Exception{
        Connection con=dbUtil.getCon();//获取连接
        String sql = "update t_book set bookName='" + book.getBookName() + "',author='" + book.getAuthor() + "',price="
                + book.getPrice() + ",bookTypeId=" + book.getBookTypeId() + " where id="+book.getId();
        Statement stmt= con.createStatement();//创建statement
        int result=stmt.executeUpdate(sql);
        dbUtil.close(stmt, con);
        return result;        
    }
    public static void main(String[] args) throws Exception {
        Book book=new Book(3,"java牛牛2","牛哥",1212,1);
        int result=updateBook(book);
        if (result==1) {
            System.out.println("更新成功!");    
        }else {
            System.out.println("更新失败!");
            }
    }

 

3、实现删除数据操作

private static int deleteBook(int id)throws Exception{
        Connection con=dbUtil.getCon();//获取连接
        String sql ="delete from t_book where id="+id;
        Statement stmt= con.createStatement();//创建statement
        int result=stmt.executeUpdate(sql);
        dbUtil.close(stmt, con);
        return result;
    }
    public static void main(String[] args) throws Exception {
        int result=deleteBook(3);
        if (result==1) {
            System.out.println("删除成功!");    
        }else {
            System.out.println("删除失败!");
        }
    }

 

posted on 2018-01-20 10:52  超群&Q  阅读(329)  评论(0编辑  收藏  举报