在SVN上下了个项目到myeclipse中,一直跑不起来,每次点击登录就报Connection错误:
org.springframework.jndi.JndiLookupFailureException:JndiObjectTargetSource failed to obtain new targer object;nested exception is javax.naming.NamingException:Cannot create resource instance
主要就是没有配置好Jndi,关于Jndi,可以看这个链接:http://blog.csdn.net/zhaosg198312/article/details/3979435
主要就是在使用Jndi前要配置源数据,Jndi可以解除代码的耦合,在代码中不涉及到数据库驱动,数据库,用户名和用户密码,但要先在服务器中配置好这些信息。
开发过程中用的是tomcat服务器,所以在tomcat中要进行配置:配置----->tomcat\conf\context.xml文件
<Context>标签里面追加这么一段:
<Resource driverClassName="oracle.jdbc.driver.OracleDriver"
auth="Container"
然后再web.xml中配置这么一段:(在<web-app>标签里加)
<resource-ref>
<res-ref-name>jdbc/jwjsglpt</res-ref-name>
<res-type>java.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
最后使用的代码为:
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
Connection conn = ds.getConnection();