我正在尝试访问在部署了tomcat服务器的计算机上创建的MS-Access dsn。
正常代码:
Connection con = DriverManager.getConnection(sun.jdbc ....)无效。
所以我得到了一个可以使用上下文完成的解决方案。我目前的代码如下:
<%@ page import="java.sql.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%
String un=request.getParameter("un");
String ps=request.getParameter("ps");
out.println("Redirected from index page<br>");
%>
<%
Connection con=null;
Statement st=null;
ResultSet rs=null;
try{
Context initCtx=new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
DataSource ds=(DataSource)envCtx.lookup("jdbc/product");
con=ds.getConnection();
st=con.createStatement();
rs=st.executeQuery("select * from login where User_name='"+un+"' and password='"+ps+"' ");
if(rs.next())
{
session.setAttribute("user",un);
out.println("<html> <body onload='f.submit()'> <form action='dis.jsp' name='f' </form></body></html> ");
}
else
{
out.println("<h1> Kindly check your username and password </h1>");
}
}
catch(Exception ex)
{
out.println(ex.getMessage());
}
%>
但是当点击提交时,它会显示以下错误:
Redirected from index page
Name [jdbc/product] is not bound in this Context. Unable to find [jdbc].
帮帮我......我是新来的..
问候。
答案 0 :(得分:1)
尝试将 context.xml 放在 META-INF 文件夹下,然后输入以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="jdbc/product" docBase="ABSOLUTE_PATH_OF_PROJECT_HERE"
debug="5" reloadable="true">
<Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="" password="" driverClassName="sun.jdbc.odbc.JdbcOdbcDriver"
url="jdbc:odbc:dbname"/>
</Context>