Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package foo;
- import org.jetbrains.annotations.Nullable;
- import java.sql.*;
- public class Bug {
- public static void main(String[] args) throws Exception {
- String server = "xxx";
- int port = 3306;
- String database = "xxx";
- String user = "xxx";
- String password = "xxx";
- String jdbcString = "jdbc:mysql://" + server + ":" + port + "/" + database + "?rewriteBatchedStatements=true&autoReconnect=true&characterEncoding=UTF-8";
- Connection connection = DriverManager.getConnection(jdbcString, user, password);
- connection.setAutoCommit(false);
- /**
- * CREATE TABLE `test` (
- `foo` int(11) unsigned NOT NULL AUTO_INCREMENT,
- `bar` int(11) DEFAULT NULL,
- PRIMARY KEY (`foo`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- */
- PreparedStatement stmt = prepareStatement("SELECT * FROM test WHERE foo = ?", new Object[]{
- 1
- }, connection);
- ResultSet rs = stmt.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- // iterate through the java resultset
- int i = 0;
- while (rs.next()) {
- i++;
- }
- System.out.println("'SELECT * FROM test WHERE foo = ?' returned " + i + " rows (on Stefan's System: 0)");
- stmt.close();
- // i should be zero by here. because test table is empty. let's insert a row.
- stmt = prepareStatement("INSERT INTO test(foo,bar) VALUES (?,?)", new Object[]{1, 2}, connection);
- int rows = stmt.executeUpdate();
- System.out.println("'INSERT INTO test(foo,bar) VALUES (?,?)' returned " + rows + " rows (on Stefan's System: 1)");
- // rows should be 1 by here, because one row was inserted
- connection.commit();
- stmt.close();
- stmt = prepareStatement("SELECT * FROM test WHERE foo = ?", new Object[]{
- 1
- }, connection);
- rs = stmt.executeQuery();
- rsmd = rs.getMetaData();
- // iterate through the java resultset
- i = 0;
- while (rs.next()) {
- i++;
- }
- stmt.close();
- System.out.println("'SELECT * FROM test WHERE foo = ?' returned " + i + " rows (on Stefan's System: 0)");
- // i is still 0 here, no results although we just inserted !!
- }
- private static PreparedStatement prepareStatement(String sql, @Nullable Object[] args, Connection connection) throws SQLException {
- PreparedStatement stmt = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
- stmt.setPoolable(false);
- stmt.clearParameters();
- if (args != null) {
- int i = 1;
- for (Object arg : args) {
- if (arg instanceof String) {
- stmt.setString(i, (String) arg);
- } else if (arg instanceof Integer) {
- stmt.setInt(i, (Integer) arg);
- } else if (arg instanceof Float) {
- stmt.setFloat(i, (Float) arg);
- } else if (arg instanceof Short) {
- stmt.setShort(i, (Short) arg);
- } else if (arg instanceof Long) {
- stmt.setLong(i, (Long) arg);
- } else if (arg instanceof java.util.Date) {
- stmt.setTimestamp(i, new java.sql.Timestamp(((java.util.Date) arg).getTime()));
- } else if (arg instanceof Boolean) {
- stmt.setBoolean(i, (Boolean) arg);
- } else if (arg == null) {
- stmt.setNull(i, Types.OTHER);
- }
- i++;
- }
- }
- return stmt;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement