java操作access
* Created on 2008-12-31
* Copyright 2008 by Springs Design Inc. All Rights Reserved
*
*/package net.xfok.db;
import java.sql.*;
/**
* @author YangHua
* 转载请注明出处:http://www.xfok.net/2009/10/134509.html
*/
public class DBAccess {
public Connection conn=null;
/**
* 连接未加密的数据库
* @param dbPath
* @return
* @throws Exception
*/
public Statement getStatement(String dbPath) throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};" +
"DBQ="+dbPath;// 此为NO-DSN方式
// String dburl ="jdbc:odbc:odbcName";//此为ODBC连接方式
conn = DriverManager.getConnection(dburl);
return conn.createStatement();
}
/**
* 连接加密的数据库
* @param dbPath
* @return
* @throws Exception
*/
public Statement getStatement(String dbPath,String password) throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dburl = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};" +
"pwd="+password+";DBQ="+dbPath;// 此为NO-DSN方式
// String dburl ="jdbc:odbc:odbcName";//此为ODBC连接方式
conn = DriverManager.getConnection(dburl);
return conn.createStatement();
}
/**
* 查询数据库
* @param stmt
* @param query
* @return
* @throws Exception
*/
public ResultSet executeQuery(Statement stmt,String query) throws Exception{
ResultSet rs=stmt.executeQuery(query);
return rs;
}
/**
* 更新数据库
* @param stmt
* @param query
* @throws SQLException
*/
public void executeUpdate(Statement stmt,String query) throws SQLException{
stmt.executeUpdate(query);
}
/**
* 关闭链接
* @throws SQLException
*/
public void close() throws SQLException{
if(conn!=null)
conn.close();
}
public static void main(String args[]) throws Exception {
DBAccess db=new DBAccess();
String dbPath="D:\work\workTime\iCCard.mdb";
String query="select top 20 * from t_b_Consumer";
String password="168168";
Statement stmt=db.getStatement(dbPath, password);
ResultSet rs=db.executeQuery(stmt, query);
while(rs.next()){
System.out.println(rs.getString(1) + " " + rs.getString(2) + " "
+ rs.getString(3) + " " + rs.getString(4) + " "
+ rs.getString(5));
}
}
}
补充:软件开发 , Java ,