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.