佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > JSP連接Access數(shù)據(jù)庫

JSP連接Access數(shù)據(jù)庫

熱門標(biāo)簽:科大訊飛語音識(shí)別系統(tǒng) 團(tuán)購網(wǎng)站 Mysql連接數(shù)設(shè)置 阿里云 銀行業(yè)務(wù) 服務(wù)器配置 電子圍欄 Linux服務(wù)器
一.建立數(shù)據(jù)庫及ODBC數(shù)據(jù)源

  1.建立jcc.mdb數(shù)據(jù)庫及user表     
 ?。?添加測(cè)試數(shù)據(jù)
  3.配置ODBC數(shù)據(jù)源

二.在%wwwroot%>/下,新建Access數(shù)據(jù)庫連接文件Select.jsp

  Select.jsp源碼如下:


%@page contentType="text/html;charset=gb2312"%>
%@page import="java.sql.*"%>
html>
body>
%
try{
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
  out.print(e);
}
try{
  String url = "jdbc:odbc:jcc";
  Connection conn = DriverManager.getConnection(url,"jcc","jsp.com.cn");
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("Select * FROM user");
  out.println("User-list"+"br>");
  while(rs.next()){
    out.print(rs.getString(1)+" ");
    out.print(rs.getString(2)+"br>");
  }
  rs.close();
  stmt.close();
  conn.close();
}
catch(Exception ex){
  out.print(ex);
}
%>
/body>
/html>



四.運(yùn)行http://localhost/Select.jsp,顯示結(jié)果如下:

User-list
1 Corebit
2 Ivan

  則表示數(shù)據(jù)庫連接成功!恭喜!恭喜!

  否則請(qǐng)檢查數(shù)據(jù)源相關(guān)設(shè)置,出錯(cuò)可能性比較高!

附言:

  常有人問起,如何在不做ODBC數(shù)據(jù)源的情況下讓JSP訪問Access數(shù)據(jù)庫,為解開這個(gè)迷團(tuán),特寫以下連接代碼,以供參考!其中,jcc.mdb與Select.jsp同位于%wwwroot%>(根目錄)下。

  改寫后的Select.jsp源碼如下:


%@page contentType="text/html;charset=gb2312"%>
%@page import="java.sql.*"%>
html>
body>
%
try{
  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException e){
  out.print(e);
}
try{
  String strDirPath=application.getRealPath(request.getRequestURI());
  strDirPath=strDirPath.substring(0,strDirPath.lastIndexOf('\\'))+"\\";
  String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ="+strDirPath+"jcc.mdb";
  Connection conn = DriverManager.getConnection(url);
  Statement stmt = conn.createStatement();
  ResultSet rs = stmt.executeQuery("Select * FROM user");
  out.println("User-list"+"br>");
  while(rs.next()){
    out.print(rs.getString(1)+" ");
    out.print(rs.getString(2)+"br>");
  }
  rs.close();
  stmt.close();
  conn.close();
}
catch(Exception ex){
  out.print(ex);
}
%>
/body>
/html>



  運(yùn)行結(jié)果應(yīng)該與使用ODBC時(shí)的運(yùn)行結(jié)果相同!

  *注:文件名Select.jsp區(qū)分大小寫!

  希望本文能對(duì)你的JSP連接Access數(shù)據(jù)庫有所幫助!
==========================================
只能使用jdbc-odbc橋來連接   
  想要設(shè)置odbc數(shù)據(jù)源   
  然后連接   
  String     dbdriver   =   "oracle.jdbc.driver.OracleDriver";   
                  String     dbname   =   "jdbc:oracle:thin:@192.168.0.101:1521:orcl";//根據(jù)你的情況修改   
                  String     user   =   "system";//用戶名   
                  String   password   =   "manager";//密碼   
                  Connection   conn   =   null;   
                  Statement   stmt   =   null;   
                  ResultSet   rs   =null;   
  String   sql="select   *   from   表名";//根據(jù)實(shí)際情況修改   
  try   
                              {   
                                      Class.forName(dbdriver);   
                              }   
                              catch(java.lang.ClassNotFoundException   e){   
                                          System.err.println("Class   access_dbconnect   not   fount!"+e.getMessage());   
                                        }   
  conn=DriverManager.getConnection(dbname,user,password);   
                                      Statement   stmt=conn.createStatement();   
                                      rs=stmt.executeQuery(sql); 
=========================================
sDBDriver   =   "sun.jdbc.odbc.JdbcOdbcDriver";   
                  sConnStr   =   "jdbc:odbc:odbc名稱";   
                  conn   =   null;   
                  rs   =   null;   
                  try   
                  {   
                          Class.forName(sDBDriver);   
                  }   
                          conn   =   DriverManager.getConnection(sConnStr);   
                          Statement   statement   =   conn.createStatement();   
                          rs   =   statement.executeQuery(s);   
  你在odbc數(shù)據(jù)源中建一個(gè)access連接,然后把上面的代碼中的odbc名稱改成你的odbc數(shù)據(jù)源連接名稱就可以了。
您可能感興趣的文章:
  • JSP中操作數(shù)據(jù)庫的常用SQL標(biāo)簽用法總結(jié)
  • jsp從數(shù)據(jù)庫獲取數(shù)據(jù)填充下拉框?qū)崿F(xiàn)二級(jí)聯(lián)動(dòng)菜單的方法
  • JSP中使用JDBC訪問SQL Server 2008數(shù)據(jù)庫示例
  • Java實(shí)現(xiàn)JSP在Servelt中連接Oracle數(shù)據(jù)庫的方法
  • jsp讀取數(shù)據(jù)庫實(shí)現(xiàn)分頁技術(shù)簡析
  • jsp 從web.xml讀取連接數(shù)據(jù)庫的參數(shù)
  • JSP連接MySql/MS SQL Server/Oracle數(shù)據(jù)庫連接方法[整理]
  • Jsp連接Access數(shù)據(jù)庫(不通過建立ODBC數(shù)據(jù)源的方法)
  • 利用asp或jsp,flash怎樣把數(shù)據(jù)庫中的一張表中的所有記錄讀取并顯示出來
  • JSP數(shù)據(jù)庫操數(shù)據(jù)分頁顯示
  • 在JSP中訪問Oracle數(shù)據(jù)庫
  • 如何使用JSP訪問MySQL數(shù)據(jù)庫
  • JSP中的PreparedStatement對(duì)象操作數(shù)據(jù)庫的使用教程

標(biāo)簽:大理 江蘇 萍鄉(xiāng) 廣元 棗莊 衢州 蚌埠 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《JSP連接Access數(shù)據(jù)庫》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    资兴市| 家居| 察雅县| 湘潭市| 云梦县| 徐汇区| 天镇县| 玉溪市| 方正县| 鸡泽县| 蒲城县| 平原县| 托克托县| 益阳市| 元江| 凤台县| 天门市| 越西县| 格尔木市| 新巴尔虎左旗| 墨脱县| 农安县| 榆树市| 枞阳县| 盐山县| 油尖旺区| 华池县| 平安县| 揭西县| 仙桃市| 吉林省| 黔西| 雷山县| 定南县| 伊宁市| 万源市| 新河县| 永城市| 水城县| 宁安市| 台前县|