首页 数据库 mysql教程 java连接access数据库说明以及示例代码

java连接access数据库说明以及示例代码

Jun 07, 2016 pm 03:43 PM
access java 代码 数据库 示例 说明 连接

之前 我链接access的时候 经常出现odbc源驱动器的错误 后来我才发现 其实是这样的 要 连接 Access数据源,首先要建立一个JDBC-ODBC桥接器,其方法是: Class . forName ( sun.jdbc.odbc.JdbcOdbcDriver ); Class 是 Java.lang 包中的一个类,通过调用它的静态

 

 

之前 我链接access的时候 经常出现odbc源驱动器的错误

 

后来我才发现 其实是这样的

连接Access数据源,首先要建立一个JDBC-ODBC桥接器,其方法是:

<span>Class</span><span>.</span><span>forName</span><span>(</span><span>"sun.jdbc.odbc.JdbcOdbcDriver"</span><span>);</span>
登录后复制

ClassJava.lang包中的一个类,通过调用它的静态方法forName就可以建立桥接器。但建立桥接器时可能发生异常,这是我们所不想见到的,因此要捕获这个异常:

try{<span>Class</span><span>.</span><span>forName</span><span>(</span><span>"sun.jdbc.odbc.JdbcOdbcDriver"</span><span>);</span>}catch(ClassNotFoundException e){异常捕获}
登录后复制

然后就是连接数据库了,这里要用到java.sql包中的Connection类声明一个对象,再用类DriverManager调用它的静态方法getConnection创建这个连接

Connection con=DriverManager.getConnection("jdbc:odbc:数据源名字","数据源用户名","数据源密码");//注意 这里是数据源的名字 用户名和密码都需要配置。try{<span>Class</span><span>.</span><span>forName</span><span>(</span><span>"sun.jdbc.odbc.JdbcOdbcDriver"</span><span>);</span>}catch(ClassNotFoundException e){System.out.println(""+e);}  
   // 下面一句是重点String path =this.getClass().getClassLoader().getResource("db1.mdb").getPath().substring(1);//db1.mdb为创建的Access数据库文件//此数据库文件路径与java文件在同一路径下String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+path;Connection conn=DriverManager.getConnection(url);Statement st = conn.createStatement();
登录后复制

实例

