데이터베이스 커넥션이 필요한 서블릿이나 jsp 파일에서 호출 할 수있는 .java 파일에 별도의 데이터베이스 연결 메소드를 작성하려고했습니다. 내 코드 jsp와 서블릿을위한 별도의 데이터베이스 연결 방법
import java.sql.*;
import java.lang.*;
public class ConnectionClass {
private String username="root";
private String password="passwd";
/* Adjust the above two as per the username
* password combination of your MySql databse */
public Connection connect()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/schooldatabase";
Connection con = DriverManager.getConnection(url,username,password);
return con;
}
catch(Exception e)
{
response.sendRedirect("studentserr.html");
out.println(e);
}
}
}
지금, 문제는 (데이터베이스 연결을 필요로) 모든 서블릿 다양한 문을 실행하는 데 사용할 수 있도록 내가 연결 유형을 반환 할 것이라는 점이다. 그러나, 내 코드에서 catch 블록에서 무엇을 반환해야합니까? (즉, 데이터베이스에 연결할 수 없음을 의미합니다)? 또한, 연결 실패의 경우에, 나는 다음 페이지로 사용자를 리디렉션 해요 :
"studentserr.html"
난 서블릿에 아니지만 된 .java 클래스를 사용하는 경우이 잘 작동합니다. 나는 이것을 위해 무엇을해야합니까 ??
sendRedirect의 사용을 다시 고려할 수도 있습니다. 예를 들어 http://stackoverflow.com/questions/2047122/requestdispatcher-interface-vs-sendredirect를 참조하십시오. – Sridhar