Using JSP Code for JDBC

Here, we will use the same directory for our web application named JdbcWeb under “C:\apache-tomcat-8.5.37\webapps” used in the previous section. A JSP program is created with the name Jdbc.jsp to make the JDBC connection. It is placed directly under JdbcApp. As usual, lib under WEB-INF has the JDBC Type 4 driver for MySQL 8.0.15 named mysql-connector-java-8.0.15.jar. The whole directory structure is shown below:

       Figure 1: The directory structure of the web application

The complete JSP code is given below:~
<%-- Jdbc.jsp --%>
<%@ page import="java.sql.*" %>
<table border="1">
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true";
String user = "root";
String pass = "system";
try {
con = DriverManager.getConnection(url, user, pass);
if(con!=null) {
out.println("Successfully connected to " + "MySQL server using TCP/IP..." + "<br>");
stmt = con.createStatement();
rs = stmt.executeQuery("select * from students");
while ( {

catch (Exception e) {
out.println("Exception: " + e.getMessage());
finally {
try {
if (con != null) {
catch (SQLException e) { }
The procedure for running the Tomcat service (startup.bat) from command prompt is given below:~
C:\apache-tomcat-8.5.37\bin> startup.bat
Using CATALINA_BASE: "C:\apache-tomcat-8.5.37"
Using CATALINA_HOME: "C:\apache-tomcat-8.5.37"
Using CATALINA_TMPDIR: "C:\apache-tomcat-8.5.37\temp"
Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0_162"
Using CLASSPATH: "C:\apache-tomcat-8.5.37\bin\bootstrap.jar;C:\apache-tomcat-8.5.37\bin\tomcat-juli.jar"


Now we open a web browser (Google Chrome) to send the Web URL (http://localhost:8085/JdbcWeb/Jdbc.jsp) to the Tomcat Server. The output is given below:

Figure 2: The output of the JSP code using JDBC


In the next section we will learn about different important methods of JDBC API.