最近做一个小项目,然后用到数据池。写了一下代码
【Java】
//连接数据库连接池
package jspex.jdbc.stuinfo;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class DbConnPool {
private final static String DATASOURCE="java:comp/env/jspex/conpool";
@SuppressWarnings("finally")
public static Connection getConnection() {
Connection con=null;
try {
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup(DATASOURCE);
con=ds.getConnection();
}catch(Exception e){
System.out.println("Fail to connect the pool!");
System.out.println(e);
}
finally {
return con;
}
}
【/】
为什么老是提示Fail to connect the pool!用的是MyEclipse!
【Java】
//连接数据库连接池
package jspex.jdbc.stuinfo;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class DbConnPool {
private final static String DATASOURCE="java:comp/env/jspex/conpool";
@SuppressWarnings("finally")
public static Connection getConnection() {
Connection con=null;
try {
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup(DATASOURCE);
con=ds.getConnection();
}catch(Exception e){
System.out.println("Fail to connect the pool!");
System.out.println(e);
}
finally {
return con;
}
}
【/】
为什么老是提示Fail to connect the pool!用的是MyEclipse!
Fail to connect the pool!
javax.naming.NameNotFoundException: Name jspex is not bound in this Context
2011-2-26 15:47:54 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at jspex.jdbc.stuinfo.UserDAO.login(UserDAO.java:120)
at org.apache.jsp.check_005flogin_jsp._jspService(check_005flogin_jsp.java:70)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
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(Unknown Source)
还有,
你定义DataSource的时候,要这样写才能和上面的代码吻合:<Resource name="jspex/conpool" auth="Container" ...
<Context reloadbale="true">
<Resource name="jspex/conpool"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc.mysql://localhost:3306/jspex"
username="root"
password=""
type="javax.sql.DataSource"
maxActive="10"
maxIdle="4"
maxWait="-1"
auth="Container"
description="jspex DataBase Connection pool"
/>
</Context>
启动Tomcat的时候应该还报其他错了吧?
上面这行应该这样写:url="jdbc:mysql://localhost:3306/jspex"
type Status reportmessage /Student/login.jspdescription The requested resource (/Student/login.jsp) is not available.