(1) 檢索

數(shù)據(jù)庫(kù)表:


商品信息表

核心代碼:

<%@ page import="java.sql.*"%> //引入相關(guān)類庫(kù)

  <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ù)庫(kù)引擎失敗");
            System.exit(0);
        }

        try{
            String user = "sa";
            String password = "xuelong";
            Connection con = DriverManager.getConnection(connectDB,user,password);
            System.out.println("連接數(shù)據(jù)庫(kù)成功");
            
            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>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容