01: package test;
02:
03: import java.sql.Connection;
04: import java.sql.Driver;
05: import java.sql.SQLException;
06: import java.util.Properties;
07:
08: public class SQuirreLConnectTest {
09:
10: public static void main(String[] args) throws Exception {
11: if (args.length < 4) {
12: printUsage();
13: }
14: String driver = args[0];
15: String url = args[1];
16: String user = args[2];
17: String pass = args[3];
18:
19: Connection connection = getConnection(driver, url, user, pass,
20: null);
21:
22: System.out.println("Connected to: "
23: + connection.getMetaData().getURL());
24: }
25:
26: private static void printUsage() {
27: System.out
28: .println("SQuirreLConnectTest: <driver> <url> <user> <pass>");
29: System.exit(-1);
30: }
31:
32: /**
33: * @param props
34: * may be null
35: */
36: public static Connection getConnection(String driver, String url,
37: String user, String pw, Properties props)
38: throws ClassNotFoundException, IllegalAccessException,
39: InstantiationException, SQLException {
40: if (null == props) {
41: props = new Properties();
42: }
43:
44: props.put("user", user);
45: props.put("password", pw);
46:
47: Driver driverInst = (Driver) Class.forName(driver)
48: .newInstance();
49:
50: Connection jdbcConn = driverInst.connect(url, props);
51: if (jdbcConn == null) {
52: throw new RuntimeException("Connect failed");
53: }
54:
55: return jdbcConn;
56: }
57: }
|