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