JAVA 连接access2010数据库连接

JAVA 连接access2010数据库连接,第1张

JAVA 连接access2010数据库都是固定几种方法,以下是在网上找的一些方法,

示例代码:

import java.sql.*

public class ConnectAccess {

/**

* 注意:

* 1:先建立一个access文件xxx.mdb,并放在制定盘符X:/下

* 2:在数据库文件xxx.mdb中建立一个表Tablexx1;

* 3:为Table1添加一列,并插入至少一条记录;

*/

public static void main(String args[]) throws Exception {

ConnectAccess ca=new ConnectAccess()

ca.ConnectAccessFile()

ca.ConnectAccessDataSource()

}

/**

* 方法一:直接连接access文件。

*/

public void ConnectAccessFile() throws Exception {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

String dbur1 = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)}DBQ=X://xxx.mdb"

Connection conn = DriverManager.getConnection(dbur1, "username", "password")

Statement stmt = conn.createStatement()

ResultSet rs = stmt.executeQuery("select * from Tablexx1")

while (rs.next()) {

System.out.println(rs.getString(1))

}

rs.close()

stmt.close()

conn.close()

}

/**

* 方法二:采用ODBC连接方式

* 在windows下,【开始】-->【控制面板】-->【性能和维护】-->【管理工具】-->【数据源】,在数据源这里添加一个指向xxx.mdb文件的数据源。

* 比如创建名字为xxxxS1

*/

public void ConnectAccessDataSource()throws Exception {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

String dbur1 = "jdbc:odbc:xxxxS1"// 此为ODBC连接方式

Connection conn = DriverManager.getConnection(dbur1, "username", "password")

Statement stmt = conn.createStatement()

ResultSet rs = stmt.executeQuery("select * from Tablexx1")

while (rs.next()) {

System.out.println(rs.getString(1))

}

rs.close()

stmt.close()

conn.close()

}

}

下面是我写的连接access的代码,通过调试

你还有什么问题可以发我邮箱fireflade@163.com

import java.sql.*

public class Sjk{

public void Sjk(){}

public void testMdb()

throws SQLException,ClassNotFoundException

{

String dbUr1="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)}DBQ=d:\\mydata.mdb"

String user=""

String password=""

Statement s

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

Connection c=DriverManager.getConnection(dbUr1,user,password)

s=c.createStatement()

ResultSet r=s.executeQuery(

"SELECT NAME "+

"FROM diqu "+

"WHERE "+

"A='1'"

)

System.out.println("连接成功")

while(r.next()){

System.out.println(

r.getString("NAME"))

}

}

catch(Exception exp)

{

System.out.println(exp.toString())

// result.setText(exp.toString())

}

// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

//Connection c=DriverManager.getConnection(dbUr1,user,password)

//Statement s=c.createStatement()

// s.close()

}

public static void main(String args[])

{

Sjk test=new Sjk()

try{

test.testMdb()

}

catch(Exception exp)

{}

}

}

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

}

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存