Home >Database >Mysql Tutorial >java连接access数据库说明以及示例代码

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

WBOY
WBOYOriginal
2016-06-07 15:43:511254browse

之前 我链接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数据源就配置好了

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn