当前位置:Gxlcms > 数据库问题 > java入门篇17 -- JDBC编程

java入门篇17 -- JDBC编程

时间:2021-07-01 10:21:17 帮助过:16人阅读

java.sql.*; public class HelloWorld { public static void main(String[] args) throws Exception { String JDBC_URl = "jdbs:mysql://127.0.0.1:3306/bbsoo"; String JDBC_USER = "root"; String JDBC_PASSWORD = "mysql"; // 查找数据 try (Connection conn = DriverManager.getConnection(JDBC_URl, JDBC_USER, JDBC_PASSWORD)) { // 方法一使用 Statement try (Statement st = conn.createStatement()) { // 执行语句 try (ResultSet rs = st.executeQuery("select user_id, username, user_type from bbs_users;")) { // 判断是否有下一个值 while (rs.next()) { int id = rs.getInt(1); String username = rs.getString(2); int user_type = rs.getInt(3); System.out.println("first: user :id" + id + " username:" + username + "user_type:" + user_type); } } } // 方式二 PreparedStatement try (PreparedStatement st = conn.prepareStatement("select ?, ?, ? from bbs_users;")) { // 在对应位置拼接我们的字符 st.setObject(1, "user_id"); st.setObject(2, "username"); st.setObject(3, "user_type"); try (ResultSet rs = st.executeQuery()) { while (rs.next()) { int id = rs.getInt(1); String username = rs.getString(2); int user_type = rs.getInt(3); System.out.println("second: user :id" + id + " username:" + username + "user_type:" + user_type); } } } } // 插入数据 try (Connection conn = DriverManager.getConnection(JDBC_URl, JDBC_USER, JDBC_PASSWORD)) { // 准备sql语句 try (PreparedStatement ps = conn.prepareStatement("INSERT INTO bbs_users (username, user_type) values (?, ?) ")) { // 填入对应索引位置的值 ps.setObject(1, "ming"); ps.setObject(2, 1); // 执行sql语句,返回值表示插入的记录数量 int n = ps.executeUpdate(); System.out.println(n); // 这个可以抓住插入的那条数据 try (ResultSet rs = ps.getGeneratedKeys()) { if (rs.next()) { int id = rs.getInt(1); // 获取id } } } } // 更新 try (Connection conn = DriverManager.getConnection(JDBC_URl, JDBC_USER, JDBC_PASSWORD)) { try (PreparedStatement ps = conn.prepareStatement("UPDATE bbs_users SET username=? where id =?")) { ps.setObject(1, "ning"); ps.setObject(2, 1); int n = ps.executeUpdate(); // 返回更新的条数 System.out.println(n); } } // 删除 try (Connection conn = DriverManager.getConnection(JDBC_URl, JDBC_USER, JDBC_PASSWORD)) { try (PreparedStatement ps = conn.prepareStatement("DELETE FROM bbs_users where id = ?")) { ps.setObject(1, 1); int n = ps.executeUpdate(); // 返回删除条数 System.out.println(n); } } // 事务 try (Connection conn = DriverManager.getConnection(JDBC_URl, JDBC_USER, JDBC_PASSWORD)) { try { conn.setAutoCommit(false); try (PreparedStatement ps = conn.prepareStatement("DELETE FROM bbs_users where id = ?")) { ps.setObject(1, 1); int n = ps.executeUpdate(); // 返回删除条数 System.out.println(n); } try (PreparedStatement ps = conn.prepareStatement("DELETE FROM bbs_users where id = ?")) { ps.setObject(1, 2); int n = ps.executeUpdate(); // 返回删除条数 System.out.println(n); } conn.commit(); } catch (SQLException e) { e.printStackTrace(); } finally { conn.setAutoCommit(true); conn.close(); } } // batch, 对于又重复操作,并且可以进行整理数据进行逐条执行,效率较低,可以使用batch try (Connection conn = DriverManager.getConnection(JDBC_URl, JDBC_USER, JDBC_PASSWORD)) { // 准备sql语句 try (PreparedStatement ps = conn.prepareStatement("INSERT INTO bbs_users (username, user_type) values (?, ?) ")) { // 填入对应索引位置的值 for (int i = 0; i < 10; i++) { ps.setObject(1, "ming" + i); ps.setObject(2, 1); ps.addBatch(); // 添加到batch中 } // 执行sql语句,返回的是每一条成功数量的数组 int[] n = ps.executeBatch(); for (int nn : n) { System.out.println(nn + "success"); } } } } }

我们都知道数据库的几大特性:原子性,一致性,隔离性,持久性,那么为了保证数据一致性,在进行多次操作数据库,或者这些数据修改会影响数据库一致性的情况下,一定要开启事务,这样最终失败,可以进行数据回滚,保证数据的一致性

java入门篇17 -- JDBC编程

标签:lse   失败   记录   username   trace   catch   exception   持久   rest   

人气教程排行