我正在尝试访问在部署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].
帮帮我。。我是新手。。
谨致问候。
尝试将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>