Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将SQL查询结果发送到jsp页面?_Jsp_Jdbc - Fatal编程技术网

如何将SQL查询结果发送到jsp页面?

如何将SQL查询结果发送到jsp页面?,jsp,jdbc,Jsp,Jdbc,我有一个包含id(数字)、名称(字符串)、地址(字符串)字段的数据库 我编写了一个java程序EmployeeDAO来执行查询。我将它存储在ResultSet对象rs中。我需要在JSP页面中将这个结果集显示为一个表。如何将此rs发送到JSP页面 public class EmployeeDAO { public _____ list() throws Exception { try { Class.forName("sun

我有一个包含id(数字)、名称(字符串)、地址(字符串)字段的数据库

我编写了一个java程序EmployeeDAO来执行查询。我将它存储在ResultSet对象
rs
中。我需要在JSP页面中将这个结果集显示为一个表。如何将此
rs
发送到JSP页面

public class EmployeeDAO 
{
    public _____ list() throws Exception
    {
        try
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
            String url = "jdbc:odbc:employee_dsn";
            Connection con = DriverManager.getConnection(url);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("Select * from emp_table");
        }
            catch (Exception e)
        {   
             System.out.println(e);
        }
    }
}

优雅的解决方案是将结果集映射到对象列表。看看弹簧,了解如何处理这个问题


在jsp中,您可以使用
循环写出此列表。

优雅的解决方案是将结果集映射到对象列表。看看弹簧,了解如何处理这个问题


在jsp中,您可以使用
循环写出此列表。

首先创建Java模型类Employee,其字段与emp_表中的列相同。例如:

public class Employee {
  private String name;
  private String lastName;
  public void setName(String name) {
    this.name = name;
  }
  public String getName() {
    return this.name;
  }
  public String getLastName() {
    return this.lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}
然后在方法中_list()按如下方式迭代结果集:

public List<Employee> _ list() throws Exception {
     Connection con = null;
     ResultSet rs = null;
     List<Employee> result = new ArrayList<Employee>();
     try
       {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
         String url = "jdbc:odbc:employee_dsn";
         con = DriverManager.getConnection(url);
         Statement stmt = con.createStatement();
         rs = stmt.executeQuery("Select * from emp_table");
         while (rs.next()) {
           Employee emp = new Employee();
          emp.setName(rs.getString("emp_name"));
          emp.setLastName(rs.getString("emp_last_name"));
          result.add(emp);
         }

        }
        catch (Exception e)
        {       
            System.out.println(e);
        } finally {
            if (null != rs) {
              try { rs.close()} catch(Exception ex) {};
            }
            if (null != con) {
              try { con.close()} catch(Exception ex) {};
            }
        }
return result;
  }
<table>
  <c:forEach var="emp" items="${empDao._list}">
    <tr>
      <td>${emp.name}</td>
      <td>${emp.lastName}</td>
    </tr>
  </c:forEach>
</table>
public List\ulist()引发异常{
连接con=null;
结果集rs=null;
列表结果=新建ArrayList();
尝试
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”).newInstance();
String url=“jdbc:odbc:employee\u dsn”;
con=DriverManager.getConnection(url);
语句stmt=con.createStatement();
rs=stmt.executeQuery(“从emp_表中选择*);
while(rs.next()){
员工emp=新员工();
emp.setName(rs.getString(“emp_name”);
emp.setLastName(rs.getString(“emp_last_name”);
结果:添加(emp);
}
}
捕获(例外e)
{       
系统输出打印ln(e);
}最后{
如果(空!=rs){
尝试{rs.close()}catch(Exception ex){};
}
如果(空!=con){
尝试{con.close()}catch(Exception ex){};
}
}
返回结果;
}
在JSP中,您可以像这样迭代集合:

public List<Employee> _ list() throws Exception {
     Connection con = null;
     ResultSet rs = null;
     List<Employee> result = new ArrayList<Employee>();
     try
       {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
         String url = "jdbc:odbc:employee_dsn";
         con = DriverManager.getConnection(url);
         Statement stmt = con.createStatement();
         rs = stmt.executeQuery("Select * from emp_table");
         while (rs.next()) {
           Employee emp = new Employee();
          emp.setName(rs.getString("emp_name"));
          emp.setLastName(rs.getString("emp_last_name"));
          result.add(emp);
         }

        }
        catch (Exception e)
        {       
            System.out.println(e);
        } finally {
            if (null != rs) {
              try { rs.close()} catch(Exception ex) {};
            }
            if (null != con) {
              try { con.close()} catch(Exception ex) {};
            }
        }
return result;
  }
<table>
  <c:forEach var="emp" items="${empDao._list}">
    <tr>
      <td>${emp.name}</td>
      <td>${emp.lastName}</td>
    </tr>
  </c:forEach>
</table>

${emp.name}
${emp.lastName}

首先创建Java模型类Employee,其字段与emp_表中的列相同。例如:

public class Employee {
  private String name;
  private String lastName;
  public void setName(String name) {
    this.name = name;
  }
  public String getName() {
    return this.name;
  }
  public String getLastName() {
    return this.lastName;
  }
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
}
然后在方法中_list()按如下方式迭代结果集:

public List<Employee> _ list() throws Exception {
     Connection con = null;
     ResultSet rs = null;
     List<Employee> result = new ArrayList<Employee>();
     try
       {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
         String url = "jdbc:odbc:employee_dsn";
         con = DriverManager.getConnection(url);
         Statement stmt = con.createStatement();
         rs = stmt.executeQuery("Select * from emp_table");
         while (rs.next()) {
           Employee emp = new Employee();
          emp.setName(rs.getString("emp_name"));
          emp.setLastName(rs.getString("emp_last_name"));
          result.add(emp);
         }

        }
        catch (Exception e)
        {       
            System.out.println(e);
        } finally {
            if (null != rs) {
              try { rs.close()} catch(Exception ex) {};
            }
            if (null != con) {
              try { con.close()} catch(Exception ex) {};
            }
        }
return result;
  }
<table>
  <c:forEach var="emp" items="${empDao._list}">
    <tr>
      <td>${emp.name}</td>
      <td>${emp.lastName}</td>
    </tr>
  </c:forEach>
</table>
public List\ulist()引发异常{
连接con=null;
结果集rs=null;
列表结果=新建ArrayList();
尝试
{
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”).newInstance();
String url=“jdbc:odbc:employee\u dsn”;
con=DriverManager.getConnection(url);
语句stmt=con.createStatement();
rs=stmt.executeQuery(“从emp_表中选择*);
while(rs.next()){
员工emp=新员工();
emp.setName(rs.getString(“emp_name”);
emp.setLastName(rs.getString(“emp_last_name”);
结果:添加(emp);
}
}
捕获(例外e)
{       
系统输出打印ln(e);
}最后{
如果(空!=rs){
尝试{rs.close()}catch(Exception ex){};
}
如果(空!=con){
尝试{con.close()}catch(Exception ex){};
}
}
返回结果;
}
在JSP中,您可以像这样迭代集合:

public List<Employee> _ list() throws Exception {
     Connection con = null;
     ResultSet rs = null;
     List<Employee> result = new ArrayList<Employee>();
     try
       {
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
         String url = "jdbc:odbc:employee_dsn";
         con = DriverManager.getConnection(url);
         Statement stmt = con.createStatement();
         rs = stmt.executeQuery("Select * from emp_table");
         while (rs.next()) {
           Employee emp = new Employee();
          emp.setName(rs.getString("emp_name"));
          emp.setLastName(rs.getString("emp_last_name"));
          result.add(emp);
         }

        }
        catch (Exception e)
        {       
            System.out.println(e);
        } finally {
            if (null != rs) {
              try { rs.close()} catch(Exception ex) {};
            }
            if (null != con) {
              try { con.close()} catch(Exception ex) {};
            }
        }
return result;
  }
<table>
  <c:forEach var="emp" items="${empDao._list}">
    <tr>
      <td>${emp.name}</td>
      <td>${emp.lastName}</td>
    </tr>
  </c:forEach>
</table>

${emp.name}
${emp.lastName}