JDBC补充 statement对象 CRUD增删改查

JDBC补充 statement对象 CRUD增删改查

statement对象

Jdbc中的statement对象用于向数据库发送SQL语句,想完成对数据库的增删改查,只需要通过这个对象向数据库发送增删改
查语句即可。

Statement对象的executeUpdate方法,用于向数据库发送增、删、改的sq|语句, executeUpdate执行完后,将会返回一个整
数(即增删改语句导致了数据库几行数据发生了变化)。
Statement.executeQuery方法用于向数据库发送查询语句,executeQuery方法返回代表查询结果的ResultSet对象。

CRUD操作-create增加

使用executeUpdate(String sq)方法完成数据添加操作,示例操作:

Statement st = conn.createstatement();
String sq1 = "insert into uer(...) values(....) ";
int num = st.executeupdate(sq1);
if (num>0){
System.out.print1n("插入成功!!!");
}

CRUD操作-delete

使用executeUpdate(String sq|)方法完成数据删除操作,示例操作:

Statement st = conn.CreateStatement();
String sq1 = "delete from user where id=1";
int num = st.executeupdate(sq1) ;
if (num>0){
System.out. print1n(“删除成功! ! ! ");
}

CRUD操作-update

使用executeUpdate(String sq|)方法完成数据修改操作,示例操作:

Statement st = conn.createStatement () ;
string sq1 = "update user set name='' where name='";
int num = st.executeupdate(sq1);
if(num>0){
	System.out.print1n(“修改成功! ! ! ");
}

CRUD操作-Retrieve 检索

使用executeQuery(String sq|)方法完成数据查询操作,示例操作:

Statement st = conn.createStatement();
string sq1 = "select * fcom user where id=1";
Resultset rs = st.executeupdate(sq1);
while(rs.next(){
//根据 获取列的数据类型,分别调用rs的相应方法映射到java对象中
}

代码实现

1、提取工具类

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class jdbcUtils {

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    /*
     * 文件读取,只会执行一次,使用静态代码块
     */
    static {
        try {
            /*
             获取到db.properties文件的输入流
            1 InputStream in = MyJdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            2 InputStream in = MyJdbcUtils.class.getResourceAsStream("db.properties");
            注意:
                第一行注释的读取的配置文件db.properties应该在resources目录下
                第二行读取的配置文件db.properties应该和MyJdbcUtils在同一个package下
             */

            InputStream in = jdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(in);

            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");

            // 驱动只用加载一次
            Class.forName(driver);

        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //获取链接
    public static Connection getConnection() throws SQLException {
        Connection conn = DriverManager.getConnection(url, username, password);
        return conn;
    }

    //释放资源
    public static void release(Connection conn, Statement st, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}

2、编写增删改的方法 'executeUpdate' (执行更新)

举例 create增加

public class TestInsert {
    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        //获取连接
        try {
            conn = jdbcUtils.getConnection();    //获取数据库连接
            st   = conn.createStatement();               //获得SQL的执行对象
						//增删改操作只需修改此处sql语句
            String sql = "INSERT INTO users (`id`,`name`,`password`,`email`,`birthday`)" +
                    "VALUES(4,'LANTIAN','123456','24736743@qq.com','2020-01-01')";
            int in = st.executeUpdate(sql);
            if (in>0){
                System.out.println("插入成功!");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbcUtils.release(conn,st,rs);
        }
    }
}


3、查询 executeQuery (执行查询)

public class TestSelect {
    public static void main(String[] args) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            //获取连接
            conn = jdbcUtils.getConnection();
            //获取sql执行对象
            st = conn.createStatement();

        //    SQL
            String sql = "select * from users where id = 4";

            rs = st.executeQuery(sql);  //查询完毕会返回一个结果集

            while (rs.next()){
                System.out.println("id="+ rs.getObject("id"));
                System.out.println("name="+ rs.getObject("name"));
                System.out.println("pwd="+ rs.getObject("password"));
                System.out.println("email="+ rs.getObject("email"));
                System.out.println("birthday="+ rs.getObject("birthday"));
                System.out.println("=======================================================");
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbcUtils.release(conn,st,rs);//s
        }
    }
}
posted @ 2021-08-01 01:26  爱学习的蓝天  阅读(67)  评论(0)    收藏  举报