<%@ page language="java" import="java.util.*,java.sql.*"
pageEncoding="gbk"%><%
//GetDBConnect db = null;
Statement stmt = null;
Connection con = null;
ResultSet rs = null;
String sql = ""; String m = (String) request.getParameter("select");
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager
.getConnection("jdbc:mysql://localhost/ch01?user=root&password=19890921"); stmt = con.createStatement();
System.out.println("数据库连接成功"); } catch (ClassNotFoundException e) {
System.out.println("类路径没有找着");
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
e.getNextException();
} //ResultSet rs = null;
//stmt=conn.createStatement(); //String m = request.getParameter("select"); //db = new GetDBConnect(); //db.Connect();
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head> <title>你查询的返回结果</title> </head> <body>
您查询返回的结果为
<br>
<table border="1" cellpadding="0" cellspacing="0"> <%
try {
while (rs.next()) { if (m == "first_name") {
sql = "select first_name  from user_info";
rs = stmt.executeQuery(sql);
%>
<tr>
<td><%=rs.getString("first_name")%></td>
</tr>
<%
} else if (m == "last_name") {
sql = "select last_name from user_info";
rs = stmt.executeQuery(sql);
%>
<tr>
<td><%=rs.getString("last_name")%></td>
</tr>
<%
} else if (m == "nick_name") {
sql = "select nick_name from user_info";
rs = stmt.executeQuery(sql);
%>
<tr>
<td><%=rs.getString("nick_name")%></td>
</tr> <%
} else {
System.out.println("SORRY!没有找到你要查询的信息");
}
}
} catch (Exception e) {
} finally {
//rs.close();
stmt.close();
con.close();
System.out.println("数据库已经成功关闭"); }
%>
</table>
</body>
</html>
我在三个下拉列表里面希望根据选择的项查询   想显示结果 不知道怎么写啊  为什么这样写不可以啊?麻烦高手帮忙解惑

解决方案 »

  1.   

    代码有问题啊,上面的while (rs.next()) 在这里rs还是null呢。
      

  2.   

    <%@ page language="java" import="java.util.*,java.sql.*"
    pageEncoding="utf-8"%><%
    //GetDBConnect db = null;
    List<String> selects = new ArrayList<String>();
    Statement stmt = null;
    Connection con = null;
    ResultSet rs = null;
    String sql = "";
    String m = (String) request.getParameter("select");
    try
    {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager
    .getConnection("jdbc:mysql://localhost/tklcrm?user=root&password=123456"); stmt = con.createStatement();
    System.out.println("数据库连接成功"); if (m == "username")
    {
    sql = "select username from t_user";
    rs = stmt.executeQuery(sql);
    }
    else if (m == "userid")
    {
    sql = "select userid from t_user";
    rs = stmt.executeQuery(sql);
    }
    else if (m == "loginname")
    {
    sql = "select loginname from t_user";
    rs = stmt.executeQuery(sql);
    }
    else
    {
    System.out.println("SORRY!没有找到你要查询的信息");
    }
    if (rs != null)
    {
    while (rs.next())
    {
    selects.add(rs.getString(1));
    }
    }
    }
    catch (ClassNotFoundException e)
    {
    System.out.println("类路径没有找着");
    e.printStackTrace();
    }
    catch (SQLException e)
    {
    e.printStackTrace();
    e.getNextException();
    }
    finally
    {
    //rs.close();
    stmt.close();
    con.close();
    System.out.println("数据库已经成功关闭"); }
    //ResultSet rs = null;
    //stmt=conn.createStatement(); //String m = request.getParameter("select"); //db = new GetDBConnect(); //db.Connect();
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head><title>你查询的返回结果</title></head><body>
    您查询返回的结果为
    <br>
    <table border="1" cellpadding="0" cellspacing="0">
    <%
    for (String select : selects)
    {
    %> <tr bgcolor="e7e7e7">
    <td><%=select%></td>
    </tr>
    <%
    }
    %>
    </table>
    </body>
    </html>
      

  3.   

    谢谢~ 这是叫别人帮忙看了之后的 
    <%@ page language="java" import="java.util.*,java.sql.*"
    pageEncoding="gbk"%><%
    //GetDBConnect db = null;
    Statement stmt = null;
    Connection con = null;
    ResultSet rs = null;
    String sql = ""; String m = (String) request.getParameter("select");
    try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager
    .getConnection("jdbc:mysql://localhost/ch01?user=root&password=19890921"); stmt = con.createStatement();
    System.out.println("数据库连接成功");
    sql = "select "+m+" from user_info  ";
    rs = stmt.executeQuery(sql); } catch (ClassNotFoundException e) {
    System.out.println("类路径没有找着");
    e.printStackTrace();
    } catch (SQLException e) {
    e.printStackTrace();
    e.getNextException();
    } //ResultSet rs = null;
    //stmt=conn.createStatement(); //String m = request.getParameter("select"); //db = new GetDBConnect(); //db.Connect();
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head> <title>你查询的返回结果</title> </head> <body>
    您查询返回的结果为
    <br>
    <table border="1" cellpadding="0" cellspacing="0"> <%
    <table>
    try {
    if (rs != null) {
    while (rs.next()) {
    <tr>
    <td>
    <%=rs.getString(m)%>
    </td>
    </tr>

    } catch (Exception e) {
    } finally {
    //rs.close();
    stmt.close();
    con.close();
    System.out.println("数据库已经成功关闭"); }
    %>
    </table>
    </body>
    </html>