从mysql数据库加载下拉列表中的valude

时间:2021-06-02 14:15:55

I am trying to load the value from the mysql database on drop list in jsp . When run the I got some exception. can anyone help me please to fix this issue. I Have use Department bean and adddepartment.jsp file as presentatio layer.

我试图从jsp中的下拉列表中的mysql数据库加载值。跑步时我得到了一些例外。任何人都可以帮我解决这个问题。我使用Department bean和adddepartment.jsp文件作为presentatio层。

<form action="LoginServlet" method="POST" id="theform" name="theform">
                    <table cellspacing="20" cellpading="10">
                        <tr>
                            <td>Department Id:</td>
                            <td><input type="text" name="departmentId" id="userName"/></td>
                        </tr>
                        <tr>
                            <td>Department Name:</td>
                            <td><input type="password" name="departmentName" id="password"/></td>
                        </tr>
                        <tr>
                            <td>Department Name:</td>
                            <td><input type="password" name="departmentName" id="password"/></td>
                        </tr>
                        <tr>
                            <td>Department Head ID:</td>
                            <td><select name="departmentHeadId" style="width: 158px"> <option value="0">Select Department Head</option>
                                    <%
                            //if(request.getParameter("btnvew")!=null)

                         {
                           String val="";
                            Department dp=new Department();
                            ResultSet rs=dp.addHeadId();
                            while(rs.next())
                                {
                                 val=rs.getString(1);
                                %>

                                  <option value="<%= val%>"><%= val%></option>
                                  <%
                                }
                            }
                           %>


                        </select></td>
                        </tr>
                        <tr>
                            <td>Appointed Date:</td>
                            <td><input id="example6" type="text"></td>
                        </tr>
                        <tr>
                            <td><input type="submit" value="Login"/></td>
                        </tr>


                    </table>

org.apache.jasper.JasperException: An exception occurred processing JSP page /AddDepartment.jsp at line 52

49:                        String val="";
50:                         Department dp=new Department();
51:                         ResultSet rs=dp.addHeadId();
52:                         while(rs.next())
53:                             {
54:                              val=rs.getString(1);
55:  
Stacktrace:] with root cause
java.lang.NullPointerException
    at org.apache.jsp.AddDepartment_jsp._jspService(AddDepartment_jsp.java:170)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

public class Department {

    private Connection con;

    public ResultSet addHeadId() throws SQLException {

        ResultSet rs;
        String query = "select * from department_heads";
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(query);

        return rs;

    }
}

2 个解决方案

#1


0  

Your Department#addHeadId() seems to return 'null' as result, as the line on which the NullPointerException happens is

您的部门#addHeadId()似乎返回'null'作为结果,因为发生NullPointerException的行是

while(rs.next())

Please try debugging the addHeadId()-method. Acutally Statement#executeQuery(String) is explicitly specified to never return 'null', so the error seems weird.

请尝试调试addHeadId() - 方法。 Acutally Statement #cuteQuery(String)被明确指定为永远不会返回'null',因此错误似乎很奇怪。

#2


0  

In your addHeadId() method, connection instance is not created. Try this sample;

在addHeadId()方法中,未创建连接实例。试试这个样本;

  public ResultSet addHeadId() throws SQLException {

     try {
        Connection con = DriverManager.getConnection(url,username,password);
        ResultSet rs;
        String query = "select * from department_heads";
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(query);
     } catch (Exception e) {
         e.printStackTrace();
     }
     return rs;

}

Don't forget to instantiate url, username and password.

不要忘记实例化网址,用户名和密码。

#1


0  

Your Department#addHeadId() seems to return 'null' as result, as the line on which the NullPointerException happens is

您的部门#addHeadId()似乎返回'null'作为结果,因为发生NullPointerException的行是

while(rs.next())

Please try debugging the addHeadId()-method. Acutally Statement#executeQuery(String) is explicitly specified to never return 'null', so the error seems weird.

请尝试调试addHeadId() - 方法。 Acutally Statement #cuteQuery(String)被明确指定为永远不会返回'null',因此错误似乎很奇怪。

#2


0  

In your addHeadId() method, connection instance is not created. Try this sample;

在addHeadId()方法中,未创建连接实例。试试这个样本;

  public ResultSet addHeadId() throws SQLException {

     try {
        Connection con = DriverManager.getConnection(url,username,password);
        ResultSet rs;
        String query = "select * from department_heads";
        Statement stmt = con.createStatement();
        rs = stmt.executeQuery(query);
     } catch (Exception e) {
         e.printStackTrace();
     }
     return rs;

}

Don't forget to instantiate url, username and password.

不要忘记实例化网址,用户名和密码。