在网页上显示sql数据库

时间:2015-01-28 16:01:18

标签: java javascript sql

<%-- 
    Document   : CheckRetrieve1
    Created on : Jan 26, 2015, 12:50:28 PM
    Author     : Dell
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"  %>
<%@page import="java.sql.ResultSet" %>
<%@page import="java.sql.Statement" %>
<%@page import="java.io.IOException" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>STAFF</title>
    </head>
    <body>
        <h1>Website</h1>
        <h2>Teaching Staff</h2>
       <% 
           try
           {
               Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");



            Connection con = DriverManager.getConnection(
                "jdbc:odbc:Kam", "", "");

            Statement statement = con.createStatement() ;
            ResultSet resultset = 
                statement.executeQuery("select * from extcteach") ; 
           }
           catch(Exception e)
           {
               e.printStackTrace();
            }
        %>


        </body>
</html>

我正在尝试使用Java制作网站。 sql数据库,其中有一个特定的表,我想在我的网站上显示。然后我运行它..它显示“TODO WRITE CONTENT”。我发现与连接相关的东西它没有用。请帮助:)

1 个答案:

答案 0 :(得分:0)

首先,您必须将结果集放入对象列表中。例如:

ArrayList<Extcteach> extcteachList = new ArrayList<Extcteach>();
while(resultset .next()) {
        Extcteach extcteach = new Extcteach ();

        extcteach.setAttr1 = (result.getString("column1")
        extcteach.setAttr2 = (result.getString("column2")

        /****THIS FOR EACH COLUMN OF YOUR TABLE****/

        extcteachList.put(extcteach) 
    }

现在你有一个对象列表,所以在你的jsp你必须做这样的事情:

<table>
   <% 
   for(int i=0; i < extcteachList.size(); i++) {
   %>           
      <tr>
        <td> <%= extcteachList.get(i).getAttr1() %>  </td>
        <td> <%= extcteachList.get(i).getAttr1() %>  </td>  
      <tr>
   <%
   }
   %>
</table>

注意:您必须创建Extcteach类