首页 > 其他分享 >JDBC

JDBC

时间:2024-08-06 19:24:57浏览次数:9  
标签:语句 JDBC String static Statement sql properties

一、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. 按照步骤编写接下来的代码

标签:语句,JDBC,String,static,Statement,sql,properties
From: https://blog.csdn.net/ckx0703/article/details/140918529

相关文章

  • MySQL JDBC连接参数rewriteBatchedStatements(转)
    原文:https://blog.51cto.com/u_16213583/9701812MySQLJdbc驱动在默认情况下会无视executeBatch()语句,把我们期望批量执行的一组sql语句拆散,一条一条地发给MySQL数据库,直接造成较低的性能。只有把rewriteBatchedStatements参数置为true,驱动才会帮你批量执行。不过,驱动具体是怎......
  • 【YashanDB数据库】statement级别的触发器在jdbc接口调用executeBatch时被多次触发
    问题现象某客户使用jdbc接口向yashandb的表A插入数据。表A上有一个语句级触发器,其内容为在触发时执行altersequence操作;另外还有一个insert时的行级触发器,其内容为将每行的部分列赋给新值,这些新值要么来自sequence.nextval,要么来自于current_timestamp。客户向表A插入90万条数......
  • 实战:深入了解JDBC和分享JDBCUtil
    Java数据库连接(JDBC)是一个功能强大的API,它弥补了Java应用程序与关系数据库之间的差距。通过利用JDBC,您可以无缝地与数据库交互以存储、检索和操作数据。但是,要有效使用JDBC,需要遵循最佳实践,以确保代码的最佳性能、安全性和可维护性。正如我之前提到的观点,学习一个新......
  • JDBC实现多数据库切换
    一、编译环境JDK:1.8IDEA:2023.1.2二、maven依赖<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.28</version></dependency><!--......
  • PGjdbc源码试读(二)
    本期目标追踪Connection.createStatement()Statement.executeUpdate(Stringsql)追踪Connection.createStatement()在PgConnection中找到createStatement()方法:该方法调用了同名方法,并传递了两个参数,查询两个常量的注释:TYPE_FORWARD_ONLY表示返回的集合ResultSet只......
  • PGjdbc源码试读(一)
    目标通过追踪常用的jdbc方法去熟悉PGjdbc的执行流程常见jdbc使用流程Class.forName("org.postgresql.Driver");ConnectionconnectionPG=DriverManager.getConnection("jdbc:postgresql://localhost:5432/xxx","xxxxxx","xxxxxx");Statementstatement......
  • JDBC(重点)
    JDBC(重点)数据库驱动JDBCSUN公司为了简化开发人员的(对数据库的同一)操作,提供了一个(Java操作数据库的)规范,俗称JDBC这些规范的实现由具体的厂商去做对于开发人员来说,我们只需要掌握JDBC接口的操作即可java.sqljavax.sql还需要导入一个数据库驱动包第一个JDBC程序创建普......
  • jdbc
    1.添加依赖(因为创建的是maven项目所以不用去创建lib目录去导入包了方便)点击查看代码<dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47&l......
  • JdbcTemplate
    JdbcTemplate是Spring框架提供的一个用于简化JDBC操作的类。它处理了资源的创建和释放,使得开发者能够更专注于SQL语句本身和结果的处理。JdbcTemplate提供了大量的方法,用于执行各种类型的SQL语句,包括查询、更新、批处理、调用存储过程等。导入jar包<depende......
  • sharding-jdbc 兼容 MybatisPlus的动态数据源
    背景:之前的项目做读写分离的时候用的MybatisPlus的动态数据做的,很多地方使用的@DS直接指定的读库或者写库实现的业务;随着表数据量越来越大,现在打算把比较大的表进行水平拆分,准备使用ShardingJDBC实现,但是发现两者配合起来并不是那么顺利,网上大部分文章都是直接把整个Sharding的......