statement对象

简介: statement对象

1、新建一个 lesson02 的包

2、在src目录下创建一个db.properties文件

20200805133236206.png


3、在lesson02 下新建一个 utils 包,新建一个类 JdbcUtils

public class JdbcUtils {
    private static String driver=null;
    private static String url=null;
    private static String user=null;
    private static String password=null;
    static {
        try {
            //读取db.properties文件中的数据库连接信息
            InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(in);
            //获取数据库连接驱动
            driver= properties.getProperty("driver");
            //获取数据库连接url
            url = properties.getProperty("url");
            //获取数据库用户名
            user = properties.getProperty("user");
            //获取数据库密码
            password = properties.getProperty("password");
            //加载数据库驱动
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取数据库连接对象
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url,user,password);
    }
    //释放资源,要释放的资源包括Connection数据库连接对象,负责执行SQL命令的Statement 对象,存储查询结果的ResultSet对象
    public static void release(Connection co, Statement st,ResultSet re){
        if(re!=null){
            try {
                re.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            re=null;
        }
        if(st!=null){
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(co!=null){
            try {
                co.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}


使用statement对象完成对数据库的CRUD操作

public class TestInsert {
    public static void main(String[] args) {
        Connection conn=null;
        Statement st = null;
        ResultSet re = null;
        try {
            conn= JdbcUtils.getConnection();
            st = conn.createStatement();
            String sql = "insert into user(id,name,password)" +
                    "values(1,zangzang,123)";
            int i = st.executeUpdate("sql ");
            if(i>0){
                System.out.println("插入成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //sql执行完成后释放相关资源
            JdbcUtils.release(conn,st,re);
        }
    }
}
相关文章
|
7月前
|
SQL 存储 Java
JDBC Statement:执行 SQL 语句的重要接口
在Java应用程序中,与数据库进行交互是一项常见的任务。为了执行数据库操作,我们需要使用JDBC(Java Database Connectivity)来建立与数据库的连接并执行SQL语句。Statement接口是JDBC中的一个重要接口,它用于执行SQL语句并与数据库进行交互。本文将详细介绍Statement接口的使用,包括如何创建Statement对象、执行SQL语句、处理结果等内容。
111 0
|
1月前
|
SQL 关系型数据库 MySQL
java.sql.SQLException: No operations allowed after statement closed.
java.sql.SQLException: No operations allowed after statement closed.
42 0
|
7月前
|
SQL 数据库
解决Can not issue executeUpdate() or executeUpdate() with statement that produce result sets问题~
解决Can not issue executeUpdate() or executeUpdate() with statement that produce result sets问题~
128 0
|
8月前
|
SQL Java
[已解决]SpringDataJPA+Hibernate在执行executeUpdate()的时候报错 Executing an update/delete query
[已解决]SpringDataJPA+Hibernate在执行executeUpdate()的时候报错 Executing an update/delete query
|
11月前
|
SQL 存储 数据库
|
12月前
|
SQL 缓存 Java
数据库连接关闭工具类、Statement介绍、PreparedStatement介绍及区别
数据库连接关闭工具类、Statement介绍、PreparedStatement介绍及区别
94 0
|
SQL Java 数据库连接
statement对象
statement对象
使用CL_RS_WHERE创建dynamic SQL statement
使用CL_RS_WHERE创建dynamic SQL statement
使用CL_RS_WHERE创建dynamic SQL statement
|
区块链
从FrameworkElement对象创建Cursor对象
原文:从FrameworkElement对象创建Cursor对象   理论上可以从任何派生自 Visual的对象创建光标对象,但是由于 FrameworkElement拥有 ActualWidth和 ActualHeight属性,这就省去了指定光标大小的麻烦。
780 0
|
数据库
[Mybatis-Plus] 调用自带方法 报错 Invalid bound statement
在调用Mybatis-Plus(版本:2.1-gamma)的自带方法 selectById 时,报错 Invalid bound statement: PlaceType hasPlaceType = placeTypeMapper.
5432 0