| 1 23 24 package org.dbforms.xmldb; 25 26 import java.io.*; 27 28 import java.sql.*; 29 30 import java.util.*; 31 32 33 34 40 public class DbTool { 41 private String dbURL; 42 private String driverClass; 43 private String outputFile; 44 private String propertyFile; 45 46 52 public DbTool(String propertyFile, 53 String outputFile) { 54 this.propertyFile = propertyFile; 55 this.outputFile = outputFile; 56 } 57 58 61 public void createXMLOutput() { 62 try { 63 StringBuffer result = new StringBuffer (); 64 result.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n\n<dbforms-config>\n"); 65 66 Connection con = createConnection(); 67 68 if (con == null) { 69 System.exit(1); 70 } 71 72 DatabaseMetaData dbmd = con.getMetaData(); 73 String [] types = { 74 "TABLE", 75 "VIEW" 76 }; 77 ResultSet tablesRS = dbmd.getTables("", "", "", types); 78 79 while (tablesRS.next()) { 80 String tableName = tablesRS.getString(3); 81 82 result.append("\t<table name=\""); 83 result.append(tableName); 84 result.append("\">\n"); 85 86 ResultSet rsKeys = dbmd.getPrimaryKeys("", "", tableName); 87 Vector keys = new Vector(); 88 89 while (rsKeys.next()) { 90 String columnName = rsKeys.getString(4); 91 keys.addElement(columnName); 92 } 93 94 rsKeys.close(); 95 96 ResultSet rsFields = dbmd.getColumns("", "", tableName, null); 97 98 while (rsFields.next()) { 99 String columnName = rsFields.getString(4); 100 String typeName = rsFields.getString(6); 101 int columnSize = rsFields.getInt(7); 102 103 result.append("\t\t<field name=\""); 104 result.append(columnName); 105 result.append("\" fieldType=\""); 106 result.append(typeName); 107 result.append("\" size=\""); 108 result.append(columnSize); 109 result.append("\""); 110 111 if (keys.contains(columnName)) { 112 result.append(" isKey=\"true\""); 113 } 114 115 result.append("/>\n"); 116 } 117 118 rsFields.close(); 119 120 result.append("\n\t\t<!-- add \"granted-privileges\" element for security constraints -->\n\n\t</table>\n\n"); 121 } 122 123 tablesRS.close(); 124 125 result.append("\t<!-- ========== Connection =================================== -->\n"); 126 result.append("\t<!--\n"); 127 result.append("\tuncomment this if you have access to JNDI of an application server (see users guide for more info)\n"); 128 result.append("\t<dbconnection\n"); 129 result.append("\t\tname = \"jdbc/dbformstest\"\n"); 130 result.append("\t\tisJndi = \"true\"\n"); 131 result.append("\t/>\n"); 132 result.append("\t-->\n\n"); 133 134 result.append("\t<dbconnection\n"); 135 result.append("\t\tname = \"" + dbURL + "\"\n"); 136 result.append("\t\tisJndi = \"false\"\n"); 137 result.append("\t\tconClass = \"" + driverClass + "\"\n"); 138 result.append("\t/>\n"); 139 result.append("</dbforms-config>"); 140 141 FileOutputStream os = new FileOutputStream(new File(outputFile)); 142 ByteArrayInputStream is = new ByteArrayInputStream(result.toString().getBytes()); 143 144 byte[] b = new byte[1024]; 145 int read; 146 147 while ((read = is.read(b)) != -1) { 148 os.write(b, 0, read); 149 } 150 151 os.close(); 152 System.out.println("finished"); 153 } catch (Exception e) { 154 System.out.println("Error:" + e.toString()); 155 e.printStackTrace(); 156 } 157 } 158 159 160 165 public static void main(String [] args) { 166 if (args.length != 2) { 167 System.out.println("usage: java DbTool propertyFile outputFile\n\nexample:\njava DbTool db.properties config.xml"); 168 System.exit(1); 169 } 170 171 new DbTool(args[0], args[1]).createXMLOutput(); 172 } 173 174 175 private Connection createConnection() 176 throws SQLException, ClassNotFoundException , 177 InstantiationException , IOException, 178 IllegalAccessException { 179 Properties props = new Properties(); 180 props.load(new FileInputStream(new File(propertyFile))); 181 182 this.driverClass = props.getProperty("connection-class"); 183 this.dbURL = props.getProperty("connection-url"); 184 185 String dbUser = props.getProperty("username"); 186 String dbPwd = props.getProperty("password"); 187 188 System.out.println("driverClass=" + driverClass); 189 System.out.println("dbURL=" + dbURL); 190 System.out.println("dbUser=" + dbUser); 191 System.out.println("dbPwd=" + dbPwd); 192 193 Class.forName(driverClass) 194 .newInstance(); 195 196 return DriverManager.getConnection(dbURL, dbUser, dbPwd); 197 } 198 } 199 | Popular Tags |