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 /* * DerbyProfileDB.java * * Created on October 21, 2006, 9:14 PM * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. */ package my_beans; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * * @author bllim */ public class DerbyProfileNoIdDB { public static void main(String args[]) { new DerbyProfileNoIdDB(args); } /** Creates a new instance of DerbyProfileDB */ public DerbyProfileNoIdDB(String args[]) { try { Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance(); //NOI18N } catch (Exception e) { System.err.println("Class.forName: " + e.getMessage()); //NOI18N System.exit(1); } createProfileDatabase(args); } void createProfileDatabase(String args[]) { String port = "21527"; Connection con; Statement stmt; String url = "jdbc:derby://localhost:" + port + "/sample"; //NOI18N try { con = DriverManager.getConnection(url, "dbadmin", "dbadmin"); //NOI18N CallableStatement cs = con.prepareCall("CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)"); cs.setString(1, "derby.user.profile"); cs.setString(2, "profile"); cs.execute(); // make profile a full access user cs.setString(1, "derby.database.fullAccessUsers"); cs.setString(2, "APP,dbadmin,travel,vir,jsc,music,login,profile"); cs.execute(); cs.close(); stmt = con.createStatement(); 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 try { createSchema(stmt, "ProfileNoId"); //NOI18N } catch (Exception e) { System.err.println("Unable to create schema on url " + url + ": " + e.getMessage()); System.err.println("Okay if Schema 'PROFILE' already exists."); } con.close(); con = DriverManager.getConnection(url, "profile", "profile"); //NOI18N stmt = con.createStatement(); // Drop all tables first to avoid constraints dropTable(stmt, "Users"); dropTable(stmt, "ValidationOnly"); // Users Table createTable(stmt, "Users", "Username VARCHAR(20), Gender VARCHAR(20), Position VARCHAR(20), Industries VARCHAR(50), Interests VARCHAR(50)"); // If one wants to add PK constraint (and bunch of others), one can add the statement below: // addConstraint(stmt, "Users", "UsersPK PRIMARY KEY (UserID)"); insertRow(stmt, "Users", "'testUser', 'male', 'CEO', 'Education, Retail', 'Travel, Sports'"); createTable(stmt, "ValidationOnly", "Col1 INTEGER NOT NULL, Col2 VARCHAR(15)"); //NOI18N System.out.println("Profile database was created."); stmt.close(); con.close(); } catch (SQLException ex) { System.err.println("Error on url " + url ); System.err.println("ERROR: SQLException: " + ex.getMessage()); ex.printStackTrace(); } catch (Exception e) { System.out.println(e); e.printStackTrace(); } } void createSchema(Statement stmt, String schemaName) throws SQLException { try { stmt.executeUpdate("DROP SCHEMA " + schemaName); //NOI18N } catch (SQLException e) { } stmt.executeUpdate("CREATE SCHEMA " + schemaName); //NOI18N } void createTable(Statement stmt, String name, String cols) throws SQLException { stmt.executeUpdate("CREATE TABLE " + name + " (" + cols + ")"); //NOI18N } void dropTable(Statement stmt, String name) throws SQLException { try { stmt.executeUpdate("DROP TABLE " + name); //NOI18N } catch (SQLException e) { System.err.println("cannot drop table " + name + ": " + e.getMessage()); } } 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 void createView(Statement stmt, String name, String select) throws SQLException { try { stmt.executeUpdate("DROP VIEW " + name); //NOI18N } catch (SQLException e) { } stmt.executeUpdate("CREATE VIEW " + name + " AS " + select); //NOI18N } void addConstraint(Statement stmt, String name, String constraint) throws SQLException { stmt.executeUpdate("ALTER TABLE " + name + " ADD CONSTRAINT " + constraint); //NOI18N } void insertRow(Statement stmt, String name, String values) throws SQLException { stmt.executeUpdate("INSERT INTO " + name + " VALUES (" + values + ")"); //NOI18N } }