I was assigned a task of making a NetBeans jsp page to run on Tomcat so that the code interfaces and queries a MySql table in order to retrieve its data and display them on an IE page. I did that and the code is working when executed; I'm reproducing it (it's the PhoneBookTableDataRetrieval.jsp file) below:
<%@page import="java.sql.*"%>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%> //<!DOCTYPE html>
// <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<h1></h1>
<%!
public class Record {
String pathToDB = "jdbc:mysql://localhost:3306/agenda";
String username = "root";
String password = "Digital1";
Connection sessionWithAgendaDB = null;
PreparedStatement precompiledStatementToPhoneBookTable = null;
ResultSet queriedTableData = null;
public Record (){
try {
sessionWithAgendaDB =
DriverManager.getConnection(pathToDB, username, password);
precompiledStatementToPhoneBookTable
= sessionWithAgendaDB.prepareStatement("SELECT *
FROM phone_book");
} catch (SQLException e){
e.printStackTrace();
}
}
public ResultSet fetchRecords(){
try {
queriedTableData
= precompiledStatementToPhoneBookTable.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return queriedTableData;
}
}
%>
<%
Record record = new Record();
ResultSet records = record.fetchRecords();
%>
<table border="1">
<tbody>
<tr>
<td>Phone Book Code</td>
<td>First Name</td>
<td>Last Name</td>
<td>Phone Number</td>
</tr>
<% while (records.next()) { %>
<tr>
<td><%= records.getInt("p_B_CODE")%></td>
<td><%= records.getString("p_B_NAME")%></td>
<td><%= records.getString("p_B_SURNAME")%></td>
<td><%= records.getString("p_B_PHONE")%></td>
</tr>
<% } %>
</tbody>
</table>
</body>
Yet I was told that my code was mixing together parts of Java code and parts of HTML code, which is true, and that wasn't good for modularity and portability purposes. So I was told to split the Java code of the classes I wrote inside the jsp page, and to save those classes as *.java files within the src/java folder of the NetBeans project, whose branching I cannot reproduce below with a screenshot, so I kind of plot it:
project Name: PhoneBookTableDataRetrieval, under which are the folders:
build
nbproject
src
conf
java
web
WEB-INF
PhoneBookTableDataRetrieval.jsp
build.xml
My problem is that I don't know neither how I have to write those *.java files in order to make them usable classes (are they a 'public static void main' thing or what ?), nor how I have to call them from within the HTML code in the jsp page (by the way, should I also split the HTML code from the jsp page and save it somewhere else too ? If so, I don't know either how to do that). Can you help me on this ? If you want really to do so, please, write completely the code that should make the *.java files (using of course the Java code of the classes I already wrote), and, please, write completely the statements that call those *.java files from within the jsp page. If you don't write explicitly the code, I won't be able to write it on my own; therefore directions like 'Yes, save the classes in a public static void main file and call them from within the jsp page with a "Load" statement and you'll be fine' are totally useless to me. I apologize in advance for my ignorance but, as I told you, I'm really a dummy programmer (yet this thing is important to me). Thank you really very so much for the detailed (written in a complete fashion) help you'll provide me with.