首页 > 其他分享 >JDBC

JDBC

时间:2022-11-15 17:01:39浏览次数:28  
标签:ps JDBC try SQL properties conn

最常见SQL数据类型

注册驱动器

大多数jdbc的jar包都可以自动注册
手动注册的方式:注册mysql 8.0+
Class.forName("com.mysql.cj.jdbc.Driver")

连接数据库

将数据库连接值存放在database.properties,放在resources目录下

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/imooc?useUnicode=true&characterEncoding=utf-8
username=root
password=123456
  Properties properties = new Properties();
  properties.load(Test1.class.getResourceAsStream("/database.properties"));
  String url = properties.getProperty("url");
  String username = properties.getProperty("username");
  String password = properties.getProperty("password");
  try (Connection connection = DriverManager.getConnection(url, username, password)) {
      System.out.println(connection);
  }

Statement对象


ResultSet对象

管理连接、语句和结果集

SQL异常


PreparedStatement对象

防止SQL注入,但凡sql语句中有变量的都要用preparedstatement

读写LOB数据

  • 读取
  • 写入


多结果集

获取自动生成的键

  • 使用preparedStatement
try (Connection conn = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD)) {
    try (PreparedStatement ps = conn.prepareStatement(
            "INSERT INTO students (grade, name, gender) VALUES (?,?,?)",
            Statement.RETURN_GENERATED_KEYS)) {
        ps.setObject(1, 1); // grade
        ps.setObject(2, "Bob"); // name
        ps.setObject(3, "M"); // gender
        int n = ps.executeUpdate(); // 1
        try (ResultSet rs = ps.getGeneratedKeys()) {
            if (rs.next()) {
                long id = rs.getLong(1); // 注意:索引从1开始
            }
        }
    }
}
  • 使用statement

可滚动和可更新的结果集



行集RowSet



元数据

事务transaction

Connection conn = openConnection();
try {
    // 关闭自动提交:
    conn.setAutoCommit(false);
    // 执行多条SQL语句:
    insert(); update(); delete();
    // 提交事务:
    conn.commit();
} catch (SQLException e) {
    // 回滚事务:
    conn.rollback();
} finally {
    conn.setAutoCommit(true);
    conn.close();
}

批量更新

  • preparedStatement
try (PreparedStatement ps = conn.prepareStatement("INSERT INTO students (name, gender, grade, score) VALUES (?, ?, ?, ?)")) {
    // 对同一个PreparedStatement反复设置参数并调用addBatch():
    for (Student s : students) {
        ps.setString(1, s.name);
        ps.setBoolean(2, s.gender);
        ps.setInt(3, s.grade);
        ps.setInt(4, s.score);
        ps.addBatch(); // 添加到batch
    }
    // 执行batch:
    int[] ns = ps.executeBatch();
    for (int n : ns) {
        System.out.println(n + " inserted."); // batch中每个SQL执行的结果数量
    }
}
  • Statement

高级SQL类型

数据源 JNDI

标签:ps,JDBC,try,SQL,properties,conn
From: https://www.cnblogs.com/studyhaha/p/16891727.html

相关文章

  • ORA-01688:生产环境表空间不足导致数据库jdbc连接失败
    ORA-01688:unabletoextendtable,这个错误表明表空间使用率满,查看提示的表对应利用率;设置自动扩展或者增大容量。(1)查看日志提示的表名selecttablespace_name,file_i......
  • Create 1select+jdbc+jsp
    <formaction="UserServlet"method="get"> 查询条件:<inputtype="text"placeholder="输入用户名模糊查询"name="userName"> <inputtype="submit"value="搜索"> <in......
  • 【JDBC】使用PreparedStatement操作数据库
    1.对数据库调用的不同方式java.sql包下有3个接口Statement:用于执行静态SQL语句。PreparedStatement:SQL语句被预编译并存储在此对象中。CallableStatement:用于执行SQL......
  • JDBC(一)
    1、简介Java数据库连接,(JavaDatabaseConnectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。我......
  • JDBC_2_相关api说明
                                      ......
  • MySql - 基础学习 - JDBC
    一.为什么要学习JDBCSUM公司为了简化开发人员的操作(对数据库的统一),提供了一个规范(Java操作数据库的规范),俗称:JDBC这些规范的实现是由厂商们去做~对于开发人员来说,我们只......
  • jdbc连接Oracle数据库
    importjava.sql.*;publicclassMain{publicstaticvoidmain(String[]args){System.out.println("Helloworld!");Connectionconnection......
  • JDBCTemplate-快速入门和执行DML语句
    Spring框架对DBC的简单封装。提供了一个DBCTemplate对象简化JDBC的开发步骤:1、导入jar包2、创建jdbcTemplate对象。依赖于数据源Datasource jdbcTemplatetemplate......
  • JDBCTemplate-介绍
    spring框架对JDBC的简单封装提供了一个JDBCTemplate对象简化JDBC的开发步骤1.导入依赖<dependency><groupId>org.springframework</groupId>......
  • 多数据源配置JdbcTemplate(十五)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了SpringBoot整合MyBatisPlus(十四),如果没有看过,​​请观看上一章​​工作中,在业务的发展......