Using JDBC In Java Servlet Programming

Home  »  Java  »  Advance Java  »  Java Servlets  »  Using JDBC In Servlet


Servlet allows you to write and read from a database using JDBC. JDBC helps provide the Java database connectivity application programming interface (API) available in the Java software. Using JDBC in servlet includes setting up the environment, creating a table in the database, and establishing a connection with the database. Before establishing connection with the database, you need to install a database on your machine. Then, you need a database driver to set the environment and locate the database.

After locating the database you need to assign appropriate permissions to connect to the database. The establishment of connection with the database is based on the login information and password entered by the user. The type of permissions assigned may vary with the type of driver used to establish the connection management sytem, the servlet executes the query against the database and displays the result of the query.

Using JDBC in Java servlet enables you to develop servlets that can perform database connectivity. The JDBC code in the servlet file creates a database connection, executes the client queries and closes the connection after the response to the client queries is sent to the clients.

The following program shows the Html file, which invokes the Java servlet that uses JDBC to maintain database connection and executes the SQL queries:

HTML File to Invoke the Java Servlet



The HTML file accepts an integer parameter in the text box and passes it to the Java servlet when the Submit Query button is clicked.

Diagram 24.20 shows the output of the HTML file on the Web browser.



Diagram 24.20 HTML File for Invoking Java Servlet


The Submit query button on the HTML web page invokes the Java servlet. A databaase connection is created by the Java servlet and returns the information of student with the integer value passed in the text box.

The output of the Java servlet that displays the information of the student is shows in Diagram 24.21.

The following program code shows using JDBC in Java Servlet:



The above code creates a Java servlet that can maintain database connection and execute SQL queries using JDBC.


Diagram 24.21 Using JDBC in Servlet



Java Servlet Programming







9 comments:

  1. I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
    Tableau Training in Bangalore

    Tableau Training in Chennai

    ReplyDelete
  2. My coder is trying to persuade me to move to .net from PHP. I have always disliked the idea because of the costs. But he's tryiong none the less. I've been using WordPress on a number of websites for about a year and am anxious about switching to another platform. I have heard fantastic things about blogengine.net. Is there a way I can transfer all my wordpress content into it? Any kind of help would be greatly appreciated! web hosting Sydney

    ReplyDelete
  3. Good Post! Thank you so much for sharing the post, it was so good to read and useful to improve
    Java Training in Electronic City

    ReplyDelete
  4. Java courses in Delhi at APTRON Solutions branches are offered by experienced IT professionals with 10+ years of real-time experience in software development industry. Our trainers have strong experience in Java software development so that best quality training is guaranteed.
    For More Info: Java Course in Delhi

    ReplyDelete