JPad
Snippets
Download
Help
Contact
Adding to an h2 database and select'ing
Code
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public static void main(String[] args) throws Exception { execute("CREATE TABLE PERSON(id int primary key, name varchar(255))"); insertPerson(1, "Jim"); insertPerson(2, "Kim"); insertPerson(3, "Barry"); try (Connection connection = getDBConnection()){ Statement stmt = connection.createStatement(); Dump(stmt.executeQuery("select * from PERSON"), "select all"); Dump(stmt.executeQuery("select * from PERSON WHERE ID<2"), "Filtered"); } execute("DROP TABLE PERSON"); } private static void execute(String sql) throws SQLException { try (Connection connection = getDBConnection(); Statement stmt = connection.createStatement()) { stmt.execute(sql); } } private static void insertPerson(int id, String name) throws SQLException { String InsertQuery = "INSERT INTO PERSON" + "(id, name) values" + "(?,?)"; try (Connection connection = getDBConnection(); PreparedStatement stmt = connection.prepareStatement(InsertQuery)) { stmt.setInt(1, id); stmt.setString(2, name); stmt.executeUpdate(); } } private static Connection getDBConnection() { try { Class.forName("org.h2.Driver"); return DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "", ""); } catch (ClassNotFoundException | SQLException e) { System.out.println(e.getMessage()); } return null; }
Result
Console
________select all________ ID NAME -------- 1 Jim 2 Kim 3 Barry ________Filtered________ ID NAME ------- 1 Jim
Bytecode
Compiled from "RunnContainer.java" public class io.jpad.scratch.RunnContainer { public io.jpad.scratch.RunnContainer(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]) throws java.lang.Exception; Code: 0: ldc #2 // String CREATE TABLE PERSON(id int primary key, name varchar(255)) 2: invokestatic #3 // Method execute:(Ljava/lang/String;)V 5: iconst_1 6: ldc #4 // String Jim 8: invokestatic #5 // Method insertPerson:(ILjava/lang/String;)V 11: iconst_2 12: ldc #6 // String Kim 14: invokestatic #5 // Method insertPerson:(ILjava/lang/String;)V 17: iconst_3 18: ldc #7 // String Barry 20: invokestatic #5 // Method insertPerson:(ILjava/lang/String;)V 23: invokestatic #8 // Method getDBConnection:()Ljava/sql/Connection; 26: astore_1 27: aconst_null 28: astore_2 29: aload_1 30: invokeinterface #9, 1 // InterfaceMethod java/sql/Connection.createStatement:()Ljava/sql/Statement; 35: astore_3 36: aload_3 37: ldc #10 // String select * from PERSON 39: invokeinterface #11, 2 // InterfaceMethod java/sql/Statement.executeQuery:(Ljava/lang/String;)Ljava/sql/ResultSet; 44: ldc #12 // String select all 46: invokestatic #13 // Method io/jpad/scratch/Dumper.Dump:(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object; 49: pop 50: aload_3 51: ldc #14 // String select * from PERSON WHERE ID<2 53: invokeinterface #11, 2 // InterfaceMethod java/sql/Statement.executeQuery:(Ljava/lang/String;)Ljava/sql/ResultSet; 58: ldc #15 // String Filtered 60: invokestatic #13 // Method io/jpad/scratch/Dumper.Dump:(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object; 63: pop 64: aload_1 65: ifnull 143 68: aload_2 69: ifnull 90 72: aload_1 73: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 78: goto 143 81: astore_3 82: aload_2 83: aload_3 84: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 87: goto 143 90: aload_1 91: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 96: goto 143 99: astore_3 100: aload_3 101: astore_2 102: aload_3 103: athrow 104: astore 4 106: aload_1 107: ifnull 140 110: aload_2 111: ifnull 134 114: aload_1 115: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 120: goto 140 123: astore 5 125: aload_2 126: aload 5 128: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 131: goto 140 134: aload_1 135: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 140: aload 4 142: athrow 143: ldc #19 // String DROP TABLE PERSON 145: invokestatic #3 // Method execute:(Ljava/lang/String;)V 148: return Exception table: from to target type 72 78 81 Class java/lang/Throwable 29 64 99 Class java/lang/Throwable 29 64 104 any 114 120 123 Class java/lang/Throwable 99 106 104 any private static void execute(java.lang.String) throws java.sql.SQLException; Code: 0: invokestatic #8 // Method getDBConnection:()Ljava/sql/Connection; 3: astore_1 4: aconst_null 5: astore_2 6: aload_1 7: invokeinterface #9, 1 // InterfaceMethod java/sql/Connection.createStatement:()Ljava/sql/Statement; 12: astore_3 13: aconst_null 14: astore 4 16: aload_3 17: aload_0 18: invokeinterface #20, 2 // InterfaceMethod java/sql/Statement.execute:(Ljava/lang/String;)Z 23: pop 24: aload_3 25: ifnull 113 28: aload 4 30: ifnull 54 33: aload_3 34: invokeinterface #21, 1 // InterfaceMethod java/sql/Statement.close:()V 39: goto 113 42: astore 5 44: aload 4 46: aload 5 48: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 51: goto 113 54: aload_3 55: invokeinterface #21, 1 // InterfaceMethod java/sql/Statement.close:()V 60: goto 113 63: astore 5 65: aload 5 67: astore 4 69: aload 5 71: athrow 72: astore 6 74: aload_3 75: ifnull 110 78: aload 4 80: ifnull 104 83: aload_3 84: invokeinterface #21, 1 // InterfaceMethod java/sql/Statement.close:()V 89: goto 110 92: astore 7 94: aload 4 96: aload 7 98: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 101: goto 110 104: aload_3 105: invokeinterface #21, 1 // InterfaceMethod java/sql/Statement.close:()V 110: aload 6 112: athrow 113: aload_1 114: ifnull 192 117: aload_2 118: ifnull 139 121: aload_1 122: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 127: goto 192 130: astore_3 131: aload_2 132: aload_3 133: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 136: goto 192 139: aload_1 140: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 145: goto 192 148: astore_3 149: aload_3 150: astore_2 151: aload_3 152: athrow 153: astore 8 155: aload_1 156: ifnull 189 159: aload_2 160: ifnull 183 163: aload_1 164: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 169: goto 189 172: astore 9 174: aload_2 175: aload 9 177: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 180: goto 189 183: aload_1 184: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 189: aload 8 191: athrow 192: return Exception table: from to target type 33 39 42 Class java/lang/Throwable 16 24 63 Class java/lang/Throwable 16 24 72 any 83 89 92 Class java/lang/Throwable 63 74 72 any 121 127 130 Class java/lang/Throwable 6 113 148 Class java/lang/Throwable 6 113 153 any 163 169 172 Class java/lang/Throwable 148 155 153 any private static void insertPerson(int, java.lang.String) throws java.sql.SQLException; Code: 0: ldc #22 // String INSERT INTO PERSON(id, name) values(?,?) 2: astore_2 3: invokestatic #8 // Method getDBConnection:()Ljava/sql/Connection; 6: astore_3 7: aconst_null 8: astore 4 10: aload_3 11: aload_2 12: invokeinterface #23, 2 // InterfaceMethod java/sql/Connection.prepareStatement:(Ljava/lang/String;)Ljava/sql/PreparedStatement; 17: astore 5 19: aconst_null 20: astore 6 22: aload 5 24: iconst_1 25: iload_0 26: invokeinterface #24, 3 // InterfaceMethod java/sql/PreparedStatement.setInt:(II)V 31: aload 5 33: iconst_2 34: aload_1 35: invokeinterface #25, 3 // InterfaceMethod java/sql/PreparedStatement.setString:(ILjava/lang/String;)V 40: aload 5 42: invokeinterface #26, 1 // InterfaceMethod java/sql/PreparedStatement.executeUpdate:()I 47: pop 48: aload 5 50: ifnull 143 53: aload 6 55: ifnull 80 58: aload 5 60: invokeinterface #27, 1 // InterfaceMethod java/sql/PreparedStatement.close:()V 65: goto 143 68: astore 7 70: aload 6 72: aload 7 74: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 77: goto 143 80: aload 5 82: invokeinterface #27, 1 // InterfaceMethod java/sql/PreparedStatement.close:()V 87: goto 143 90: astore 7 92: aload 7 94: astore 6 96: aload 7 98: athrow 99: astore 8 101: aload 5 103: ifnull 140 106: aload 6 108: ifnull 133 111: aload 5 113: invokeinterface #27, 1 // InterfaceMethod java/sql/PreparedStatement.close:()V 118: goto 140 121: astore 9 123: aload 6 125: aload 9 127: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 130: goto 140 133: aload 5 135: invokeinterface #27, 1 // InterfaceMethod java/sql/PreparedStatement.close:()V 140: aload 8 142: athrow 143: aload_3 144: ifnull 232 147: aload 4 149: ifnull 173 152: aload_3 153: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 158: goto 232 161: astore 5 163: aload 4 165: aload 5 167: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 170: goto 232 173: aload_3 174: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 179: goto 232 182: astore 5 184: aload 5 186: astore 4 188: aload 5 190: athrow 191: astore 10 193: aload_3 194: ifnull 229 197: aload 4 199: ifnull 223 202: aload_3 203: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 208: goto 229 211: astore 11 213: aload 4 215: aload 11 217: invokevirtual #18 // Method java/lang/Throwable.addSuppressed:(Ljava/lang/Throwable;)V 220: goto 229 223: aload_3 224: invokeinterface #16, 1 // InterfaceMethod java/sql/Connection.close:()V 229: aload 10 231: athrow 232: return Exception table: from to target type 58 65 68 Class java/lang/Throwable 22 48 90 Class java/lang/Throwable 22 48 99 any 111 118 121 Class java/lang/Throwable 90 101 99 any 152 158 161 Class java/lang/Throwable 10 143 182 Class java/lang/Throwable 10 143 191 any 202 208 211 Class java/lang/Throwable 182 193 191 any private static java.sql.Connection getDBConnection(); Code: 0: ldc #28 // String org.h2.Driver 2: invokestatic #29 // Method java/lang/Class.forName:(Ljava/lang/String;)Ljava/lang/Class; 5: pop 6: ldc #30 // String jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 8: ldc #31 // String 10: ldc #31 // String 12: invokestatic #32 // Method java/sql/DriverManager.getConnection:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/sql/Connection; 15: areturn 16: astore_0 17: getstatic #35 // Field java/lang/System.out:Ljava/io/PrintStream; 20: aload_0 21: invokevirtual #36 // Method java/lang/Exception.getMessage:()Ljava/lang/String; 24: invokevirtual #37 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 27: aconst_null 28: areturn Exception table: from to target type 0 15 16 Class java/lang/ClassNotFoundException 0 15 16 Class java/sql/SQLException }
Jpad Version:1.04
viewCount:4421
Creator:
admin
Latest Editor:
admin
Tags
jdbc
h2
h2db
sql
Download
Copy As Markdown:
**code [Adding to an h2 database and select'ing.java](/example/1p/adding-to-an-h2-database-and-selecting)** import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public static void main(String[] args) throws Exception { execute("CREATE TABLE PERSON(id int primary key, name varchar(255))"); insertPerson(1, "Jim"); insertPerson(2, "Kim"); insertPerson(3, "Barry"); try (Connection connection = getDBConnection()){ Statement stmt = connection.createStatement(); Dump(stmt.executeQuery("select * from PERSON"), "select all"); Dump(stmt.executeQuery("select * from PERSON WHERE ID<2"), "Filtered"); } execute("DROP TABLE PERSON"); } private static void execute(String sql) throws SQLException { try (Connection connection = getDBConnection(); Statement stmt = connection.createStatement()) { stmt.execute(sql); } } private static void insertPerson(int id, String name) throws SQLException { String InsertQuery = "INSERT INTO PERSON" + "(id, name) values" + "(?,?)"; try (Connection connection = getDBConnection(); PreparedStatement stmt = connection.prepareStatement(InsertQuery)) { stmt.setInt(1, id); stmt.setString(2, name); stmt.executeUpdate(); } } private static Connection getDBConnection() { try { Class.forName("org.h2.Driver"); return DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "", ""); } catch (ClassNotFoundException | SQLException e) { System.out.println(e.getMessage()); } return null; } **Result:** ________select all________ ID NAME -------- 1 Jim 2 Kim 3 Barry ________Filtered________ ID NAME ------- 1 Jim