Mysql數據導出簡單顯示在頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%  
        try {  
            Class.forName("com.mysql.jdbc.Driver");  ////驅動程序名
            String url = "jdbc:mysql://localhost:3306/poitable"; //數據庫名
            String username = "root";  //數據庫用戶名
            String password = "root";  //數據庫用戶密碼
            Connection conn = DriverManager.getConnection(url, username, password);  //連接狀態

            if(conn != null){  
                out.print("數據庫連接成功!");  
                out.print("<br />"); 
%>
<table border="2">
<tr align="center">
<td width="100" number="title">ID</td>
<td width="100" name="title">Name</td>
<td width="100" age="title">Age</td>
<td width="150" age="title">Job</td>
<td width="100" age="title">Chinese</td>
<td width="100" age="title">English</td>
<td width="100" age="title">Math</td>
<td width="100" age="title">Physics</td>
<td width="100" age="title">Chemistry</td>
<td width="100" age="title">Biology</td>
</tr>
               <%
                Statement stmt = null;  
                ResultSet rs = null;  
                String sql = "SELECT * FROM classmate;";  //查詢語句
                stmt = conn.createStatement();  
                rs = stmt.executeQuery(sql);  
                out.print("查詢結果:");  
                out.print("<br/>");
                while (rs.next()) {
                %>
  <tr align="center">  
    <td width="100" ><%=rs.getString("id") %></td>  
    <td width="100" ><%=rs.getString("name") %></td>  
    <td width="100"><%=rs.getString("age") %></td>  
    <td width="100"><%=rs.getString("job") %></td>  
    <td width="100"><%=rs.getString("chinese") %></td>  
    <td width="100"><%=rs.getString("english") %></td>  
    <td width="100"><%=rs.getString("math") %></td>  
    <td width="100"><%=rs.getString("physics") %></td>  
    <td width="100"><%=rs.getString("chemistry") %></td>  
    <td width="100"><%=rs.getString("biology") %></td>  
  </tr>
  <%
            }  
            }else{  
                out.print("連接失敗!");  
            }  
        }catch (Exception e) {        
            //e.printStackTrace();  
            out.print("數據庫連接異常!");  
        }  
%> 
</table>
</body>
</html>

 

顯示結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章