<span>package</span><span> com</span><span>.neusoft</span><span>.</span><span>common</span><span>;</span><span>import</span><span> java</span><span>.</span><span>sql</span><span>.</span><span>Connection</span><span>;</span><span>import</span><span> java</span><span>.</span><span>sql</span><span>.</span><span>PreparedStatement</span><span>;</span><span>import</span><span> java</span><span>.</span><span>sql</span><span>.</span><span>ResultSet</span><span>;</span><span>import</span><span> java</span><span>.</span><span>sql</span><span>.</span><span>SQLException</span><span>;</span><span>import</span><span> java</span><span>.</span><span>sql</span><span>.</span><span>Statement</span><span>;</span><span>import</span><span> com</span><span>.</span><span>mchange</span><span>.</span><span>v2</span><span>.</span><span>c3p0</span><span>.</span><span>ComboPooledDataSource</span><span>;</span><span>/**
 * c3p0 的 jdbc<strong>连接</strong>池
 * 
 * @author 
 * 
 */</span><span>public</span><span>class</span><span>DBConnectionPool</span><span>{</span><span>protected</span><span>DBConnectionPool</span><span>()</span><span>{</span><span>}</span><span>private</span><span>static</span><span>ComboPooledDataSource</span><span> ds </span><span>=</span><span>null</span><span>;</span><span>static</span><span>{</span><span>try</span><span>{</span><span>
            ds </span><span>=</span><span>new</span><span>ComboPooledDataSource</span><span>();</span><span>// 设置jdbc的Driver类</span><span>
            ds</span><span>.</span><span>setDriverClass</span><span>(</span><span>"sun.jdbc.odbc.JdbcOdbcDriver"</span><span>);</span><span>// 设置jdbc的url</span><span>
            ds
                    </span><span>.</span><span>setJdbcUrl</span><span>(</span><span>"jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=H:/gyt_web/Database/SiteWeaver.mdb"</span><span>);</span><span>//            // 设置<strong>数据库</strong>的登录用户名</span><span>//            ds.setUser("admin");</span><span>//            // 设置<strong>数据库</strong>的登录用户名</span><span>//            ds.setPassword("admin");</span><span>// 设置<strong>连接</strong>池的最大<strong>连接</strong>数</span><span>
            ds</span><span>.</span><span>setMaxPoolSize</span><span>(</span><span>200</span><span>);</span><span>// 设置<strong>连接</strong>池的最小<strong>连接</strong>数</span><span>
            ds</span><span>.</span><span>setMinPoolSize</span><span>(</span><span>20</span><span>);</span><span>}</span><span>catch</span><span>(</span><span>Exception</span><span> e</span><span>)</span><span>{</span><span>
            e</span><span>.</span><span>printStackTrace</span><span>();</span><span>}</span><span>}</span><span>protected</span><span>static</span><span>synchronized</span><span>Connection</span><span> getConnection</span><span>()</span><span>{</span><span>Connection</span><span> con </span><span>=</span><span>null</span><span>;</span><span>try</span><span>{</span><span>
            con </span><span>=</span><span> ds</span><span>.</span><span>getConnection</span><span>();</span><span>}</span><span>catch</span><span>(</span><span>SQLException</span><span> e</span><span>)</span><span>{</span><span>
            e</span><span>.</span><span>printStackTrace</span><span>();</span><span>}</span><span>return</span><span> con</span><span>;</span><span>}</span><span>/**
     * 查询通用方法
     * 
     * @param sql
     * @param params
     * @return
     */</span><span>public</span><span>ResultSet</span><span> query</span><span>(</span><span>String</span><span> sql</span><span>,</span><span>Object</span><span>[]</span><span>params</span><span>)</span><span>{</span><span>Connection</span><span> conn </span><span>=</span><span>null</span><span>;</span><span>PreparedStatement</span><span> ps</span><span>教程</span><span>tmt </span><span>=</span><span>null</span><span>;</span><span>ResultSet</span><span> rs </span><span>=</span><span>null</span><span>;</span><span>
        conn </span><span>=</span><span>this</span><span>.</span><span>getConnection</span><span>();</span><span>try</span><span>{</span><span>
            pstmt </span><span>=</span><span> conn</span><span>.</span><span>prepareStatement</span><span>(</span><span>sql</span><span>);</span><span>if</span><span>(</span><span>params</span><span>!=</span><span>null</span><span>)</span><span>{</span><span>for</span><span>(</span><span>int</span><span> i </span><span>=</span><span>0</span><span>;</span><span> i </span><span><span>params</span><span>.</span><span>length</span><span>;</span><span> i</span><span>++)</span><span>{</span><span>
                    pstmt</span><span>.</span><span>setObject</span><span>(</span><span>i </span><span>+</span><span>1</span><span>,</span><span>params</span><span>[</span><span>i</span><span>]);</span><span>}</span><span>}</span><span>
            rs </span><span>=</span><span> pstmt</span><span>.</span><span>executeQuery</span><span>();</span><span>}</span><span>catch</span><span>(</span><span>SQLException</span><span> e</span><span>)</span><span>{</span><span>// TODO Auto-generated catch block</span><span>
            e</span><span>.</span><span>printStackTrace</span><span>();</span><span>}</span><span>return</span><span> rs</span><span>;</span><span>}</span><span>/**
     * 修改 增加 删除通用方法
     * 
     * @param sql
     * @param params
     * @return
     */</span><span>public</span><span>int</span><span> executeSQL</span><span>(</span><span>String</span><span> sql</span><span>,</span><span>Object</span><span>[]</span><span>params</span><span>)</span><span>{</span><span>Connection</span><span> conn </span><span>=</span><span>null</span><span>;</span><span>PreparedStatement</span><span> pstmt </span><span>=</span><span>null</span><span>;</span><span>int</span><span> result </span><span>=</span><span>0</span><span>;</span><span>
        conn </span><span>=</span><span>this</span><span>.</span><span>getConnection</span><span>();</span><span>try</span><span>{</span><span>
            pstmt </span><span>=</span><span> conn</span><span>.</span><span>prepareStatement</span><span>(</span><span>sql</span><span>);</span><span>if</span><span>(</span><span>params</span><span>!=</span><span>null</span><span>)</span><span>{</span><span>for</span><span>(</span><span>int</span><span> i </span><span>=</span><span>0</span><span>;</span><span> i </span><span><span>params</span><span>.</span><span>length</span><span>;</span><span> i</span><span>++)</span><span>{</span><span>
                    pstmt</span><span>.</span><span>setObject</span><span>(</span><span>i </span><span>+</span><span>1</span><span>,</span><span>params</span><span>[</span><span>i</span><span>]);</span><span>}</span><span>}</span><span>
            result </span><span>=</span><span> pstmt</span><span>.</span><span>executeUpdate</span><span>();</span><span>}</span><span>catch</span><span>(</span><span>SQLException</span><span> e</span><span>)</span><span>{</span><span>// TODO Auto-generated catch block</span><span>
            e</span><span>.</span><span>printStackTrace</span><span>();</span><span>}</span><span>return</span><span> result</span><span>;</span><span>}</span><span>}</span></span></span>
