1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
| public class TestCRUD extends TestApplication { @Test public void insert() { String sql = "insert into user(`birthday`,`update_time`,`create_time`,`name`,`status`,`country_id`) values(?,?,?,?,?,?)"; User user = new User(2, new Date(), new Date(), "or 1=1", 1, new Date(), 1002); Connection conn = DBUtil.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setDate(1, new java.sql.Date(user.getBirthday().getTime())); pst.setDate(2, new java.sql.Date(user.getUpdateTime().getTime())); pst.setDate(3, new java.sql.Date(user.getCreateTime().getTime())); pst.setString(4, user.getName()); pst.setInt(5, user.getStatus()); pst.setInt(6, user.getCountryId()); log.info("执行的SQL: " + pst.toString()); int res = pst.executeUpdate(); log.info("受影响的行数: " + res); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("创建 预执行语句 异常"); } finally { DBUtil.closeConnection(conn); } }
@Test public void update() { String sql = "update user set name=? where id=1"; Connection conn = DBUtil.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, "老男孩"); log.info("执行的SQL: " + pst.toString()); int res = pst.executeUpdate(); log.info("受影响的行数: " + res); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("创建 预执行语句 异常"); } finally { DBUtil.closeConnection(conn); } }
@Test public void delete() { String sql = "delete from user where name=?"; Connection conn = DBUtil.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); pst.setString(1, "老男孩"); log.info("执行的SQL: " + pst.toString()); int res = pst.executeUpdate(); log.info("受影响的行数: " + res); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("创建 预执行语句 异常"); } finally { DBUtil.closeConnection(conn); } }
@Test public void select() { String sql = "select * from user"; Connection conn = DBUtil.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql); ResultSet resultSet = pst.executeQuery(); log.info("执行的SQL: " + pst.toString()); while (resultSet.next()) { log.info("第" + resultSet.getRow() + "行数据: " + resultSet.getObject(1).toString() + " \t" + resultSet.getObject(2).toString() + " \t" + resultSet.getObject(3).toString() + " \t" + resultSet.getObject(4).toString() + " \t" + resultSet.getObject(5).toString() + " \t" + resultSet.getObject(6).toString() + " \t" + resultSet.getObject(7).toString() + " \t"); } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("创建 预执行语句 异常"); } finally { DBUtil.closeConnection(conn); } }
@Test public void insertReturnKey(){ String sql = "insert into user(`birthday`,`update_time`,`create_time`,`name`,`status`,`country_id`) values(?,?,?,?,?,?)"; User user = new User(2, new Date(), new Date(), "or 1=1", 1, new Date(), 1002); Connection conn = DBUtil.getConnection(); try { PreparedStatement pst = conn.prepareStatement(sql,1); pst.setDate(1, new java.sql.Date(user.getBirthday().getTime())); pst.setDate(2, new java.sql.Date(user.getUpdateTime().getTime())); pst.setDate(3, new java.sql.Date(user.getCreateTime().getTime())); pst.setString(4, user.getName()); pst.setInt(5, user.getStatus()); pst.setInt(6, user.getCountryId()); log.info("执行的SQL: " + pst.toString()); int res = pst.executeUpdate(); log.info("受影响的行数: " + res); if(res > 0){ ResultSet resultSet = pst.getGeneratedKeys(); while (resultSet.next()) { log.info("第" + resultSet.getRow() + "行数据: " + resultSet.getObject(1)); } } } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("创建 预执行语句 异常"); } finally { DBUtil.closeConnection(conn); } }
}
|