我做了一个servlet的web小程序练手,但是数据库这一块始终出错,找不到原因,弄得我身心疲惫啊,请各位大神帮我看看啊.下面是web.xml的设置
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/DBtest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>下面是context.xml才设置
<!-- The contents of this file will be loaded for each web application -->
<Context>
<Resource name="jdbc/DBtest" auth="Container"
type="javax.sql.DataSource"
maxActive="30" maxIdle="10" maxWait="10000"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/hsp"
username="root" password="root" />
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
下面是连接数据库的代码:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/hsp");
con = ds.getConnection();
}catch(Exception ex){
ex.printStackTrace();
}
if(con == null){
System.out.println("con is null!!!!!!!!!!!1");
}
System.out.println("datasource set over!");
try{
System.out.println("statement set!"); stm = con.createStatement();
System.out.println("statement set over!");
}catch(Exception ex){
ex.printStackTrace();
}
我发现con始终是空的,这让我蛋疼了,始终改不出来,求帮助啊!!
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/DBtest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>下面是context.xml才设置
<!-- The contents of this file will be loaded for each web application -->
<Context>
<Resource name="jdbc/DBtest" auth="Container"
type="javax.sql.DataSource"
maxActive="30" maxIdle="10" maxWait="10000"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/hsp"
username="root" password="root" />
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
下面是连接数据库的代码:
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/hsp");
con = ds.getConnection();
}catch(Exception ex){
ex.printStackTrace();
}
if(con == null){
System.out.println("con is null!!!!!!!!!!!1");
}
System.out.println("datasource set over!");
try{
System.out.println("statement set!"); stm = con.createStatement();
System.out.println("statement set over!");
}catch(Exception ex){
ex.printStackTrace();
}
我发现con始终是空的,这让我蛋疼了,始终改不出来,求帮助啊!!
at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
at org.apache.naming.NamingContext.lookup(NamingContext.java:140)
at org.apache.naming.NamingContext.lookup(NamingContext.java:781)
at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at dzy.model.UserBeanCL.setCon(UserBeanCL.java:17)
at dzy.model.UserBeanCL.checkUser(UserBeanCL.java:55)
at dzy.model.LoginCL.doPost(LoginCL.java:17)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
con is null!!!!!!!!!!!1
datasource set over!
statement set!
java.lang.NullPointerException
at dzy.model.UserBeanCL.setCon(UserBeanCL.java:31)
at dzy.model.UserBeanCL.checkUser(UserBeanCL.java:55)
at dzy.model.LoginCL.doPost(LoginCL.java:17)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
第一,数据源你配置的名称是jdbc/DBtest 但是在程序里面你查找的却是java:comp/env/jdbc/hsp,需要改成java:comp/env/jdbc/DBtest第二,你需要确保将mysql的jdbc驱动包放到tomcat/lib 这个包下你这样处理一下,看看还有报错么
<res-ref-name>jdbc/DBtest</res-ref-name>
这个DBtest无所谓的,只要能和<Resource name="jdbc/DBtest"
对上就行了
Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
我在web-inf里的lib里放了mysql-connector-java-5.1了
但是不行
不一样