Advertisement
Guest User

Untitled

a guest
Jul 11th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. package foo;
  2.  
  3. import org.jetbrains.annotations.Nullable;
  4.  
  5. import java.sql.*;
  6.  
  7.  
  8. public class Bug {
  9.     public static void main(String[] args) throws Exception {
  10.         String server = "xxx";
  11.         int port = 3306;
  12.         String database = "xxx";
  13.         String user = "xxx";
  14.         String password = "xxx";
  15.         String jdbcString = "jdbc:mysql://" + server + ":" + port + "/" + database + "?rewriteBatchedStatements=true&autoReconnect=true&characterEncoding=UTF-8";
  16.         Connection connection = DriverManager.getConnection(jdbcString, user, password);
  17.         connection.setAutoCommit(false);
  18.         /**
  19.          * CREATE TABLE `test` (
  20.          `foo` int(11) unsigned NOT NULL AUTO_INCREMENT,
  21.          `bar` int(11) DEFAULT NULL,
  22.          PRIMARY KEY (`foo`)
  23.          ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  24.          */
  25.         PreparedStatement stmt = prepareStatement("SELECT * FROM test WHERE foo = ?", new Object[]{
  26.                 1
  27.         }, connection);
  28.         ResultSet rs = stmt.executeQuery();
  29.         ResultSetMetaData rsmd = rs.getMetaData();
  30.         // iterate through the java resultset
  31.         int i = 0;
  32.         while (rs.next()) {
  33.             i++;
  34.         }
  35.         System.out.println("'SELECT * FROM test WHERE foo = ?' returned " + i + " rows (on Stefan's System: 0)");
  36.         stmt.close();
  37.         // i should be zero by here. because test table is empty. let's insert a row.
  38.         stmt = prepareStatement("INSERT INTO test(foo,bar) VALUES (?,?)", new Object[]{1, 2}, connection);
  39.         int rows = stmt.executeUpdate();
  40.         System.out.println("'INSERT INTO test(foo,bar) VALUES (?,?)' returned " + rows + " rows (on Stefan's System: 1)");
  41.         // rows should be 1 by here, because one row was inserted
  42.         connection.commit();
  43.         stmt.close();
  44.         stmt = prepareStatement("SELECT * FROM test WHERE foo = ?", new Object[]{
  45.                 1
  46.         }, connection);
  47.         rs = stmt.executeQuery();
  48.         rsmd = rs.getMetaData();
  49.         // iterate through the java resultset
  50.         i = 0;
  51.         while (rs.next()) {
  52.             i++;
  53.         }
  54.         stmt.close();
  55.         System.out.println("'SELECT * FROM test WHERE foo = ?' returned " + i + " rows (on Stefan's System: 0)");
  56.         // i is still 0 here, no results although we just inserted !!
  57.     }
  58.  
  59.     private static PreparedStatement prepareStatement(String sql, @Nullable Object[] args, Connection connection) throws SQLException {
  60.         PreparedStatement stmt = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY, ResultSet.CLOSE_CURSORS_AT_COMMIT);
  61.         stmt.setPoolable(false);
  62.         stmt.clearParameters();
  63.         if (args != null) {
  64.             int i = 1;
  65.             for (Object arg : args) {
  66.                 if (arg instanceof String) {
  67.                     stmt.setString(i, (String) arg);
  68.                 } else if (arg instanceof Integer) {
  69.                     stmt.setInt(i, (Integer) arg);
  70.                 } else if (arg instanceof Float) {
  71.                     stmt.setFloat(i, (Float) arg);
  72.                 } else if (arg instanceof Short) {
  73.                     stmt.setShort(i, (Short) arg);
  74.                 } else if (arg instanceof Long) {
  75.                     stmt.setLong(i, (Long) arg);
  76.                 } else if (arg instanceof java.util.Date) {
  77.                     stmt.setTimestamp(i, new java.sql.Timestamp(((java.util.Date) arg).getTime()));
  78.                 } else if (arg instanceof Boolean) {
  79.                     stmt.setBoolean(i, (Boolean) arg);
  80.                 } else if (arg == null) {
  81.                     stmt.setNull(i, Types.OTHER);
  82.                 }
  83.                 i++;
  84.             }
  85.         }
  86.         return stmt;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement