1

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; 


} 



} 

LoginSevlet.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) 
     { 

     } 

    }  
} 

I am trying to access DBHelper class in LoginServlet while Compilation i did javac DBHelper.java javac -cp "C:\Users\dell\Desktop\MYServlet\WEB-INF\lib\servlet-api-2.5.jar;" LoginServlet.java LoginServlet.java:13: error: cannot find symbol
DBHelper obj=new DBHelper();
^
symbol: class DBHelper
location: class LoginServlet
LoginServlet.java:13: error: cannot find symbol
DBHelper obj=new DBHelper();
^
symbol: class DBHelper
location: class LoginServlet
2 errors
I have placed .java files in C:\Users\dell\Desktop\MYServlet\WEB-INF\classes\com\control
All the Files are in the same package and that too public then why cant LoginServlet access DBHelper class

回答

0

因爲你沒有import com.control.*;LoginServlet類。 有關包Java packages 的更多信息,請嘗試在使用包時使用正確的命令進行編譯和執行。

+0

它們在同一個包中,那麼爲什麼我應該導入並且我認爲導入語句中的關鍵字包不正確。 – user5501265

+0

對不起,更新。 –

+0

我甚至嘗試導入,但它結束了相同的錯誤 – user5501265