日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JSP數(shù)據(jù)庫操作例程(JDBC-ODBC)

為了實(shí)現(xiàn)JSP數(shù)據(jù)庫操作的實(shí)例,建立了一個(gè)MS SQLServer7數(shù)據(jù)庫 DNS,名稱為:Test_DB

創(chuàng)新互聯(lián)公司主營禮縣網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都app開發(fā),禮縣h5小程序開發(fā)搭建,禮縣網(wǎng)站營銷推廣歡迎禮縣等地區(qū)企業(yè)咨詢

數(shù)據(jù)庫中有一個(gè)表:guestbook字段為:name(varchar),email(varchar),body(text)

數(shù)據(jù)庫用戶為sa 密碼空,可以自己修改的。

代碼

 
 
 
  1. < %@ page contentType="text/html;charset=gb2312"%> 
  2. < %  
  3. //變量聲明  
  4. java.sql.Connection sqlCon; //數(shù)據(jù)庫連接對(duì)象  
  5. java.sql.Statement sqlStmt; //SQL語句對(duì)象  
  6. java.sql.ResultSet sqlRst; //結(jié)果集對(duì)象  
  7. java.lang.String strCon; //數(shù)據(jù)庫連接字符串  
  8. java.lang.String strSQL; //SQL語句  
  9. int intPageSize; //一頁顯示的記錄數(shù)  
  10. int intRowCount; //記錄總數(shù)  
  11. int intPageCount; //總頁數(shù)  
  12. int intPage; //待顯示頁碼  
  13. java.lang.String strPage;  
  14. int i,j,k; //設(shè)置一頁顯示的記錄數(shù)  
  15. intPageSize = 5; //取得待顯示頁碼  
  16. strPage = request.getParameter("page");  
  17. if(strPage==null){  
  18. //表明在QueryString中沒有page這一個(gè)參數(shù),此時(shí)顯示第一頁數(shù)據(jù)  
  19. intPage = 1;  
  20. } else{  
  21. //將字符串轉(zhuǎn)換成整型  
  22. intPage = java.lang.Integer.parseInt(strPage);  
  23. if(intPage< 1) intPage = 1; }  
  24. //裝載JDBC-ODBC驅(qū)動(dòng)程序  
  25. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  26. //設(shè)置數(shù)據(jù)庫連接字符串  
  27. strCon = "jdbc:odbc:Test_DB";  
  28. //連接數(shù)據(jù)庫  
  29. sqlCon = java.sql.DriverManager.getConnection(strCon,"sa","");  
  30. //創(chuàng)建SQL語句對(duì)象  
  31. sqlStmt = sqlCon.createStatement();  
  32. //獲取記錄總數(shù)  
  33. strSQL = "select count(*) from guestbook";  
  34. sqlRst = sqlStmt.executeQuery(strSQL);  
  35. //執(zhí)行SQL語句并取得結(jié)果集  
  36. sqlRst.next(); //記錄集剛打開的時(shí)候,指針位于第一條記錄之前  
  37. intRowCount = sqlRst.getInt(1);  
  38. sqlRst.close(); //關(guān)閉結(jié)果集  
  39. //記算總頁數(shù)  
  40. intPageCount = (intRowCount+intPageSize-1) / intPageSize;  
  41. //調(diào)整待顯示的頁碼 if(intPage>intPageCount) intPage = intPageCount;  
  42. //設(shè)置獲取數(shù)據(jù)SQL語句  
  43. strSQL = "select name,email,body from guestbook";  
  44. //執(zhí)行SQL語句并取得結(jié)果集  
  45. sqlRst = sqlStmt.executeQuery(strSQL);  
  46. //將記錄指針定位到待顯示頁的第一條記錄上  
  47. i = (intPage-1) * intPageSize;  
  48. for(j=0;j< i;j++) sqlRst.next(); %> 
  49. < html> 
  50. < head> 
  51. < title>JSP數(shù)據(jù)庫操作例程 - 數(shù)據(jù)分頁顯示 - JDBC-ODBC< /title> 
  52. < /head> 
  53. < body> 
  54. < p align=center>jdbc-odbc留言版< /p> 
  55. < table border="1" cellspacing="0" cellpadding="0" width=600 align=center> 
  56. < %  
  57. //顯示數(shù)據(jù)  
  58. i = 0;  
  59. while(i< intPageSize && sqlRst.next()){ %> 
  60. < tr> 
  61. < td>姓名:< %=sqlRst.getString(1)%>< /td> 
  62. < td>郵件:< %=sqlRst.getString(2)%>< /td> 
  63. < /tr> 
  64. < tr> 
  65. < td colspan=2>< %=sqlRst.getString(3)%>< /td> 
  66. < /tr> 
  67. < % i++; } %> 
  68. < tr> 
  69. < td colspan=2 align=center> 
  70. < %=intPage%>頁 共< %=intPageCount%>頁  
  71. < %if(intPage< intPageCount){%> 
  72. < a href="mssql.jsp?page=< %=intPage+1%>">下一頁< /a>< %  
  73. }  
  74. %> 
  75. < %if(intPage>1){%> 
  76. < a href="mssql.jsp?page=< %=intPage-1%>">上一頁< /a>< %  
  77. }  
  78. %> 
  79. < /td> 
  80. < /tr> 
  81. < /table> < /body> 
  82. < /html> 
  83. < %  
  84. //關(guān)閉結(jié)果集  
  85. sqlRst.close();  
  86. //關(guān)閉SQL語句對(duì)象  
  87. sqlStmt.close();  
  88. //關(guān)閉數(shù)據(jù)庫  
  89. sqlCon.close();  
  90. %> 

如何運(yùn)行JSP數(shù)據(jù)庫操作?

將代碼存為文件test.jsp

Orion Application Server下:

Copy到orion的default-web-app目錄下,通過:

 
 
 
  1. http://localhost:port/test.jsp 

訪問測(cè)試

對(duì)于Resin,Tomcat,JWS等等,都可以運(yùn)行通過。JSP數(shù)據(jù)庫操作到此完成。


本文題目:JSP數(shù)據(jù)庫操作例程(JDBC-ODBC)
文章位置:http://m.5511xx.com/article/djijogj.html