试试用JDNI连接数据库。其实不晓得JDNI是啥会事。就照猫画虎。配置一个出来看看。网上也找了些资料来看。还是遇到问题了。所以请教各位大哥大姐们!!!
我用的是Oracle10g数据库:数据库名为 ccpa,用户名为 postaudit,密码为 paea;tomcat是6.0 的。
在tomcat的 context.xml 里的配置为:
<Resource
name="ccpa"
auth="Container"
type="javax.sql.DataSource"
driverClass="oracle.jdbc.driver.OracleDriver"
username="postaudit"
password="paea"
jdbcUrl="jdbc:oracle:thin:@127.0.0.1:1521:ccpa"
idleConnectionTestPeriod="0"
idleMaxAge="60"
partitionCount="1"
maxConnectionsPerPartition="5"
minConnectionsPerPartition="2"
acquireIncrement="2"
poolAvailabilityThreshold="20"
connectionTimeout="60000"
/>
在项目的web.xml里的配置为: <resource-ref>
<description>DB Connection</description>
<res-ref-name>ccpa</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref> 在 项目中代码是:
//连接数据库
public Connection getConnection(){
Connection conn=null;
try {
String jndi="ccpa";
Context ctxt=new InitialContext();
DataSource ds=(DataSource) ctxt.lookup(jndi);
System.out.println("XXXXXXXXXXXXXXX"+ds);
try {
conn=ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
最后却是报错的。报的错为:
javax.naming.NameNotFoundException: Name ccpa is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
at javax.naming.InitialContext.lookup(InitialContext.java:392) 哥哥姐姐们啊!您们说这是咋会事?谢谢!!!
我用的是Oracle10g数据库:数据库名为 ccpa,用户名为 postaudit,密码为 paea;tomcat是6.0 的。
在tomcat的 context.xml 里的配置为:
<Resource
name="ccpa"
auth="Container"
type="javax.sql.DataSource"
driverClass="oracle.jdbc.driver.OracleDriver"
username="postaudit"
password="paea"
jdbcUrl="jdbc:oracle:thin:@127.0.0.1:1521:ccpa"
idleConnectionTestPeriod="0"
idleMaxAge="60"
partitionCount="1"
maxConnectionsPerPartition="5"
minConnectionsPerPartition="2"
acquireIncrement="2"
poolAvailabilityThreshold="20"
connectionTimeout="60000"
/>
在项目的web.xml里的配置为: <resource-ref>
<description>DB Connection</description>
<res-ref-name>ccpa</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref> 在 项目中代码是:
//连接数据库
public Connection getConnection(){
Connection conn=null;
try {
String jndi="ccpa";
Context ctxt=new InitialContext();
DataSource ds=(DataSource) ctxt.lookup(jndi);
System.out.println("XXXXXXXXXXXXXXX"+ds);
try {
conn=ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
最后却是报错的。报的错为:
javax.naming.NameNotFoundException: Name ccpa is not bound in this Context
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
at javax.naming.InitialContext.lookup(InitialContext.java:392) 哥哥姐姐们啊!您们说这是咋会事?谢谢!!!
解决方案 »
- 使用浏览器怎么调用Java Web Service的接口
- Spring 2.5.6+Hibernate 3.5.1整合时,*.hbm.xml文件管理问题
- 学过s2sh的都知道:import 那里报错,用的别人的代码,不知道要加载什么包
- hibernate的查询问题
- 初学:struts2的标签、模板和主题几者之间的关系
- 用Tomcat5.5如何配置Oracle JDBC Driver?
- jsp+resin+sqlserver 中出现的“将截断字符串或二进制数据”问题怎样解决???各位高手帮帮忙?分析一下……
- 怎样添加一个资源的jndi名
- EJB主要是用来做什么呢?好像我目前接触的东西用JSP+JAVABEAN就可以了
- rmi中可以在nameservice中加入安全认证机制嘛200分)
- 学spring时对getBean方法的疑问
- 紧急求助,关于截取字符
<Resource name="jdbc/sample_db" auth="Container"
type="javax.sql.DataSource" maxActive="20" maxIdle="5" maxWait="10000"
username="postaudit"
password="paea"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:ccpa"/>
</Context>
web.xml中如下配置:<resource-ref>
<description>JNDI JDBC DataSource</description>
<res-ref-name>jdbc/ccpa</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
调用的代码如下:Context initContext=new InitialContext();//得到拥有当前java运行环境资源的上下文
Context envContext=(Context)initContext.lookup("java:/comp/env");//从该上下文对象中得到数据源头
DataSource ds=(DataSource)envContext.lookup("jdbc/ccpa");//从数据源中的到一个连接对象
Connection con = ds.getConnection()
……
server.xml中第二行:<Resource name="jdbc/sample_db"更正为:<Resource name="jdbc/ccpa"和你的web.xml中配置的一致.