登录后复制

java连接access实例:

<span>import</span><span> java</span><span>.</span><span>sql</span><span>.*;</span><span>public</span><span>class</span><span>ConnectAccess</span><span>{</span><span>/** 
* 初学者请注意: 
* 1:先建立一个access文件a1.mdb,并放在D:下; 
* 2:在<strong>数据库</strong>文件a1.mdb中建立一个表Table1; 
* 3:为Table1添加一列,并插入至少一条记录; 
* 4:本文是一个完整的类,直接拿去运行就可以。 
*/</span><span>public</span><span>static</span><span>void</span><span> main</span><span>(</span><span>String</span><span> args</span><span>[])</span><span>throws</span><span>Exception</span><span>{</span><span>ConnectAccess</span><span> ca</span><span>=</span><span>new</span><span>ConnectAccess</span><span>();</span><span> 
  ca</span><span>.</span><span>ConnectAccessFile</span><span>();</span><span> 
  ca</span><span>.</span><span>ConnectAccessDataSource</span><span>();</span><span>}</span><span>public</span><span>void</span><span>ConnectAccessFile</span><span>()</span><span>throws</span><span>Exception</span><span>{</span><span>Class</span><span>.</span><span>forName</span><span>(</span><span>""</span><span>sun</span><span>.</span><span>jdbc</span><span>.</span><span>odbc</span><span>.</span><span>JdbcOdbcDriver</span><span>""</span><span>);</span><span>/** 
  * 直接<strong>连接</strong>access文件。 
  */</span><span>String</span><span> dbur1 </span><span>=</span><span>""</span><span>jdbc</span><span>:</span><span>odbc</span><span>:</span><span>driver</span><span>={</span><span>Microsoft</span><span>Access</span><span>Driver</span><span>(*.</span><span>mdb</span><span>)};</span><span>DBQ</span><span>=</span><span>d</span><span>:</span><span>a1</span><span>.</span><span>mdb</span><span>""</span><span>;</span><span>Connection</span><span> conn </span><span>=</span><span>DriverManager</span><span>.</span><span>getConnection</span><span>(</span><span>dbur1</span><span>,</span><span>""</span><span>username</span><span>""</span><span>,</span><span>""</span><span>password</span><span>""</span><span>);</span><span>Statement</span><span> stmt </span><span>=</span><span> conn</span><span>.</span><span>createStatement</span><span>();</span><span>ResultSet</span><span> rs </span><span>=</span><span> stmt</span><span>.</span><span>executeQuery</span><span>(</span><span>""</span><span>select</span><span>*</span><span>from</span><span>Table1</span><span>""</span><span>);</span><span>while</span><span>(</span><span>rs</span><span>.</span><span>next</span><span>())</span><span>{</span><span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>rs</span><span>.</span><span>getString</span><span>(</span><span>1</span><span>));</span><span>}</span><span> 
  rs</span><span>.</span><span>close</span><span>();</span><span> 
  stmt</span><span>.</span><span>close</span><span>();</span><span> 
  conn</span><span>.</span><span>close</span><span>();</span><span>}</span>
