-2

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?

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(firstname);

                }
             }catch(Exception e)
              {
                  e.printStackTrace();
              }
            out.close();
    }
}

The output I'm receiving is:

null
Programmer
  • 1,266
  • 5
  • 23
  • 44
  • Didn't you find anything on Google ? JDBC is a good start, and you can take a look at JPA/Hibernate also ;) – Gaël J Jan 05 '16 at 10:14
  • @Gaël Hi Gael! I already have the code to connect to the database (MySQL). I just need help with the servlet/html part. Ultimately I need to display a simple web page which prints the first name, last name etc. And I don't really want to use any external software such as JPA and Hibernate. I just want to use the tools within Java Eclipse IDE, MySQL, and the Apache Tomcat Server I have running. – Programmer Jan 05 '16 at 10:17
  • JPA and Hibernate are frameworks, you don't need to install additional software ;) – Pieter De Bie Jan 05 '16 at 10:28
  • @PieterDeBie Oh, that sounds cool. Well, I'll search for a tutorial which will be useful for my requirement here. Thanks! – Programmer Jan 05 '16 at 10:32
  • For completeness sake: JPA is a specification and hibernate a framework – Pieter De Bie Jan 05 '16 at 10:36
  • @PieterDeBie Is there any way of performing this basic task without another framework? Simply using html and servlet. – Programmer Jan 05 '16 at 10:40
  • I'm not really sure, I use JSF in combination with Hibernate most of the time. I think looking into JDBC would be the best bet indeed. – Pieter De Bie Jan 05 '16 at 11:21

1 Answers1

-1

Use exceptional handling. close the out object. look the below example and try to debug more : http://www.javawebtutor.com/articles/servlets/servlet_db_example.html

pankaj
  • 1
  • 1