java远程访问access数据库
① java杩炴帴access鏁版嵁搴撶殑涓夌嶆柟寮忎互鍙婅繙绋嬭繛鎺ュ疄鐜颁唬镰佸强鏁欑▼
杩炴帴access鏁版嵁搴掳纴缃戜笂镄勫唴瀹瑰緢澶氾纴灏濊瘯镄勮繃绋嬩腑涔熶细阆囧埌钖勭嶉梾棰桡纴娌℃湁鐗瑰埆濂界殑浠嬬粛锛屾墍浠ヨ嚜宸辨兂镐荤粨涓涓嬶纴镞ュ悗澶囩敤銆
杩欓噷闇瑕佹彁鍓嶈存槑涓嬶纴win7绯荤粺32bit鍜64bit榛樿ゅ畨瑁呯殑access閮芥槸32bit镄勶纴浣嗘槸濡傛灉鏄64bit镄勭郴缁熻繛鎺access鏁版嵁搴撴椂闇瑕佸畨瑁64bit镄刼ffice杞浠讹纴涓嬮溃浼氲村埌銆
浠64bit镎崭綔绯荤粺涓哄垪
绗涓绉嶏细鐢佃剳涓婇厤缃缁揿悎java浠g爜瀹炵幇
a.镓揿紑鐩褰旵:WindowsSystem32镓惧埌odbcad32.exe鏂囦欢锛屽弻鍑绘墦寮
b.镣瑰嚮鍙充晶娣诲姞鎸夐挳锛屽湪鍒楄〃涓阃夋嫨access鏁版嵁搴挞┍锷
杩欎釜鍦版柟瑕佹敞镒忥纴浣跨敤绾痡ava瀹炵幇杩炴帴access鏁版嵁搴撶殑镞跺欓渶瑕佸啓access镄勯┍锷锛岃宎ccess镄勯┍锷ㄥ悕绉版槸镙规嵁杩欓噷鍐欑殑锛屼篃灏辨槸浣犳湰链烘湁镄勯┍锷锛岃屼笉鏄缃戜笂璇寸殑浠涔堣佹湁绌烘牸锛岃佹妸*.mdb, *.accdb鎹㈡垚*.mdb鎴栬*.accdb涔嬬被銆傚洜姝よ繖閲岄渶瑕佹敞镒忋傚彟澶栬缮鍙鑳芥湁涓闂棰樸傛湁鍙鑳界偣鍑绘坊锷犵殑镞跺栾繖閲屽苟娌℃湁access椹卞姩锛屾垨钥呮姤阌橻Microsoft][ODBC 椹卞姩绋嫔簭绠$悊鍣╙ 链鍙戠幇鏁版嵁婧愬悕绉板苟涓旀湭鎸囧畾榛樿ら┍锷ㄧ▼搴忔椂锛
杩欎釜镞跺欑绣涓娄笅杞戒竴涓狝ccessDatabaseEngine_X64.exe锛屽傛灉鏄32bit镎崭綔绯荤粺鐩稿簲镄勪笅杞紸ccessDatabaseEngine_X32.exe鍗冲彲,镣瑰嚮涓嬭浇64bit椹卞姩
c.镣瑰嚮瀹屾垚鍑虹幇涓嫔垪瀵硅瘽妗嗭纴鏁版嵁婧愬悕绉伴殢镒忓~鍐欙纴璇存槑鍙链夊彲镞狅纴灏辨槸瀵规暟鎹婧愮殑鎻忚堪锛岀偣鍑婚夋嫨鎸夐挳阃夋嫨鏁版嵁搴撴墍鍦ㄧ殑璺寰
d.镣瑰嚮纭瀹氭寜阍钖庡氨鍙浠ュ湪ODBC鏁版嵁婧愮$悊鍣ㄤ腑鐪嫔埌娣诲姞镄勬暟鎹婧
e.涓嬮溃鏄痡ava镄勪唬镰
package com.xu.bean;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectAccessDatabase {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:databaseName";//databaseName灏辨槸鍒氩垰娣诲姞镄勬暟鎹婧愬悕绉
Connection con = DriverManager.getConnection(url, "", "");//娌℃湁鐢ㄦ埛钖嶅拰瀵嗙爜镄勬椂鍊欑洿鎺ヤ负绌
Statement sta = con.createStatement();
ResultSet rst = sta.executeQuery("select * from demoTable");//demoTable涓篴ccess鏁版嵁搴扑腑镄勪竴涓琛ㄥ悕
if(rst.next()){
System.out.println(rst.getString("name"));
//瑙e喅涔辩爜闂棰
// System.out.println(new String(rst.getBytes("name"), "gbk"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} /*catch (UnsupportedEncodingException e) {
e.printStackTrace();
}*/
}
}
铹跺悗杩愯屼唬镰佸嵆鍙
绗浜岀嶆柟寮忥细绾痡iava瀹炵幇
a.杩欓噷闇瑕佹敞镒忕殑灏辨槸url闾i噷锛屽墠闱涔熸彁鍒拌繃鏄镙规嵁鐢佃剳链鍦版湁镄勯┍锷ㄥ啓镄
package com.xu.bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 绾痡ava浠g爜瀹炵幇 杩炴帴access鏁版嵁搴
* @author xuyl
*
*/
public class ConnectAccessDatabase2 {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=E:\demo.mdb";
Connection con = DriverManager.getConnection(url, "", "");//娌℃湁鐢ㄦ埛钖嶅拰瀵嗙爜镄勬椂鍊欑洿鎺ヤ负绌
Statement sta = con.createStatement();
ResultSet rst = sta.executeQuery("select * from demoTable");//demoTable涓篴ccess鏁版嵁搴扑腑镄勪竴涓琛ㄥ悕
if(rst.next()){
System.out.println("绾痡ava浠g爜瀹炵幇:" + rst.getString("name"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
b.杩愯岀粨鏋
绗涓夌嶆柟寮忥细浣跨敤RmiJdbc.jar锛堜篃鏄杩灭▼杩炴帴镄勬柟娉曪级浠ヨ繙绋嬭繛鎺ヤ负渚
棣栧厛涓嬭浇RmiJdbc.jar
鍦ㄦ斁缃瓵ccess鏁版嵁搴撶殑鐢佃剳涓婇厤缃
1.閰岖疆RmiJDbc.jar鐜澧冨彉閲忥纸鍦ㄨ繖涔嫔墠杩橀渶瑕侀厤缃甹dk鐜澧冿纴jdk閰岖疆鍙镊琛岀栌搴︼级
瑙e帇缂㏑miJdbc3.3.zip锛屽皢瑙e帇鍑虹殑鏂囦欢鏀惧湪浠绘剰鐩樼︺傚彸阌璁$畻链洪夋嫨灞炴 - 阃夋嫨楂樼骇绯荤粺璁剧疆 - 阃夋嫨鐜澧冨彉閲 - 鍦ㄧ郴缁熷彉閲忎腑镓惧埌CLASSPATH阃変腑镣瑰嚮缂栬緫鎸夐挳鍦ㄦ渶钖庨溃娣诲姞%CLASSPATH%;D:RmiJdbc3.3RmiJdbcdistlibRmiJdbc.jar锛堜互鏀惧湪d鐩樿窡鐩褰曚负渚嬶级
2.钖锷≧miJbdc.jar链嶅姟
win + r 寤烘墦寮杩愯岀獥鍙h緭鍏cmd镓揿紑锻戒护绐楀彛杈揿叆
java -jar D:RmiJdbc3.3RmiJdbcdistlibRmiJdbc.jar鍑虹幇涓嫔浘镓绀哄嵆閰岖疆鎴愬姛銆
钖锷ㄤ箣钖庨渶瑕佷竴鐩磋繍琛屻
3.閰岖疆鏁版嵁婧
镙规嵁涓婇溃镄勬柟娉曢厤缃濂芥暟鎹婧
4.java浠g爜
public static String executeQuery(String sql) {
log.info("===========executeQuery start=============");
String jsonArray = "{";
StringBuilder jsonObject = new StringBuilder();
Connection con = null;
Statement stmt = null;
ResultSet rst = null;
try{
//娉ㄥ唽鏁版嵁鎻愪緵绋嫔簭
Class.forName("org.objectweb.rmijdbc.Driver").newInstance();
//accessDatabase涓鸿繙绋嬫湇锷″櫒鏁版嵁婧愬悕绉
// String url = "jdbc:rmi://10.23.16.83/jdbc:odbc:accessDatabase";
//鐢熶骇鍦板潃
String url = "jdbc:rmi://10.23.10.6/jdbc:odbc:accessDatabase";
// String url = "jdbc:rmi://10.23.71.211/jdbc:odbc:demo";
//寤虹珛杩炴帴
con = DriverManager.getConnection(url, "", "");
stmt = con.createStatement();
rst = stmt.executeQuery(sql);
if(rst != null){
while(rst.next()){
String id = rst.getInt("id") + "";
String name = new String(rst.getBytes("title"), "gbk");
String time = rst.getDate("adate").toString();
String filePath = rst.getString("url");
jsonObject.append("{"ID":"" + id + "", "NAME":"" + name + "", "FILE_PATH":"" + filePath + "", "TIME":"" + time + ""}, ");
}
String temp = jsonObject.toString();
temp = temp.substring(0, temp.length() - 2);
jsonArray += ""IS_EMPTY":"N",LIST:[" + temp + "]}";
}else{
jsonArray += ""IS_EMPTY":"Y"}";
}
}catch(Exception e){
log.info("===========executeQuery start=============" + e.getMessage());
}finally{
try {
con.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
log.info("===========executeQuery end=============");
return jsonArray;
}
浠ヤ笂灏辨槸涓夌嶈块梾access鏁版嵁搴撶殑鏂规硶
② java怎么直接连接access数据库
通过jdbc直接连。驱动是com.hxtt.sql.access.AccessDriver
在http://www.hxtt.com/access.zip可以下到,
还需要数据库方言com.hxtt.support.hibernate.HxttAccessDialect
数据库方言包在http://www.hxtt.com/test/hibernate.jar可以下到。
配置的时候使用。
DRIVER=com.hxtt.sql.access.AccessDriver
URL=jdbc:access:///D:\workspace\database\fireDB.mdb
USERNAME=admin
PASSWORD=
注意linux下的\是/,刚开始导入的url是/。害我想了半天才明白他是linux。
改成\了以后就可以了,还要注意在java字符串里\要写成转译字符'\\'
其他没问题了。这样就可以使用hibernate自动生成代码了。还是满方便的。
发个模板。
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//注册驱动
Connection conn = null; //创建链接
conn = DriverManager.getConnection("jdbc:odbc:FFFF", "admin", "");
Statement st;
st=conn.createStatement(); //创建句柄
ResultSet rs=st.executeQuery("select lamp_address,lamp_text from lamp as model where model.lamp_type='S';");//得到结果集
while (rs.next())
{
al1.add(rs.getString(1));
al2.add(rs.getString(2));
}
st.execute(sb.toString());
把这个模板改一改,用新的URL和类就行了。
还有一种是ODBC连接的,我比较推荐的方法
可以去我的空间里找相应的文章
③ 如何用java 远程连接 access数据库
package com.jiuzi.connection;
import java.sql.*;
public class ConnectAccess {
/**
* 初学者请注意:
* 1:先建立一个access文件a1.mdb,并放在D:\下;
* 2:在数据库文件a1.mdb中建立一个表Table1;
* 3:为Table1添加一列,并插入至少一条记录;
* 4:本文是一个完整的类,直接拿去运行就可以。
*/
public static void main(String args[]) throws Exception {
ConnectAccess ca=new ConnectAccess();
ca.ConnectAccessFile();
// ca.ConnectAccessDataSource();
}
public void ConnectAccessFile() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/**
* 直接连接access文件。
*/
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver
(*.mdb)};DBQ=d:\\hongloumeng.mdb";
Connection conn = DriverManager.getConnection(dbur1, "username", "password");
Statement stmt = conn.createStatement();
String sql="select * from dream where 序号=1";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String text=rs.getString(4);
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
System.out.println(text);
}
rs.close();
stmt.close();
conn.close();
}
public void ConnectAccessDataSource()throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
/**
* 采用ODBC连接方式 如何建立ODBC连接?
* 答:在windows下,【开始】->【控制面板】->【性能和维护】->【管理工具】->【数
据源】,在数据源这里添加一个指向a1.mdb文件的数据源。
* 比如创建名字为dataS1
*/
String dbur1 = "jdbc:odbc:dataS1";// 此为ODBC连接方式
Connection conn = DriverManager.getConnection(dbur1, "username", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from Table1");
while (rs.next()) {System.out.println(rs.getString(1));
}
rs.close();
stmt.close();
conn.close();
}
}
利用连接池(以下是一个连接池)
package com.jiuzi.connection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class ConnectionPool {
private List<ConnectionDesc> connections = new ArrayList<ConnectionDesc>();
private static final int MIN_CONNECTIONS = 2;
private static final int MAX_CONNECTIONS = 10;
private static ConnectionPool connectionPool = null;
static {
try {
connectionPool = new ConnectionPool();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "数据库连接错误:"+
("jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\hongloumeng.mdb"+
e.getMessage()));
System.exit(-1);
System.err.println();
}
}
private ConnectionPool() throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
for(int i = 0; i < MIN_CONNECTIONS; ++i) {
this.createNewConnection();
}
}
public static ConnectionPool getInstance() {
return connectionPool;
}
public static void main(String [] args) throws Exception {
final ConnectionPool pool = ConnectionPool.getInstance();
for(int i = 0; i < 5; ++i) {
new Thread() {
public void run() {try {
Connection conn = pool.getConnection();
Thread.sleep(5000);
pool.releaseConnection(conn);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
public synchronized Connection getConnection() throws Exception {
ConnectionDesc cd = null;
for (int i = 0; i < connections.size(); i++) {
cd = connections.get(i);
if (cd.isIdle()) {
cd.setState(true);
System.out.println("Connection No." + cd.getNo() + " has been
Occupied.");
return cd.getConn();
}
}
cd = createNewConnection();
cd.setState(true);
return cd.getConn();
}
public synchronized void releaseConnection(Connection conn) {
for (int i = 0; i < this.connections.size(); i++) {
ConnectionDesc cd = this.connections.get(i);
if (cd.getConn() == conn) {
cd.setState(false);
System.out.println("Connection No." + cd.getNo() + " has been
released.");
}
}
}
public synchronized ConnectionDesc createNewConnection()
throws Exception {
if (this.connections.size() < MAX_CONNECTIONS) {
Connection conn =
DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver
(*.mdb)};DBQ=db\\hongloumeng.mdb",
"username", "password");
ConnectionDesc cd = new ConnectionDesc(conn);
this.connections.add(cd);
cd.setState(false);
return cd;
}
System.err.println("Too many Connection with DB:" + connections.size());
if(JOptionPane.OK_OPTION==JOptionPane.showConfirmDialog(null, "E00101:太多连接
了,对其连接复位吗?")){
resetConnection();
}
return null;
}
public void resetConnection(){
for(ConnectionDesc desc:connections){
if(!desc.isIdle()){
desc.setState(false);
}
}
}
}
class ConnectionDesc {
private Connection conn;
//state 指的是此连接是否被使用: true 为是,false 为不是
private boolean state;
private int no;
private static int count = 0;
ConnectionDesc(Connection conn) {
this.conn = conn;
this.no = count++;
System.out.println("Connection No." + this.no + " has been created.");
}
public void setState(boolean state) {
this.state = state;
}
public boolean isIdle() {
return !state;
}
public int getNo() {
return this.no;
}
public Connection getConn() {
return conn;
}
}
④ Java如何连接access数据库
1 创建数据源
控制面板--性能与维护--管理工具--数据源或者控制面板--ODBC,
在ODBC数据源管理器中选择“用户DSN”,选择添加,将出现选择安装数据源驱动程序的对话框。选择Microsoft Access Driver(*.mdb)并完成。将弹出“创建数据源”对话框。
为创建的数据源取名,例如sun。选择数据库也就是Access文件的存放地址(最好不要存在中文)。如果要设置login name或者password则选择高级,不需要直接确定。
2 建立JDBC-ODBC桥接器
建立桥接器:
try{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//(sun为数据源的名字,见1)}
catch(ClassNotFoundException e){}
3 连接到数据库
使用java.sql包中的Connection类声明的一个对象,使用DriverManager调用它的静态方法getConnection创建对象
try{Connection con=DriverManager.getConnection("jdbc:odbc:数据源名字","login name","password");//(如果不存在login name 和password,则使用双引号表示即可"")}
catch(SQLException e){}
即可完成于access的连接。
希望对你有用,望采纳!
⑤ java 用rmi 远程连接access 数据库,但总报错,请高人指点。
java.lang.NoClassDefFoundError
未找到类
估计是Class.forName("org.objectweb.rmijdbc.Driver").newInstance();
找不到org.objectweb.rmijdbc.Driver