How can I check the database connectivity in JSP. I want to print an error message if there is any problem occurs with the database connectivity.
I m using the following code:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost;databaseName=dbname;user=username;password=password";
Connection conn = DriverManager.getConnection(connectionUrl);
Statement stmt = conn.createStatement();
After successfull connection, I want to insert data to the database. I also want to check the whether the data is inserted properly. Can anyoone help me on this…
The JSP is the wrong place for this. You need to create a standalone class which does the JDBC job and let each of the methods throw an exception whenever the SQL stuff fails.
Here’s an example of a “DAO” class which does all the JDBC stuff on the
Usertable:Then, create a servlet which uses this class and handles the exception. Here’s an example of a
LoginServlet:Let JSP submit to this servlet
You see, when the DAO class throws an
SQLException, the servlet rethrows it asServletException. It will by default end up in a container-default HTTP 500 error page. You can if necessary customize this with a JSP in your own look’n’feel as followsSee also: