如何从数据库中打印名字,姓氏和地址?

时间:2016-01-05 12:14:28

标签: java servlets

我试图从数据库中检索并打印名字,姓氏和地址,然后显示它。我有下面的servlet,有人可以帮助我,谢谢:)我是否必须使用html?它不是打印名字。

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;                 

} 
} 

3 个答案:

答案 0 :(得分:0)

这些线的目的是什么.........

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

我希望这些线路足够。

PreparedStatement ps = 
  con.prepareStatement("select FirstName, LastName, Address from payroll_system.employee_info where employeeID = 1234"); 

答案 1 :(得分:0)

从您提出问题的方式来看,您似乎尚未尝试实施解决方案,但仍不熟悉网络技术。由于您使用的是Java和Apache Tomcat,我建议您在进一步研究之前,研究一下Java服务器页面(.jsp)。检查this教程即可开始使用。

答案 2 :(得分: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();
    }
}