0

I am trying to list a db table in a JSP page.

db = teams table = info

Columns: ID, Name, Year

The connection is successful, its the listing part that's not working.

I have tried changing the code, and same problem occurs.

Connection is successful, but it wont list the items.

The error is most likely in the section where I create the table.

    Document   : index
    Created on : Nov 6, 2019, 8:24:33 AM
    Author     : joy
--%>


<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>   
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>


        <% 
try {

String connectionURL = "jdbc:mysql://localhost:3306/teams"; 

// declare a connection by using Connection interface 
Connection connection = null; 

// Load JBBC driver "com.mysql.jdbc.Driver" 
Class.forName("com.mysql.jdbc.Driver").newInstance(); 

/* Create a connection by using getConnection() method that takes parameters of 
string type connection url, user name and password to connect to database. */ 
connection = DriverManager.getConnection(connectionURL, "root", "");

// check weather connection is established or not by isClosed() method 
if(!connection.isClosed())
%>
<font size="+3" color="green"></b>
<% 
out.println("Successfully connected to " + "MySQL server using TCP/IP...");
connection.close();
}
catch(Exception ex){
%>
</font>
<font size="+3" color="red"></b>
<%
out.println("Unable to connect to database.");
}
%>

    <div align="center">
        <table border="1" cellpadding="5">
            <caption>List of Teams</h2</caption>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Year</th>

            </tr>
            <c:forEach items="${teams}" var="info">
            <tr>      
                <td>${info.id}</td>
                <td>${info.name}</td>
                <td>${info.year}</td>

                    </tr>
            </c:forEach>
        </table>
    </div>



    </body>
</html>

The result should show all data in the table which has 3 columns.

jshariar
  • 338
  • 1
  • 4
  • 15
  • 4
    You are not querying the database. You are just connecting to the database. You will have to add query to fetch data from the table. – Shashank Nov 06 '19 at 14:10
  • 2
    As mentioned above, you are not querying the DB. Moreover, such code does not belong to JSP, learn about [Model-View-Controller](https://www.javaguides.net/2019/08/java-mvc-web-application-using-jsp-and-servlet.html) paradigm. – Jozef Chocholacek Nov 06 '19 at 15:17

0 Answers0