У меня есть 2 Java-файла, оба находятся в одном пакете.Я хочу вызвать метод из одного класса в другой класс, которые находятся в том же пакете.
Я пытался вызвать метод, сделав этот метод статическим
TheClass.theMethod();
И я также попытался вызвать метод, создав экземпляр класса и сделав метод нестатическим
TheClass t = new TheClass();
t.theMethod();
Я также попытался вызвать метод, сохранив java-файлы в другом пакете, а затем импортировав другой файл java Class.
Но, похоже, у меня ничего не работает.Я получаю ClassNotFoundException всеми способами.
Я вложил оба моих java файла ниже
package bookshow;
import java.sql.*;
import java.io.*;
import java.util.*;
public class BookMyShow
{
public static Connection getConnection()
{
Connection con = null;
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql:///sample","root","root");
return con;
}
catch(Exception e)
{
System.out.println(e);
return con;
}
}
public static boolean isValidUser(String name,String password)
{
try
{
Connection con=getConnection();
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from user where name='"+name+"' and password='"+password+"'");
if(rs.next())
{
return true;
}
return false;
}
catch(Exception e)
{
System.out.println(e);
return false;
}
}
}
Это мой класс сервлетов
package bookshow;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import java.io.PrintWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.DataOutputStream;
import java.nio.charset.StandardCharsets;
public class LoginServlet extends HttpServlet
{
public void doPost(HttpServletRequest request , HttpServletResponse response) throws IOException , ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name=request.getParameter("userName");
String password=request.getParameter("password");
out.println(name+" "+password);
if(BookMyShow.isValidUser(name,password))
{
out.println("Sucessfully Login");
}
else
{
out.println("Enter correct password");
}
}
}
Любые предложения enter code here