Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » 技术文章库  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 不用JDBC:ODBC bridge直接操作Access 数据库
scottding

十三部落酋长

CJSDN高级会员


发贴: 1054
积分: 101
于 2003-01-24 09:53 user profilesend a private message to usersend email to scottdingsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
(转载)

/*
用JDBC来连接数据库有很两种方式,如
<1>JDBC:ODBC bridge, <2>直接连接方式.
第二种方式有很多好处,它可以独立于依赖于系统的odbc数据源,存储数据的database
可以自由地移动,例如对个人站点的数据库更具有优势.这里我谈谈平时自己用这种方式 开发的感受.希望对大家有所帮助.
运行环境:Win2k advance server(English version),
JSDK 1.4.1
Microsoft Access Database(Office XP系列)
*/

import java.io.*;
import java.sql.*;

public class MobileDatabase
{
public static void main(String[] args)
{
try{
//Load JDBC driver
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

/*这里的数据库的url一定要写正确,这是关键,其中DBQ可以绝对路径,也可以是相对路径,为了体现数据存储路径的/独立性,你可以将数据库copy到不同的位试一下*/
String dbUrl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=access\\test.mdb";

Connection con = DriverManager.getConnection(dbUrl,"","");
Statement state = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//执行SQL语句
String sql = "select * from address";
ResultSet rs = state.executeQuery(sql);

//打印测试
if( rs != null ){
while( rs.next() ){
System.out.println("\t" + rs.getString("name"));
}
rs.close();
con.close();
}
}
catch(Exception e){
e.printStackTrace();
}
}

}//End class MobileDatabase

附:数据库test.mdb的结构设计:
字段 类型 长度(字节)
-----------------------------
name text 8
sex bool (系统指定)
age int (系统指定)
email text 20
-----------------------------
自已加几条记录到里面就可以测出结果了.



作者 Re:不用JDBC:ODBC bridge直接操作Access 数据库 [Re:scottding]
software_young



发贴: 0
积分: 0
于 2003-02-06 12:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
嘿嘿,也许是我愚昧,不过sun.jdbc.odbc.JdbcOdbcDriver就是JDBC:ODBC bridge。这算什么直接连接?

software_young edited on 2003-03-23 23:51


作者 Re:不用JDBC:ODBC bridge直接操作Access 数据库 [Re:scottding]
floater

Java Jedi

总版主


发贴: 3233
积分: 421
于 2003-02-06 22:47 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
You use this jdbc:odbc bridge, but bypass the odbc driver(made by ms, in your control panel), just go directly to the mdb file. Otherwise, you do this:
jdbc:odbc --> odbc driver --> mdb file.



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
- Martin Fowler, Refactoring - Improving the Design of Existing Code

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923