001: // Jdbc.java
002: // $Id: Jdbc.java,v 1.7 2000/08/16 21:37:49 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.tools.jdbc;
007:
008: import java.util.Properties;
009:
010: /**
011: * @version $Revision: 1.7 $
012: * @author Benoît Mahé (bmahe@w3.org)
013: */
014: public class Jdbc {
015:
016: public final static String MAX_CONNECTIONS_P = "org.w3c.tools.jdbc.maxconn";
017:
018: public final static int DEFAULT_MAX_CONN = 10;
019:
020: public final static String JDBC_DRIVER_P = "org.w3c.tools.jdbc.jdbcdriver";
021:
022: public final static String USER_P = "org.w3c.tools.jdbc.user";
023:
024: public final static String PASSWORD_P = "org.w3c.tools.jdbc.password";
025:
026: public static int getMaxConn(Properties props) {
027: return getInt(props, MAX_CONNECTIONS_P, DEFAULT_MAX_CONN);
028: }
029:
030: public static Properties setMaxConn(Properties props, int max) {
031: if (max < 1) {
032: props.put(MAX_CONNECTIONS_P, String
033: .valueOf(DEFAULT_MAX_CONN));
034: } else {
035: props.put(MAX_CONNECTIONS_P, String.valueOf(max));
036: }
037: return props;
038: }
039:
040: public static String getDriver(Properties props) {
041: return (String) props.get(JDBC_DRIVER_P);
042: }
043:
044: public static Properties setDriver(Properties props, String driver) {
045: if (driver != null)
046: props.put(JDBC_DRIVER_P, driver);
047: return props;
048: }
049:
050: public static String getUser(Properties props) {
051: return (String) props.get(USER_P);
052: }
053:
054: public static Properties setUser(Properties props, String user) {
055: if (user != null)
056: props.put(USER_P, user);
057: return props;
058: }
059:
060: public static String getPassword(Properties props) {
061: return (String) props.get(PASSWORD_P);
062: }
063:
064: public static Properties setPassword(Properties props,
065: String password) {
066: if (password != null)
067: props.put(PASSWORD_P, password);
068: return props;
069: }
070:
071: //
072: // private
073: //
074:
075: private static void setBoolean(Properties props, String name,
076: boolean bool) {
077: props.put(name, String.valueOf(bool));
078: }
079:
080: private static boolean getBoolean(Properties props, String name) {
081: String p = (String) props.get(name);
082: if (p != null) {
083: return p.equalsIgnoreCase("true");
084: }
085: return false;
086: }
087:
088: private static int getInt(Properties props, String name, int def) {
089: String str = (String) props.get(name);
090: if (str != null) {
091: try {
092: return Integer.parseInt(str);
093: } catch (NumberFormatException ex) {
094: return def;
095: }
096: } else {
097: return def;
098: }
099: }
100:
101: private static void setInt(Properties props, String name, int i) {
102: String sint = String.valueOf(i);
103: props.put(name, sint);
104: }
105:
106: private static long getLong(Properties props, String name, long def) {
107: String str = (String) props.get(name);
108: if (str != null) {
109: try {
110: return Long.parseLong(str);
111: } catch (NumberFormatException ex) {
112: return def;
113: }
114: } else {
115: return def;
116: }
117: }
118:
119: private static void setLong(Properties props, String name, long l) {
120: String slong = String.valueOf(l);
121: props.put(name, slong);
122: }
123:
124: }
|