登录后复制

注意:

JDBC能完成与一个数据库建立连接,然后向数据库发送SQL语句,再处理数据库返回的结果。

JDBC在设计上和ODBC相似。JDBC和数据库建立连接的一种方式是首先建立起一个JDBC-ODBC桥接器。

首先安装office2000,就有了Access2000,启动Access,建立新数据库mess.mdb,建立包含两个文本型字段name和sex的表,表名为member,随便录入一些字符串到member表中,保存在任意目录下,比如说C盘下。然后打开控制面板 -> 管理工具 -> 数据源 (ODBC),选择"系统DSN",也可以选择"用户DSN"选项卡,系统的DSN对每个用户都适用可避免调试时出现的权限问题,单击“添加”,

点击"完成",弹出的对话框里将"数据源名"一项设为你喜欢的名字,比如redsun。点"选择",找到"C:mess.mdb"文件,如此一个ODBC数据源就配置好了

 

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

deepseek怎么转换pdf deepseek怎么转换pdf Feb 19, 2025 pm 05:24 PM

DeepSeek 无法直接将文件转换为 PDF。根据文件类型,可以使用不同方法:常见文档(Word、Excel、PowerPoint):使用微软 Office、LibreOffice 等软件导出为 PDF。图片:使用图片查看器或图像处理软件保存为 PDF。网页:使用浏览器“打印成 PDF”功能或专用的网页转 PDF 工具。不常见格式:找到合适的转换器,将其转换为 PDF。选择合适的工具并根据实际情况制定方案至关重要。

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

如何在Node.js环境中解决第三方接口返回403的问题? 如何在Node.js环境中解决第三方接口返回403的问题? Mar 31, 2025 pm 11:27 PM

在Node.js环境中解决第三方接口返回403的问题当我们在使用Node.js调用第三方接口时,有时会遇到接口返回403错误�...

创造未来:面向零基础的 Java 编程 创造未来:面向零基础的 Java 编程 Oct 13, 2024 pm 01:32 PM

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。

Java程序查找胶囊的体积 Java程序查找胶囊的体积 Feb 07, 2025 am 11:37 AM

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Laravel Redis连接共享:为何select方法会影响其他连接? Laravel Redis连接共享:为何select方法会影响其他连接? Apr 01, 2025 am 07:45 AM

Laravel框架中Redis连接的共享与select方法的影响在使用Laravel框架和Redis时,开发者可能会遇到一个问题:通过配置...

Java 变得简单:编程能力的初学者指南 Java 变得简单:编程能力的初学者指南 Oct 11, 2024 pm 06:30 PM

JavaMadeSimple:ABeginner'sGuidetoProgrammingPower简介Java是一种强大的编程语言,广泛应用于从移动应用程序到企业级系统的各种领域。对于初学者来说,Java的语法简洁易懂,是学习编程的理想选择。基本语法Java使用基于类的面向对象编程范式。类是将相关数据和行为组织在一起的模板。以下是一个简单的Java类示例:publicclassPerson{privateStringname;privateintage;

如何在Spring Tool Suite中运行第一个春季启动应用程序? 如何在Spring Tool Suite中运行第一个春季启动应用程序? Feb 07, 2025 pm 12:11 PM

Spring Boot简化了可靠,可扩展和生产就绪的Java应用的创建,从而彻底改变了Java开发。 它的“惯例惯例”方法(春季生态系统固有的惯例),最小化手动设置

See all articles