有人知道怎么在应用程序里使用DataSource吗? 哪位朋友能告诉我怎么实现啊? 我用的是mysql数据库!急啊! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 顶啊!有人知道InitialContextFactoryImpl和com.caucho.naming是哪个jar包里的吗 public static DataSource getDataSourceFromJNDI(String jndiName) throws NamingException { Context initCtx = null; try { // Obtain the initial JNDI context initCtx = new InitialContext(); // Perform JNDI lookup to obtain resource manager // connection factory DataSource ds = (javax.sql.DataSource) initCtx.lookup("java:comp/env/jdbc/" + jndiName); return ds; } catch (NamingException ex) { throw new CannotGetJdbcConnectionException("Naming exception in JNDIConnectionFactory looking up data source [" + jndiName + "]", ex); } finally { try { if (initCtx != null) initCtx.close(); } catch (NamingException ex) { // This shouldn't really happen throw new CannotGetJdbcConnectionException("Naming exception closing context in JNDIConnectionFactory.getConnection", ex); } } } // getDataSourceFromJNDI 楼主说的那两个包象是 resin 的。www.caucho.com 哈,楼主要使用 DataSource,当然应该知道 JNDI 对不对?<web-app> ...... <resource-ref> <res-ref-name>jdbc/simwar-ds</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref></web-app>这是 web.xml 文件中配置的 JDBC 资源引用,可以使用上边的方法:类名.getDataSourceFromJNDI(simwar-ds) 返回容器中正确配置了的数据源。这样说楼主明白了吗?:) 类名.getDataSourceFromJNDI("simwar-ds") 不好意思是我没说清楚!在应用程序里没有使用容器的情况下怎么得到DataSource呢? 说清楚也没有用,那是不成地。数据源是应用服务器特有的东西,没有应用服务器就没有数据源。作为一个常用的解决性能问题的方法,服务器端运行的程序可以使用连接池。各主流数据库的驱动程序中都提供有各自的连接池实现。我从未写过这类东西,楼主可以 GOOGLE 一下,大把。 求助集合问题 请问为什么不能显示文本提示 java窗体 请教java问题 Socket编程中为什么readLine()返回的字符串前面总有\u0000? 新手求助关于怎么在JAVA中对INT 进行与或非运算 java程序里面一个小错误,那位高手来看一下是什么意思? 求救 junit4有没有可以记录@Test方法中每条语句执行时间的功能 大家帮我看看这段代码:它是如何得到name和phone的值,并写到文件中取得 .net中有个dataset的东西,java中有对应的类型马? 一个关于内部类的问题
Context initCtx = null;
try {
// Obtain the initial JNDI context
initCtx = new InitialContext();
// Perform JNDI lookup to obtain resource manager
// connection factory
DataSource ds = (javax.sql.DataSource) initCtx.lookup("java:comp/env/jdbc/" + jndiName);
return ds;
}
catch (NamingException ex) {
throw new CannotGetJdbcConnectionException("Naming exception in JNDIConnectionFactory looking up data source [" + jndiName + "]", ex);
}
finally {
try {
if (initCtx != null)
initCtx.close();
}
catch (NamingException ex) {
// This shouldn't really happen
throw new CannotGetJdbcConnectionException("Naming exception closing context in JNDIConnectionFactory.getConnection", ex);
}
}
} // getDataSourceFromJNDI
......
<resource-ref>
<res-ref-name>jdbc/simwar-ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>这是 web.xml 文件中配置的 JDBC 资源引用,可以使用上边的方法:类名.getDataSourceFromJNDI(simwar-ds) 返回容器中正确配置了的数据源。这样说楼主明白了吗?:)