數(shù)據(jù)庫表:
核心代碼:
<%@ page import="java.sql.*"%> //引入相關(guān)類庫
<body>
<%
String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connectDB = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ShopSystem";
try{
Class.forName(JDriver);
}catch(ClassNotFoundException e){
System.out.println("加載數(shù)據(jù)庫引擎失敗");
System.exit(0);
}
try{
String user = "sa";
String password = "xuelong";
Connection con = DriverManager.getConnection(connectDB,user,password);
System.out.println("連接數(shù)據(jù)庫成功");
Statement stmt = con.createStatement();
System.out.println("查詢");
System.out.println("開始查詢數(shù)據(jù)");
String strSql="SELECT TOP 5 p_id,p_type,p_name,p_price,p_quantity FROM product order by p_time desc";
ResultSet rs = stmt.executeQuery(strSql);
%>
<center><h2>最新前5位商品信息</h2></center>
<table border="1" align="center">
<tr>
<th>商品編號(hào)</th>
<th>商品類別</th>
<th>商品名稱</th>
<th>商品單價(jià)</th>
<th>商品數(shù)量</th>
</tr>
<% while(rs.next()){ %>
<tr bgcolor="blue">
<td><%=rs.getString("p_id") %></td>
<td><%=rs.getString("p_type") %></td>
<td><%=rs.getString("p_name") %></td>
<td><%=rs.getString("p_price") %></td>
<td><%=rs.getString("p_quantity") %></td>
</tr>
<% }%>
</table>
<%
System.out.println("讀取完畢");
//當(dāng)由產(chǎn)生它的Statement關(guān)閉或重新執(zhí)行或用于從多結(jié)果序列獲得下一個(gè)結(jié)果時(shí)會(huì)被自動(dòng)關(guān)閉
//rs.close();
stmt.close();
con.close();
}catch (SQLException e){
out.println(e.toString());
}
%>
</body>