网站建设推广新业务,网站建设有哪种方式,小内存 wordpress 优化,工信部官网备案查询系统JDBC基本操作 create table user( id int primary key auto_increment, name varchar(50) ) ENGINE InnoDB DEFAULT CHARSET utf8; JDBC概念 JDBC是一个独立于特定数据库管理系统、通用的SQL数据库存取和操作的公共接口#xff0c;定义了用来访问数据库的标准的Jav… JDBC基本操作 create table user( id int primary key auto_increment, name varchar(50) ) ENGINE InnoDB DEFAULT CHARSET utf8; JDBC概念 JDBC是一个独立于特定数据库管理系统、通用的SQL数据库存取和操作的公共接口定义了用来访问数据库的标准的Java类库 连接步骤 加载驱动 进行数据库连接 // 驱动private static final String DRIVER com.mysql.jdbc.Driver;// 地址private static final String URL jdbc:mysql://localhost:3306/test;//用户名private static final String USER_NAME root;// 密码private static final String PSW 123456;/** * 获取连接 */public static Connection getConnection(){ Connection conn null; try { // 加载驱动 Class.forName(DRIVER); // 数据库连接 conn DriverManager.getConnection(URL,USER_NAME,PSW); } catch (ClassNotFoundException e) { System.out.println(加载驱动失败请检查是否引入Jar包或者驱动名称是否正确); throw new RuntimeException(加载驱动失败请检查是否引入Jar包或者驱动名称是否正确,e); } catch (SQLException throwables) { System.out.println(连接数据库失败请检查数据库地址用户名密码是否正确); throw new RuntimeException(连接数据库失败请检查数据库地址用户名密码是否正确,throwables); } return conn;}/*** 关闭连接* param conn*/public static void close(Connection conn){ if(conn ! null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } }} 注意为什么需要使用Class.forName()来加载数据库驱动 是因为在每个Driver中都包含有一个静态代码块实际调用的是DriverManager.registerDriver(new Driver());方法 public class Driver extends NonRegisteringDriver implements java.sql.Driver { public Driver() throws SQLException { } static { try { DriverManager.registerDriver(new Driver()); } catch (SQLException var1) { throw new RuntimeException(Cant register driver!); } }} DriverManager 该类进行数据库驱动的管理可以注册多个数据库驱动根据url来动态的选择不同的数据库连接。 操作数据库 Statement接口 使用Statement接口来操作静态的SQL语句 executeUpdate方法 添加 /*** 插入操作* param sql*/public static void doInsert(String sql){ Connection conn getConnection(); Statement statement null; try { statement conn.createStatement(); int result statement.executeUpdate(sql); System.out.println(sql执行成功插入result条数据); } catch (SQLException e) { throw new RuntimeException(执行失败,e); } finally { if(statement ! null){ try { statement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } close(conn); }} 修改 /** * 修改操作 * param sql */ public static void doUpdate(String sql){ Connection conn getConnection(); Statement statement null; try { statement conn.createStatement(); int result statement.executeUpdate(sql); System.out.println(sql执行成功修改result条数据); } catch (SQLException e) { throw new RuntimeException(执行失败,e); } finally { if(statement ! null){ try { statement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } close(conn); } } 删除 /** * 删除操作 * param sql */ public static void doDelete(String sql){ Connection conn getConnection(); Statement statement null; try { statement conn.createStatement(); int result statement.executeUpdate(sql); System.out.println(sql执行成功删除result条数据); } catch (SQLException e) { throw new RuntimeException(执行失败,e); } finally { if(statement ! null){ try { statement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } close(conn); } } PreparedStatement接口 该接口为Statement的子接口属于预处理操作可以传入带有占位符的SQL然后再进行补充占位符索引值从1开始。 可以有效地禁止SQL注入 executeUpdate方法 插入 public static void doPreparedInsert(String name){ Connection conn getConnection(); PreparedStatement statement null; try { String sql insert into user (name) values (?); statement conn.prepareStatement(sql); statement.setString(1,name); int result statement.executeUpdate(); System.out.println(sql执行成功插入result条数据); } catch (SQLException e) { throw new RuntimeException(执行失败,e); } finally { if(statement ! null){ try { statement.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } close(conn); }} 在创建preparedStatement对象时有一个重载方法 // 第二个参数表示一个是否返回自增主键的一个biaoshi// Statement.RETURN_GENERATED_KEYS// Statement.NO_GENERATED_KEYSPreparedStatement prepareStatement(String sql, int autoGeneratedKeys) 在使用该PreparedStatement执行插入操作时可以使用statement.getGeneratedKeys()来返回一个新生成主键的ResultSet对象结果集中只有一列GENERATED_KEY存放的新生成的主键值 更新 与插入类似 删除 与插入类似 结果集 在查询数据时返回的是一个二维的结果集使用ResultSet来遍历结果集 public static void doQuery(){ String sql select * from user; Connection conn getConnection(); PreparedStatement statement null; ResultSet resultSet null; try { statement conn.prepareStatement(sql); resultSet statement.executeQuery(); // resultSet.next 方法 将光标向前移动一行最初为第一行之前第一次调用使得第一行为当前行 while (resultSet.next()){ int id resultSet.getInt(id); String name resultSet.getString(name); System.out.println(查询到id为id,name为name的记录); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { if(resultSet ! null){ try { resultSet.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } close(conn,statement); }} 批量操作 public static void doBatchInsert(String sql){ Connection conn getConnection(); PreparedStatement statement null; try { statement conn.prepareStatement(sql); for(int i 0;i1000;i){ statement.setString(1,张三i); // 积攒sql statement.addBatch(); } // 执行sql statement.executeBatch(); // 清除积攒的sql statement.clearBatch(); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { close(conn,statement); }} https://zhhll.icu/2020/java基础/JDBC/1.JDBC基本操作/ 本文由 mdnice 多平台发布