JSP數(shù)據(jù)庫操作例程(JDBC-ODBC)
為了實(shí)現(xiàn)JSP數(shù)據(jù)庫操作的實(shí)例,建立了一個(gè)MS SQLServer7數(shù)據(jù)庫 DNS,名稱為:Test_DB
數(shù)據(jù)庫中有一個(gè)表:guestbook字段為:name(varchar),email(varchar),body(text)
數(shù)據(jù)庫用戶為sa 密碼空,可以自己修改的。
代碼
- < %@ page contentType="text/html;charset=gb2312"%>
- < %
- //變量聲明
- java.sql.Connection sqlCon; //數(shù)據(jù)庫連接對(duì)象
- java.sql.Statement sqlStmt; //SQL語句對(duì)象
- java.sql.ResultSet sqlRst; //結(jié)果集對(duì)象
- java.lang.String strCon; //數(shù)據(jù)庫連接字符串
- java.lang.String strSQL; //SQL語句
- int intPageSize; //一頁顯示的記錄數(shù)
- int intRowCount; //記錄總數(shù)
- int intPageCount; //總頁數(shù)
- int intPage; //待顯示頁碼
- java.lang.String strPage;
- int i,j,k; //設(shè)置一頁顯示的記錄數(shù)
- intPageSize = 5; //取得待顯示頁碼
- strPage = request.getParameter("page");
- if(strPage==null){
- //表明在QueryString中沒有page這一個(gè)參數(shù),此時(shí)顯示第一頁數(shù)據(jù)
- intPage = 1;
- } else{
- //將字符串轉(zhuǎn)換成整型
- intPage = java.lang.Integer.parseInt(strPage);
- if(intPage< 1) intPage = 1; }
- //裝載JDBC-ODBC驅(qū)動(dòng)程序
- Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
- //設(shè)置數(shù)據(jù)庫連接字符串
- strCon = "jdbc:odbc:Test_DB";
- //連接數(shù)據(jù)庫
- sqlCon = java.sql.DriverManager.getConnection(strCon,"sa","");
- //創(chuàng)建SQL語句對(duì)象
- sqlStmt = sqlCon.createStatement();
- //獲取記錄總數(shù)
- strSQL = "select count(*) from guestbook";
- sqlRst = sqlStmt.executeQuery(strSQL);
- //執(zhí)行SQL語句并取得結(jié)果集
- sqlRst.next(); //記錄集剛打開的時(shí)候,指針位于第一條記錄之前
- intRowCount = sqlRst.getInt(1);
- sqlRst.close(); //關(guān)閉結(jié)果集
- //記算總頁數(shù)
- intPageCount = (intRowCount+intPageSize-1) / intPageSize;
- //調(diào)整待顯示的頁碼 if(intPage>intPageCount) intPage = intPageCount;
- //設(shè)置獲取數(shù)據(jù)SQL語句
- strSQL = "select name,email,body from guestbook";
- //執(zhí)行SQL語句并取得結(jié)果集
- sqlRst = sqlStmt.executeQuery(strSQL);
- //將記錄指針定位到待顯示頁的第一條記錄上
- i = (intPage-1) * intPageSize;
- for(j=0;j< i;j++) sqlRst.next(); %>
- < html>
- < head>
- < title>JSP數(shù)據(jù)庫操作例程 - 數(shù)據(jù)分頁顯示 - JDBC-ODBC< /title>
- < /head>
- < body>
- < p align=center>jdbc-odbc留言版< /p>
- < table border="1" cellspacing="0" cellpadding="0" width=600 align=center>
- < %
- //顯示數(shù)據(jù)
- i = 0;
- while(i< intPageSize && sqlRst.next()){ %>
- < tr>
- < td>姓名:< %=sqlRst.getString(1)%>< /td>
- < td>郵件:< %=sqlRst.getString(2)%>< /td>
- < /tr>
- < tr>
- < td colspan=2>< %=sqlRst.getString(3)%>< /td>
- < /tr>
- < % i++; } %>
- < tr>
- < td colspan=2 align=center>
- 第< %=intPage%>頁 共< %=intPageCount%>頁
- < %if(intPage< intPageCount){%>
- < a href="mssql.jsp?page=< %=intPage+1%>">下一頁< /a>< %
- }
- %>
- < %if(intPage>1){%>
- < a href="mssql.jsp?page=< %=intPage-1%>">上一頁< /a>< %
- }
- %>
- < /td>
- < /tr>
- < /table> < /body>
- < /html>
- < %
- //關(guān)閉結(jié)果集
- sqlRst.close();
- //關(guān)閉SQL語句對(duì)象
- sqlStmt.close();
- //關(guān)閉數(shù)據(jù)庫
- sqlCon.close();
- %>
如何運(yùn)行JSP數(shù)據(jù)庫操作?
將代碼存為文件test.jsp
Orion Application Server下:
Copy到orion的default-web-app目錄下,通過:
- http://localhost:port/test.jsp
訪問測試
對(duì)于Resin,Tomcat,JWS等等,都可以運(yùn)行通過。JSP數(shù)據(jù)庫操作到此完成。
【編輯推薦】