java连接access数据库源码

snakeacl

贡献于2013-01-15

字数:0 关键词: Java开发

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 connections = new ArrayList(); 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; } }

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 4 金币 [ 分享文档获得金币 ] 2 人已下载

下载文档

相关文档