01: /* JFox, the OpenSource J2EE Application Server
02: *
03: * Copyright (C) 2002 huihoo.org
04: * Distributable under GNU LGPL license
05: * See the GNU Lesser General Public License for more details.
06: */
07:
08: package org.huihoo.jfox.datasource;
09:
10: import org.huihoo.jfox.datasource.PoolDataSource;
11: import org.huihoo.jfox.jndi.JNDIProperties;
12:
13: import javax.sql.DataSource;
14: import javax.naming.Context;
15: import javax.naming.InitialContext;
16: import java.sql.Connection;
17: import java.sql.Statement;
18: import java.sql.ResultSet;
19:
20: /**
21: *
22: * @author <a href="mailto:kelvin_wym@hotmail.com">Kelvin Wu</a>
23: */
24: public class Test {
25: public static void main(String[] args) throws Exception {
26: System.out.println("Test Generic Usage:");
27: testGenericUsage();
28: System.out.println();
29: System.out.println();
30: System.out.println("Test JNDI Usage:");
31: testJNDIUsage();
32: }
33:
34: public static void testGenericUsage() throws Exception {
35: DataSource ds = getDataSource();
36: Connection conn = ds.getConnection();
37: executeSQL(conn);
38: }
39:
40: public static void testJNDIUsage() throws Exception {
41: System.setProperty("java.naming.factory.initial",
42: JNDIProperties.INITIAL_CONTEXT_FACTORY);
43: System.setProperty("java.naming.factory.url.pkgs",
44: "org.huihoo.jfox.jndi");
45: System.setProperty("java.naming.provider.url",
46: JNDIProperties.PROVIDER_URL);
47: Context ctx = new InitialContext();
48:
49: System.out.println("createSubcontext: yyy/zzz");
50: ctx.createSubcontext("yyy/zzz");
51:
52: System.out.println("createSubcontext: yyy/zzz/aaa");
53: ctx.createSubcontext("yyy/zzz/aaa");
54:
55: System.out.println("rebind: /yyy/zzz/mysqlDS");
56: ctx.rebind("/yyy/zzz/mysqlDS", getDataSource());
57:
58: System.out.println("lookup: yyy/zzz/mysqlDS");
59: Object ref = ctx.lookup("yyy/zzz/mysqlDS");
60:
61: System.out.println("Invoke method: ");
62: DataSource ds = (DataSource) (ref);
63: Connection conn = ds.getConnection();
64: executeSQL(conn);
65: Connection conn2 = ds.getConnection();
66: executeSQL(conn2);
67:
68: }
69:
70: public static DataSource getDataSource() throws Exception {
71: PoolDataSource ds = new PoolDataSource(
72: "org.gjt.mm.mysql.Driver",
73: "jdbc:mysql://192.168.0.2/mysql", "root", "");
74: return ds;
75: // return new PoolDataSource();
76: }
77:
78: public static void executeSQL(Connection conn) throws Exception {
79: Statement stmt = conn.createStatement();
80: ResultSet rs = stmt.executeQuery("select * from user");
81: while (rs.next()) {
82: System.out.println(rs.getString(2));
83: }
84: }
85: }
|