// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.sql.*;

public class CharVersusVarchar {
   private final static int NREC = 100000;
   private final static int SELREP = 5;
   private final static String F1VAL = "01234567890123456789012345678901234567890123456789" +
                              "01234567890123456789012345678901234567890123456789" +
                              "01234567890123456789012345678901234567890123456789" +
                              "01234567890123456789012345678901234567890123456789" +
                              "01234567890123456789012345678901234567890123456789" +
                              "01234567890123456789012345678901234567890123456789";
   private final static String F2VAL = "ABCDEFGHIJKLMNOPQRSTUVWXYABCDEFGHIJKLMNOPQRSTUVWXY" +
                              "ABCDEFGHIJKLMNOPQRSTUVWXYABCDEFGHIJKLMNOPQRSTUVWXY" +
                              "ABCDEFGHIJKLMNOPQRSTUVWXYABCDEFGHIJKLMNOPQRSTUVWXY" +
                              "ABCDEFGHIJKLMNOPQRSTUVWXYABCDEFGHIJKLMNOPQRSTUVWXY" +
                              "ABCDEFGHIJKLMNOPQRSTUVWXYABCDEFGHIJKLMNOPQRSTUVWXY" +
                              "ABCDEFGHIJKLMNOPQRSTUVWXYABCDEFGHIJKLMNOPQRSTUVWXY";
   private static Connection con;
   private static int testCharInsert(int flen) throws SQLException {
      PreparedStatement pstmt = con.prepareStatement("INSERT INTO c VALUES(?,?)");
      long t1 = System.currentTimeMillis();
      for(int i = 0; i < NREC; i++) {
         pstmt.setString(1, F1VAL.substring(0, flen));
         pstmt.setString(2, F2VAL.substring(0, flen));
         pstmt.executeUpdate();
      }
      long t2 = System.currentTimeMillis();
      pstmt.close();
      return (int)(t2-t1);
   }
   private static int testVarcharInsert(int flen) throws SQLException  {
      PreparedStatement pstmt = con.prepareStatement("INSERT INTO vc VALUES(?,?)");
      long t1 = System.currentTimeMillis();
      for(int i = 0; i < NREC; i++) {
         pstmt.setString(1, F1VAL.substring(0, flen));
         pstmt.setString(2, F2VAL.substring(0, flen));
         pstmt.executeUpdate();
      }
      long t2 = System.currentTimeMillis();
      pstmt.close();
      return (int)(t2-t1);
   }
   private static int testCharSelect(int flen) throws SQLException  {
      Statement stmt = con.createStatement();
      long t1 = System.currentTimeMillis();
      for(int j = 0; j < SELREP; j++) {
         ResultSet rs = stmt.executeQuery("SELECT * FROM c");
         int n = 0;
         String f1 = "";
         String f2 = "";
         while(rs.next()) {
            f1 = rs.getString(1);
            f2 = rs.getString(2);
            n++;
         }
         if((n != NREC) ||
            !f1.equals(F1VAL.substring(0, flen)) ||
            !f2.equals(F2VAL.substring(0, flen))) {
            throw new RuntimeException("Ooops");
         }
      }
      long t2 = System.currentTimeMillis();
      stmt.close();
      return (int)(t2-t1);
   }
   private static int testVarcharSelect(int flen) throws SQLException  {
      Statement stmt = con.createStatement();
      long t1 = System.currentTimeMillis();
      for(int j = 0; j < SELREP; j++) {
         ResultSet rs = stmt.executeQuery("SELECT * FROM vc");
         int n = 0;
         String f1 = "";
         String f2 = "";
         while(rs.next()) {
            f1 = rs.getString(1);
            f2 = rs.getString(2);
            n++;
         }
         if((n != NREC) ||
            !f1.equals(F1VAL.substring(0, flen)) ||
            !f2.equals(F2VAL.substring(0, flen))) {
            throw new RuntimeException("Ooops");
         }
      }
      long t2 = System.currentTimeMillis();
      stmt.close();
      return (int)(t2-t1);
   }
   public static void main(String[] args) throws Exception {
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection("jdbc:mysql://localhost/Test", "", "");
      Statement stmt = con.createStatement();
      int[] flen = { 25, 50, 100, 200, 255 };
      for(int k = 0; k < flen.length; k++) {
         System.out.println("Field length = " + flen[k]);
         for(int i = 0; i < 3; i++) {
            stmt.executeUpdate("CREATE TABLE c (f1 CHAR(255), f2 CHAR(255))");
            stmt.executeUpdate("CREATE TABLE vc (f1 VARCHAR(255), f2 VARCHAR(255))");
            System.out.println("CHAR INSERT : " + testCharInsert(flen[k]));
            System.out.println("VARCHAR INSERT : " + testVarcharInsert(flen[k]));
            System.out.println("CHAR SELECT : " + testCharSelect(flen[k]));
            System.out.println("VARCHAR SELECT : " + testVarcharSelect(flen[k]));
            stmt.executeUpdate("DROP TABLE c");
            stmt.executeUpdate("DROP TABLE vc");
         }
      }
      stmt.close();
      con.close();
   }
}
