DBHelper.java
package com.control;
import java.sql.*;
public class DBHelper
{
Connection conn;
Statement st;
ResultSet rs;
public DBHelper()throws ClassNotFoundException
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
public boolean getConnection()throws SQLException
{
conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","system");
if(conn!=null)
return true;
return false;
}
public void execute(String query)throws SQLException
{
st=conn.createStatement();
st.execute(query);
}
public boolean match(String uname,String pwd)throws Exception
{
st=conn.createStatement();
rs=st.executeQuery("select * from register");
while(rs.next())
{
String unm=rs.getString(1);
String pd=rs.getString(2);
if(uname.equals(unm) && pwd.equals(pd))
{
return true;
}
}
return false;
}
}
登录Sevlet.java
package com.control;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class LoginServlet extends HttpServlet
{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
try{
DBHelper obj=new DBHelper();
obj.getConnection();
PrintWriter out=res.getWriter();
res.setContentType("text/html");
String uname=req.getParameter("email");
String pwd=req.getParameter("password");
//ResultSet rs=obj.match(uname,pwd);
//if(uname.equals(rs.getString(1)) && pwd.equals(rs.getString(2)))
if(obj.match(uname,pwd))
{
RequestDispatcher rd=req.getRequestDispatcher("profile.html");
rd.forward(req,res);
}
else
{
RequestDispatcher rd=req.getRequestDispatcher("index.html");
rd.include(req,res);
out.println("<b>Incorrect Email or password!</b>");
}
}catch(Exception e)
{
}
}
}
我试图在编译时访问 LoginServlet 中的 DBHelper 类javac DBHelper.java javac -cp "C:\Users\dell\Desktop\MYServlet\WEB-INF\lib\servlet-api-2.5.jar;" LoginServlet.java LoginServlet.java:13:错误:找不到符号
DBHelper obj=new DBHelper();
^
符号:类 DBHelper
位置:类 LoginServlet
LoginServlet.java:13:错误:找不到符号
DBHelper obj=new DBHelper();
^
符号:类 DBHelper
位置:类 LoginServlet
2 错误
我已将 .java 文件放在 C:\Users\dell\Desktop\MYServlet\WEB-INF\classes\com\control
所有文件都在同一个包中,而且太公开了,那么为什么 LoginServlet 不能访问 DBHelper 类