我正在尝试使用 Ms Access 测试连接池,但无济于事。我已经阅读了 Stackoverflow 和其他地方的类似问题,但仍然无法解决问题。
这是Tomcat的错误代码:
javax.naming.NameNotFoundException: Name PracticalODBC1 is not bound in this Context
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:153)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:137)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at ProcessRegistration.init(ProcessRegistration.java:37)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:809)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:619)
这是我的 context.xml:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<Context>
<!-- Specify a JDBC datasource -->
<Resource name="jdbc/odbc:PracticalODBC1" auth="Container"
type="javax.sql.DataSource"
driverClassName="com.jdbc.odbc.JdbcOdbcDriver"
url="jdbc:odbc://localhost:3306/pract1"
maxActive="10" maxIdle="4" />
我的小服务程序:
还有我的 DoPost Servlet:
public class ProcessRegistration extends HttpServlet {
private DataSource dataSource;
private Connection conn;
private Statement stmt;
DataSource pool;
@Override
public void init() throws ServletException {
try {
// Get DataSource
Context initContext = new InitialContext();
pool = (DataSource) initContext.lookup("java:/comp/env/PracticalODBC1");
if (pool == null) {
throw new ServletException("Unknown DataSource 'jdbc/PracticalODBC1'");
}
} catch (NamingException e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
stmt = null;
try {
conn = pool.getConnection();
stmt = conn.createStatement();
// inserting records into database
-----
} catch (SQLException ex) {
Logger.getLogger(ProcessRegistration.class.getName()).log(Level.SEVERE, null,ex);
} finally {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException e) {
throw new ServletException("Servlet records", e);
}
}
out.close();
MySQL 连接器 jar 已放置在 lib 中。
希望有人能告诉我出了什么问题。