如何为两个查询条件执行单个Prepared Statement

时间:2015-02-01 20:21:04

标签: jsp jdbc prepared-statement

我希望查询根据用户编号检索结果。另一个查询按用户名检索。我做了一些工作,但是在NetBeans中它没有显示任何错误,而且输出它只是在第一个if()条件

如果我在这方面做了任何错误的事情,请帮助我解决......

<% String val_name = request.getParameter("name");
String val_number = request.getParameter("number");
String query;
if((val_name != null)&&(val_number != null)) { %>
    <h4> Use only one Column for Search </h3>
<% }
else if((val_name == null)&&(val_number == null)) { %>
    <h4> Enter any Information on above field ... </h4>
<% }
else {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hari","root");  Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","hari","root");
    PreparedStatement stat;
    ResultSet rs;
    if(val_number != null) {
        query= val_number;                // user Entered only Number Field ...
        String sql="SELECT * FROM EMP WHERE EMPNO= ?";
        stat=con.prepareStatement(sql);
        stat.setString(1,val_number);
        rs=stat.executeQuery();
    }
    else {
        query= val_name;  // user Entered only Name Field ...
        String sql="SELECT * FROM EMP WHERE ENAME = ?";
        stat=con.prepareStatement(sql);
        stat.setString(1,val_name);
        rs=stat.executeQuery();
    }

    try {
        if(rs!=null) {
            %>
            <br>
            <table class="gridtable" border=1 cellspan=60 cellpadding=16>
            <tr>
            <th> Emp ID </th>
            <th> Emp Name </th>
            <th> Emp Dept </th>
            <th> Emp D.o.B </th>
            <th> Show Record </th>
            <th> Record Update </th>
            <th> Compassionate </th>
            </tr>
            <%
            while(rs.next()) {
                %>
                <tr>
                <td><%= rs.getInt("EMPNO")%> </td>
                <td><%= rs.getString("ENAME") %> </td>
                <td><%= rs.getString("JOB")%> </td>
                <td><%= rs.getInt("DEPTNO")%> </td>
                <td><button><a  class="ui-button" type="button" href="/result.jsp?name="<%=name%> target="destination"> Show </a> </buton></td>
                <td><button><a  class="ui-button" type="button" href="/update.jsp?name="<%=name%> target="destination"> Update </a> </buton></td>
                <td><button><a  class="ui-button" type="button" href="/compassionate.jsp?name="<%=name%> target="destination"> Compassionate </a> </buton></td>
                </tr>
                <%
            }
        }
    }
    catch(SQLException e) {
        e.printStackTrace();
    }
    con.close();
}%>

1 个答案:

答案 0 :(得分:1)

您的ConnectionPreparedStatementResultSet都在您的上一个else块中定义。将它们移到顶部。

else {          
    String url = "jdbc:oracle:thin:@localhost:1521:XE";
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection con = DriverManager.getConnection(url,"hari","root"); 
    PreparedStatement stat;
    ResultSet rs;

定义像

这样的变量
String val_name = request.getParameter("name"); 
String val_number = request.getParameter("number"); 
String query; 
String url = "jdbc:oracle:thin:@localhost:1521:XE";
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = null; 
PreparedStatement stat = null;
ResultSet rs = null;

然后在if-else链中设置值。