001: // JdbcBeanUtil.java
002: // $Id: JdbcBeanUtil.java,v 1.6 2001/01/17 14:20:34 bmahe Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.tools.jdbc;
006:
007: import java.sql.SQLException;
008: import java.sql.DatabaseMetaData;
009: import java.sql.ResultSet;
010:
011: import java.util.Properties;
012:
013: /**
014: * @version $Revision: 1.6 $
015: * @author Benoît Mahé (bmahe@w3.org)
016: */
017: public class JdbcBeanUtil {
018:
019: /**
020: * Copy the following list of properties in the given bean and set the
021: * jdbcTable properties
022: * <ul>
023: * <li>jdbcURI
024: * <li>jdbcDriver
025: * <li>jdbcUser
026: * <li>jdbcPassword
027: * </ul>
028: * @param father the source bean
029: * @param child the bean to initialize
030: * @param jdbcTable the SQL table name
031: */
032: public static void initializeBean(JdbcBeanInterface father,
033: JdbcBeanInterface child, String jdbcTable) {
034: child.setJdbcURI(father.getJdbcURI());
035: child.setJdbcDriver(father.getJdbcDriver());
036: child.setJdbcUser(father.getJdbcUser());
037: child.setJdbcPassword(father.getJdbcPassword());
038: child.setJdbcTable(jdbcTable);
039: child.setMaxConn(father.getMaxConn());
040: }
041:
042: public static boolean isJdbcBean(Class c) {
043: do {
044: Class interfaces[] = c.getInterfaces();
045: for (int i = 0; i < interfaces.length; i++) {
046: if (interfaces[i] == JdbcBeanInterface.class) {
047: return true;
048: }
049: }
050: } while ((c = c.getSuperclass()) != null);
051: return false;
052: }
053:
054: public static boolean isIn(String string, String array[]) {
055: if (array == null) {
056: return false;
057: }
058: for (int i = 0; i < array.length; i++) {
059: if (array[i].equals(string)) {
060: return true;
061: }
062: }
063: return false;
064: }
065:
066: public static void generateJdbcBeans(String uri, String driver,
067: String user, String password, String outputDir)
068: throws SQLException {
069: // FIXME
070: JdbcServer server = JdbcServer.getServer(uri, user, password,
071: driver);
072: DatabaseMetaData meta = server.getMetaData();
073: ResultSet set = null;
074: String types[] = { "TABLE" };
075: set = meta.getTables("", "", "%", types); // all tables
076: if ((set != null) && (set.first())) {
077: do {
078: String table = set.getString("TABLE_NAME");
079: System.out.println("table: \"" + table + "\"");
080: ResultSet subset = meta.getColumns("", "", table, "%");
081: if ((subset != null) && (subset.first())) {
082: do {
083: System.out.println("\tColumn: "
084: + subset.getString("COLUMN_NAME"));
085: System.out.println("\t>>>>>>> Data Type: "
086: + subset.getString("DATA_TYPE"));
087: System.out.println("\t>>>>>>> Type Name: "
088: + subset.getString("TYPE_NAME"));
089: } while (subset.next());
090: }
091: } while (set.next());
092: }
093: }
094:
095: public static void main(String args[]) {
096: try {
097: JdbcBeanUtil.generateJdbcBeans(
098: "jdbc:postgresql://coot.inria.fr/dvddb",
099: "org.postgresql.Driver", "joeuser", "caca", "toto");
100: } catch (Exception ex) {
101: ex.printStackTrace();
102: }
103: }
104: }
|