如何用java 远程连接 access数据库

如何用java 远程连接 access数据库,第1张

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 = 0i <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 = 0i <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 = 0i <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 = 0i <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

}

}

装完系统,配置完IIS,发现.NET程序报503错误,出错后连接池自动关闭

这个程序是需要连接access数据库的,打开系统日志发现错误如下:

错误应用程序名称: w3wp.exe,版本: 7.5.7601.17514,时间戳: 0x4ce7afa2

错误模块名称: mscorwks.dll,版本: 2.0.50727.5420,时间戳: 0x4ca2b7e1

另外ASP程序也会报错:

IIS7报错,提示ADODB.Connection 错误 '800a0e7a' 未找到提供程序。该程序可能未正确安装

ASP连接数据库的连接字符串是Provider=Microsoft.Jet.OLEDB.4.0Data Source=路径,采用的是OleDB方式连接,问题可能就出在OleDB方式连接这一环节上。

后来搜索网络得到了解决的办法 。原来是自己装的64位Windows 7系统的原因,默认64位环境下,IIS应用程序池未启用32位应用程序,我们只需要启用一下就可以了。打开IIS 7,定位到“应用程序池”,然后选择使用OleDB方式连接数据库的程序池,然后将启用32位应用程序设置为True就可以了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6762623.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存