JDBC

一、JDBC开发步骤

1. 加载驱动
2. 获取连接对象
3. 写sql语句
4. 创建statement
5. 执行sql语句
6. 关闭连接 

二、JDBC接口核心的API


1. DriverManager类:驱动管理类,用于管理所有注册的驱动程序
    registerDriver(driver):注册驱动类对象
    Connection getConnection(url,user,password):获取连接对象
2. Connection 接口:
    Statement createStatement():创建Statement对象
    PreparedStatement PrepareStatement(String sql):创建PrepareStatement对象
    CallableStatement prepareCall(String sql):创建CallableStatement对象
3. 1Statement接口:用于执行静态的sql语句
    int executeUpdate(String sql):执行静态的更新sql语句
    ResultSet executeQuery(String sql):执行的静态的查询sql语句
3. 2PreparedStatement接口:用于执行预编译sql语句
    int executeUpdate():执行预编译的更新sql语句
    ResultSet executeQuery():执行预编译的查询sql语句
4. ResultSet接口:用户封装查询出来的数据
    boolean next():将光标移动到下一行
    getXX():获取列的值

三、PreparedStatement(预编译)和Statementment的区别


1. 语法不同:
PreparedStatement可以使用预编译的sql,只需要发送一次sql语句,后面只发参数即可,公用同一个sql语句
Statement只能使用静态的sql
2. 效率不同:
PreparesStatement使用了sql缓冲区,效率比Statement高
3. 安全性不同:
PreparedStatement可以有效的防止sql注入,而Statement不能防止sql注入

四、实例

1. 在项目根目录建立lib目录和resources目录

右键点击resources目录选择Mark Directory as下的Resources Root

2. 在resources目录下创建新Resource Bundle文件,名字为db.properties。

在其中输入如下代码

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/study?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8
username= **** //自己的数据库用户名
password= **** //自己的数据库密码

3. 下载mysql-connector,然后将其复制到lib目录下

通过百度网盘分享的文件:mysql-connector-j-8.0.31.jar
链接:https://pan.baidu.com/s/1GIjD6Of9iIRxR1bqpcUQZQ?pwd=nfw4 
提取码:nfw4

右键点击lib选择Add as Library

4. 因为加载驱动、获取连接对象和关闭连接所要写的内容较多,而且每次写的都是重复的代码,所以我们将其写到一个JDBCUtil类中,以后要用时直接将类文件复制过去即可。

public class JDBCUtil {
    static String driver;
    static String url;
    static String username;
    static String password;

    private JDBCUtil() {

    }

    static {
        ClassLoader classLoader = JDBCUtil.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("db.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
            username = properties.getProperty("username");
            password = properties.getProperty("password");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() throws SQLException {
        Connection connection = DriverManager.getConnection(url,username,password);
        return connection;
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}

5. 按照步骤编写接下来的代码

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值