如何用java 远程连接 access数据库
3个回答
展开全部
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;
}
}
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;
}
}
追问
我不要本地连接,也就是说:有两台主机在不同的地方,我用一台主机访问另一台主机中的access 数据库
展开全部
1.
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\hongloumeng.mdb";
DBQ后面换成 \\IP\xxx.mdb
2.建立一个远程共享目录的映射,然后用
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Z:\\hongloumeng.mdb";
当本地磁盘用。
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=d:\\hongloumeng.mdb";
DBQ后面换成 \\IP\xxx.mdb
2.建立一个远程共享目录的映射,然后用
String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=Z:\\hongloumeng.mdb";
当本地磁盘用。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你把数据库连接地址改成远程的地址就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询