0

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.

alexandresaiz
  • 2,678
  • 7
  • 29
  • 40
  • Unfortunately, alexandersaiz, your answer is exactly what I specified is useless to me. I really need the things done the way I ask them, only thereafter, and with the help of some video-tutorial, I'll try and understand what brought from my code to the 'modular' code I'm asking for. – john_angevin May 21 '15 at 17:08
  • Ok, thank you very much, alexandersaiz and Pshemo, I'll try that if I don't make any progress. – john_angevin May 22 '15 at 14:46

1 Answers1

1

I think it will difficult to just get code on this site.

Nevertheless, I think since you were able to write all that code, changing to a modular form should be the easy part for you once you get to know about:

  1. Servlets

  2. Forwarding from Servlet to JSP

Try this link: https://danielniko.wordpress.com/2012/04/17/simple-crud-using-jsp-servlet-and-mysql/

That article has a link to github project, which has all the source you need.

Mecon
  • 977
  • 1
  • 6
  • 17
  • Thank you very much to you too, Mecon, I'll try to do the rewrite myself by following firstly your suggestion and the one given to me in the first answer, and secondly, shouldn't I be unable to overcome the problem, by turning to freelancer.com – john_angevin May 22 '15 at 14:46