zoukankan      html  css  js  c++  java
  • JDBC------之通过连接对象获得数据库操作对象(PreparedStatement)

    当我们获得数据库的连接对象之后,我们就可以通过数据库的连接对象获得数据库的操作对象,数据库的连接对象实际上就是一个Socket套接字,我们与数据库服务器的连接,可以通过连接想数据库服务器发送请求,并且获得响应。

    我们通过数据库连接对象可以获得三类数据库的操作对象:

     但是我们只讲解PrepareStatement操作对象。

    接下来我们通过使用PrepareStatement对象向数据库中插入一条数据的例子介绍PrepareStatement操作对象:

    首先我们可以编写一个JDBC的获取连接和关闭连接的工具类:

    代码如下:

    package com.itheima.hui.utils;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    
    public class JDBCUtils {
        //工具类的第一个功能:获取连接对象
        public static Connection getConnect() throws IOException, ClassNotFoundException, SQLException {
    
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
            Properties prop = new Properties();
            prop.load(is);
    
            String driverClass = (String) prop.get("driverClass");
            String url = (String) prop.get("url");
            String user = (String) prop.get("user");
            String password = (String) prop.get("password");
    
            Class.forName(driverClass);//加载驱动的同时就已经注册驱动了
    
            Connection connection = DriverManager.getConnection(url, user, password);
            return connection;
    
        }
    
    
        //工具类的第二个功能:关闭资源
    
        public static void close(Connection connection, Statement statement) {
    
    
            if (statement != null) {
    
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
            if (connection != null) {
    
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
    
    
    
        }
    
        public static void close(Connection connection, Statement statement,ResultSet rs) {
    
    
            if (rs != null) {
    
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement != null) {
    
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
            if (connection != null) {
    
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    
    
    
    
        }
    
    
    }

     我们通过工具类可以方便的获取数据库的连接对象,看下面的通过连接对象获取数据库操作对象的代码:

    package com.itheima.hui;
    
    import com.itheima.hui.utils.JDBCUtils;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    
    public class JDBCPreparedstatment {
        public static void main(String[] args) throws Exception {
            //使用工具类获得数据库连接对象
            Connection connection = null;
            PreparedStatement preparedStatement = null;
    
            connection = JDBCUtils.getConnect();
    
    
            String sql = "insert into student (name,age) values(?,?)";
            preparedStatement = connection.prepareStatement(sql);//获取预编译对象
            preparedStatement.setString(1, "huihui");
            preparedStatement.setString(2, "23");
            boolean execute = preparedStatement.execute();
            System.out.println(execute);
    
    
            //使用工具类关闭资源
            JDBCUtils.close(connection, preparedStatement);
    
    
        }
    
    
    }

     PrepareStatement是SQL语句预编译对象,当我们执行:preparedStatement = connection.prepareStatement(sql);这一行代码获取PrepareStatement对象的时候就会把传进去的sql语句进行编译并且加载进如内存中,在接下来的操作中我们只需要对sql语句中的占位符:?进行赋值,这样sql语句就不需要再次重新编译,这是与statement对象的最主要的差别。

    迎风少年
  • 相关阅读:
    Java实现 LeetCode 802 找到最终的安全状态 (DFS)
    Java实现 LeetCode 802 找到最终的安全状态 (DFS)
    Java实现 LeetCode 802 找到最终的安全状态 (DFS)
    Java实现 LeetCode 804 唯一摩尔斯密码词 (暴力)
    Java实现 LeetCode 803 打砖块 (DFS)
    Java实现 LeetCode 804 唯一摩尔斯密码词 (暴力)
    Java实现 LeetCode 803 打砖块 (DFS)
    Java实现 LeetCode 804 唯一摩尔斯密码词 (暴力)
    英文标点
    post sharp 与log4net 结合使用,含执行源码 转拷
  • 原文地址:https://www.cnblogs.com/ZYH-coder0927/p/13837257.html
Copyright © 2011-2022 走看看