-3

I've tried to retrieve and print the firstname, lastname and address from a database and then display it. I have the servlet below, can someone please help me, thanks :) Will I have to use html at all? It is not printing firstname.

PersonalInfoOut.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class PersonalInfoOutput extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String firstname = request.getParameter("firstname");
        String lastname = request.getParameter("lastname");
        String address = request.getParameter("address");
        if(PersonalInfo.ViewPersonal(firstname, lastname, address))
        out.println(firstname);

        }
    }

PersonalInfo.java

import java.sql.*;


public class PersonalInfo { 

    public static boolean ViewPersonal(String firstname, String lastname, String address)
    {
        boolean st = false;
        try { 
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", ""); 
            PreparedStatement ps = con.prepareStatement("select FirstName, LastName, Address from payroll_system.employee_info where employeeID = 1234"); 
            ps.setString(1, firstname);
            ps.setString(2, lastname);
            ps.setString(3, address); 
            ResultSet rs = ps.executeQuery(); 
            st = rs.next(); 

         }catch(Exception e)
          {
              e.printStackTrace();
          }
             return st;                 

} 
} 
Programmer
  • 1,266
  • 5
  • 23
  • 44
  • 1
    what exactly is it you are having trouble with? – Stultuske Jan 05 '16 at 12:15
  • It's not printing anything when I'm on localhost:8080. According to my code, it should print firstname -> out.println(firstname); – Programmer Jan 05 '16 at 12:16
  • and what do you mean by "print"? let's start there. – Stultuske Jan 05 '16 at 12:17
  • The PreparedStatement really doesn't look correct. – Reinard Jan 05 '16 at 12:17
  • no sh*t. display "how", in an applet? in a Swing UI? in a cmd prompt? be clear about what you are trying to do, we're not mindreaders. If you mean on a webpage: first learn how to write on a webpage, and thén try to implement a db in the process – Stultuske Jan 05 '16 at 12:18
  • @Stultuske Yes, on a webpage with tomcat server. – Programmer Jan 05 '16 at 12:19
  • So, for now, drop the db, and first learn to work with jsp/jsf and servlets, or gwt, or ... and get hardcoded data written on the webpage. only when that works, try to implement a db in the mix – Stultuske Jan 05 '16 at 12:20
  • @Stultuske Is there a way to simply just print a value in the firstname column of a table onto the webpage? – Programmer Jan 05 '16 at 12:22
  • @Reinard What's wrong with the prepared statement? – Programmer Jan 05 '16 at 12:27
  • @Programmer there is a "," at the end of the select list. You never select "Address". You set strings parameters, but there aren't any parameters in the query. – Reinard Jan 05 '16 at 12:29

3 Answers3

0

What is the purpose of these lines .........

PreparedStatement ps = 
  con.prepareStatement("select FirstName, LastName, Address from payroll_system.employee_info where employeeID = 1234"); 
            ps.setString(1, firstname);
            ps.setString(2, lastname);
            ps.setString(3, address);

i hope enough these line .

PreparedStatement ps = 
  con.prepareStatement("select FirstName, LastName, Address from payroll_system.employee_info where employeeID = 1234"); 
MWiesner
  • 8,868
  • 11
  • 36
  • 70
  • I removed those, now it's actually printing something, but it's printing "null". So we got somewhere it seems! But still not printing the information from database. – Programmer Jan 05 '16 at 12:31
0

From the way you asked your question it seems you haven't yet tried to implement a solution and are still not familiar with web technologies. Since you are using Java and Apache Tomcat, I would recommend that, before going any further, you studied what Java server pages(.jsp) are. Check this tutorial to get started.

Luís Cunha
  • 154
  • 2
  • 11
0
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class PersonalInfoOutput extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();

            String firstname = request.getParameter("firstname");
            String lastname = request.getParameter("lastname");
            String address = request.getParameter("address");
            boolean st = false;
            try { 
                Class.forName("com.mysql.jdbc.Driver").newInstance(); 
                Connection con =    DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", ""); 
                PreparedStatement ps = con.prepareStatement("select FirstName, LastName, Address from payroll_system.employee_info where employeeID = 1234"); 
                ResultSet rs = ps.executeQuery(); 
                st = rs.next(); 
                if(st){

                out.println(rs.getString("FirstName"));
                out.println(rs.getString("LastName"));
                out.println(rs.getString("Address"));

                }
             }catch(Exception e)
              {
                  e.printStackTrace();
              }
            out.close();
    }
}
Programmer
  • 1,266
  • 5
  • 23
  • 44