servlet

kb5ga3dv  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(338)

在我的servlet中,我连接了两个表,并希望一次显示一个表,只要没有显示数据,就显示另一个表。这个servlet将基于所选的id显示其属性,
这是密码

Connection con;
    PreparedStatement ps;
    ResultSet rs;
    try{
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kps","root","KPSAdmin1234!");
        ps = con.prepareStatement("Select * from applicant a INNER JOIN JOB B where A.J_ID = B.J_ID AND B.J_ID = '" + j_id + "'" );
        rs = ps.executeQuery();

        while(rs.next())
        {
            //1st table

            String j_title = rs.getString("j_title");
            String j_description = rs.getString("j_description");
            String j_location = rs.getString("j_location");
            String j_type = rs.getString("j_type");
            String j_empType = rs.getString("j_empType");
            String d_name = rs.getString("d_name");
            String j_responsibilities = rs.getString("j_responsibilities");
            String j_salary = rs.getString("j_salary");

            //2nd table
            String js_id = rs.getString("js_id");
            String js_name = rs.getString("js_name");
            String js_ic = rs.getString("js_ic");
            String js_mobile = rs.getString("js_mobile");
            String js_email = rs.getString("js_email");

            //<!-- want to display once -->
            pw.println("<div class=\"panel-body\">");
            pw.println("<title>"+j_title+"</title>");

            pw.println("<div class=\"column left\">");
            pw.println("<div style=\"font-family: Barlow;\">");
            pw.println("<font size = \"5\";>"  + j_title+"</font>");
            pw.println("<h5><font size = \"2\";>&nbsp;<img src=\"img/location.png\"style=\"width:12px;height:12px;\">&nbsp;&nbsp;&nbsp;" + j_location + "</td>");

            pw.println("<br><br><strong><p>RESPONSIBILITIES</p></strong>");
            pw.println("<br><p>" +  j_responsibilities + "</p><br></td>");
            pw.println("<br><br><strong><p>REQUIREMENT</p></strong>");
            pw.println("<br><p>" +  j_description + "</p></td>");
            pw.println("</div>");
            pw.println("</div>");

            pw.println("<div class=\"column right\">");

            pw.println("<br>&nbsp;<img src=\"img/rm.png\"style=\"width:16px;height:16px;\">&nbsp;&nbsp;&nbsp;"  + j_salary );
            pw.println("<br>&nbsp;<img src=\"img/check.png\"style=\"width:14px;height:14px;\">&nbsp;&nbsp;&nbsp;"  + j_type );
            pw.println("<br>&nbsp;<img src=\"img/briefcase.png\"style=\"width:14px;height:14px;\">&nbsp;&nbsp;&nbsp;" + j_empType + "<br><br>");

            pw.println("</div>");
            pw.println("</div>");
            pw.println("</div>");
            pw.println("</div>");
            //<!--end of display once-->

            //want to display as long as there is data in the database so basically while(rs.next()) 
            pw.println("<div class=\"row\">");
            pw.println("<div class=\"col-lg-12\">");
            pw.println("<section class=\"panel\">");
            pw.println("<header class=\"panel-heading\">List Of Applicant</header>");
            pw.println("<div class=\"panel-body\">");
            pw.println("<br><font size = \"3\"<br><a href='resume.jsp?js_id=" + js_id +"'>" +js_name+ "</a></font>");
            pw.print(" <font size=\"2\" "+ js_ic +"&ensp;|&ensp;" +js_email +"&ensp;|&ensp;"+ js_mobile);

            pw.println("&ensp;&ensp;<a class=\"btn btn-primary\" href='ViewJobApplicant?js_id=" + js_id +"'>View Response</a></font>");

        }

我怎么能做到呢?viewservlet
我试着把第一张table放在外面,但是我得到的只是这个错误

java.sql.SQLException: Before start of result set

是的,所以它必须在一段时间内,所以我用不同的sql查询做了两个while,每个查询的错误是,当一段时间停止时,它不能再运行一段时间。。
有什么解决办法吗?

ibps3vxo

ibps3vxo1#

您显示的sql将为第一个表返回许多行 applicant ,与第二排相随 job . 好。。。代码中有两个问题:
你需要 ORDER BY 第一个表中的行。可能需要使用一个键,最好是表的主键。像这样的 order by applicant.id .
第二,你需要“检测”当你从一个改变 applicant 下一个。您需要在检索检测申请者更改的行并相应地显示它时添加逻辑。

相关问题