1. 程式人生 > >jsp(out物件可以向html頁面中輸出)

jsp(out物件可以向html頁面中輸出)

<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE HTML >
<html>
  <head>
    <title>小指令碼測試</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="jsp base">
<meta http-equiv="description" content="This is my first jsp page">
  </head>
  <body>
    <div align="center">連線資料庫</div>
    <%
           Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mydatabase","root","123456");
String sql="select * from items";
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
%>
<table border="1" bgcolor="#9999dd"  align="center">
<tr>
  <td>圖書編號</td>
  <td>圖書名稱</td>
  <td>城市</td>
  <td>價格</td>
  <td>庫存量</td>
</tr>
<%
while(rs.next()){
 
out.println("<tr align='center'>");
out.println("<td>"+rs.getInt(1)+"</td>");
out.println("<td>"+rs.getString(2)+"</td>");
out.println("<td>"+rs.getString(3) +"</td>");
out.println("<td>"+rs.getFloat(4)+"</td>");
out.println("<td>"+rs.getInt(5) +"</td>");
 out.println("</tr");
 }
 %>
</table>
  </body>
</html>