Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5968105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:02:29+00:00 2026-05-22T20:02:29+00:00

How can I check the database connectivity in JSP. I want to print an

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:02:30+00:00Added an answer on May 22, 2026 at 8:02 pm

    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 User table:

    public class UserDAO {
    
        public User find(String username, String password) throws SQLException {
            // ...
        }
    
        public void save(User user) throws SQLException {
            // ...
        }
    
        public void delete(User user) throws SQLException {
            // ...
        }
    
    }
    

    Then, create a servlet which uses this class and handles the exception. Here’s an example of a LoginServlet:

    @WebServlet(urlPatterns={"/login"})
    public class LoginServlet extends HttpServlet {
    
        @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            UserDAO userDAO = new UserDAO();
    
            try {
                User user = userDAO.find(username, password);
    
                if (user != null) {
                    request.getSession().setAttribute("user", user); // Login.
                    response.sendRedirect("userhome");
                } else {
                    request.setAttribute("message", "Unknown login, try again"); // Set error message.
                    request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Redisplay form with error.
                }
            } catch (SQLException e) {
                throw new ServletException("Fatal database failure", e); // <-- Here
            }
        }
    
    }
    

    Let JSP submit to this servlet

    <form action="login" method="post">
        <input type="text" name="username" />
        <input type="password" name="password" />
        <input type="submit" />
        ${message}
    </form>
    

    You see, when the DAO class throws an SQLException, the servlet rethrows it as ServletException. 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 follows

    <error-page>
        <error-code>500</error-code>
        <location>/error.jsp</location>
    </error-page>
    

    See also:

    • How to avoid Java code in JSP files?
    • DAO tutorial – basic code examples
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are no error codes that one can check on System.Data.SqlClient.SqlException. I want to
How I can check the date using a calender , what I want to
Just wondering is there any way I can check whether the url links to
Is there an online database where I can check the amount of available heap
How can I print the values being sent to the sqlite database while using
Can check out here alt text http://51hired.com/static/problem.bmp
I can check for iPhone with this code: (navigator.userAgent.match(/iPhone/i)) But I want to target
Is there a way I can check if a function is true inside another
Is there a way that I can check for specific errors in MySQL with
In php is there a way i can check if a string includes a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.