================================

©Copyright 蕃薯耀 2020-01-09

https://www.cnblogs.com/fanshuyao/

使用Ocbc连接是区分电脑是32位还是64位的,需要安装相应的驱动文件,不方便,所以采用第三方的Jar包(UCanAccess)

UCanAccess-4.0.4-bin.zip(自行搜索)

需要的Jar包:

ucanaccess-4.0.4.jar

在Lib文件的jar包:

commons-lang-2.6.jar

commons-logging-1.1.3.jar

hsqldb.jar

jackcess-2.1.11.jar

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import org.apache.log4j.Logger; import com.plan.commons.Row;
import com.plan.commons.RowImpl; public class MdbUtils { private static Logger log = Logger.getLogger(MdbUtils.class);
//odbc方式区分32位和64位系统
/*
private final static String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
private final static String JDBC_URL = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=";
*/ //使用UCanAccess
private final static String JDBC_DRIVER = "net.ucanaccess.jdbc.UcanaccessDriver";
private final static String JDBC_URL = "jdbc:ucanaccess://"; public static void close(ResultSet resultSet, Statement statement, Connection connection){
try {
if(resultSet != null){
resultSet.close();
//log.info("关闭mdb resultSet连接。");
//System.out.println("关闭mdb resultSet连接。");
}
if(statement != null){
statement.close();
//log.info("关闭mdb statement连接。");
//System.out.println("关闭mdb statement连接。");
}
if(connection != null){
connection.close();
//log.info("关闭mdb connection连接。");
//System.out.println("关闭mdb connection连接。");
}
} catch (SQLException e) {
e.printStackTrace();
log.error("关闭mdb连接出错。" + e);
}
} /**
* mdb文件获取连接
* @param absoluteFilePath
* @return
*/
public static Connection getConn(String absoluteFilePath){
log.info("mdb文件路径absoluteFilePath=" + absoluteFilePath); Properties prop = new Properties();
prop.put("charset", "utf-8");//解决中文乱码(GB2312/GBK)
//prop.put("user", "");
//prop.put("password", ""); String url = JDBC_URL + absoluteFilePath;
Connection connection = null;
try {
connection = DriverManager.getConnection(url, prop);
} catch (SQLException e) {
e.printStackTrace();
log.info("mdb文件获取连接出错。Exception=" + e);
}
return connection;
} /**
* 查询mdb文件的表数据
* @param absoluteFilePath mdb文件绝对路径
* @param sql 查询的sql语句
* @return
*/
public static List<Row> read(String absoluteFilePath, String sql){
log.info("mdb文件路径absoluteFilePath=" + absoluteFilePath);
log.info("mdb查询sql=" + sql); List<Row> rowList = new ArrayList<Row>();
Properties prop = new Properties();
prop.put("charset", "utf-8");//解决中文乱码(GB2312/GBK)
//prop.put("user", "");
//prop.put("password", ""); String url = JDBC_URL + absoluteFilePath;
//PreparedStatement preparedStatement = null;
Statement statement = null;
ResultSet resultSet = null;
Connection connection = null;
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(url, prop);
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); while(resultSet.next()){
Row row = new RowImpl();
for(int i=1; i<= resultSetMetaData.getColumnCount(); i++){
String columnName = resultSetMetaData.getColumnName(i);//列名
Object columnValue = resultSet.getObject(i);
row.addColumn(columnName, columnValue);
}
rowList.add(row);
}
}catch (Exception e) {
e.printStackTrace();
log.info("mdb文件读取sql出错。Exception=" + e);
throw new RuntimeException(e);
}finally{
close(resultSet, statement, connection);
}
return rowList;
} /**
* 查询mdb文件的表数据
* @param file File
* @param sql 查询的sql语句
* @return
*/
public static List<Row> read(File file, String sql){
return read(file.getAbsolutePath(), sql);
} /**
* 更新mdb文件的表数据,返回更新的记录数量,0表示没有更新(ID不允许更新)
* @param absoluteFilePath mdb文件绝对路径
* @param sql 查询的sql语句
* @return
*/
public static int update(String absoluteFilePath, String sql){
log.info("mdb文件绝对路径,absoluteFilePath=" + absoluteFilePath);
log.info("mdb文件更新,sql=" + sql); Properties prop = new Properties();
prop.put("charset", "utf-8");//解决中文乱码(GB2312/GBK) String url = JDBC_URL + absoluteFilePath;
Statement statement = null;
Connection connection = null;
int updateSize = 0;
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(url, prop);
statement = connection.createStatement();
updateSize = statement.executeUpdate(sql); }catch (Exception e) {
e.printStackTrace();
log.info("mdb文件更新sql出错。Exception=" + e);
throw new RuntimeException(e);
}finally{
close(null, statement, connection);
}
log.info("mdb更新数量,updateSize=" + updateSize + "。sql="+sql);
return updateSize;
} /**
* 更新mdb文件的表数据,返回更新的记录数量,0表示没有更新
* @param absoluteFilePath mdb文件绝对路径
* @param sql 查询的sql语句
* @return
*/
public static int update(String absoluteFilePath, String sql, List<Object> params){
log.info("mdb文件路径absoluteFilePath=" + absoluteFilePath);
log.info("mdb更新sql=" + sql);
Properties prop = new Properties();
prop.put("charset", "utf-8");//解决中文乱码(GB2312/GBK) String url = JDBC_URL + absoluteFilePath;
PreparedStatement preparedStatement = null;
Connection connection = null;
int updateSize = 0;
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(url, prop);
preparedStatement = connection.prepareStatement(sql);
if(params != null && params.size() > 0){
for(int i=0; i<params.size(); i++){
preparedStatement.setObject(i + 1, params.get(i));
}
}
updateSize = preparedStatement.executeUpdate(); }catch (Exception e) {
e.printStackTrace();
log.info("mdb文件更新sql出错。Exception=" + e);
throw new RuntimeException(e);
}finally{
close(null, preparedStatement, connection);
}
log.info("mdb更新数量,updateSize=" + updateSize + "。sql="+sql);
return updateSize;
} /**
* mdb文件sql执行(如新增、删除字段),成功返回true
* @param absoluteFilePath mdb文件绝对路径
* @param sql 查询的sql语句
* @return
*/
public static boolean execute(String absoluteFilePath, String sql){
log.info("mdb文件绝对路径,absoluteFilePath=" + absoluteFilePath);
log.info("mdb文件sql执行,sql=" + sql); Properties prop = new Properties();
prop.put("charset", "utf-8");//解决中文乱码(GB2312/GBK) String url = JDBC_URL + absoluteFilePath;
Statement statement = null;
Connection connection = null;
boolean result = false;
try{
Class.forName(JDBC_DRIVER);
connection = DriverManager.getConnection(url, prop);
statement = connection.createStatement();
statement.execute(sql);
result = true;
log.info("mdb文件执行sql成功。sql=" + sql);
}catch (Exception e) {
e.printStackTrace();
log.info("mdb文件执行sql出错。Exception=" + e);
throw new RuntimeException(e);
}finally{
close(null, statement, connection);
}
return result;
} public static void main(String[] args) {
/*
String sql = "select * from cu_proj_zxgh_land";
//List<Map<String, Object>> listMap = read("C:/db/test.mdb", sql);
List<Row> rowList = read("C:/db/02-地块划分与指标控制图.mdb", sql);
if(rowList != null && rowList.size() > 0){
System.out.println("=====listMap.size()="+rowList.size());
for (Row row : rowList) {
System.out.println(row.toString());
System.out.println("");
}
}
*/ /*
//更新数据
String sql = "update t_user set age=199 where id=1";
System.out.println(update("C:/db/test.mdb", sql));
*/ //preparedStatement
/*
String sql = "update t_user set age=?,email=? where id=?";
List<Object> params = new ArrayList<Object>();
params.add(99);
params.add("bbb@qq.com");
params.add(1);
System.out.println(update("C:/db/test.mdb", sql, params));
*/ //增加列
/*
String sql = "alter table t_user add column gh_id int";
//String sql = "alter table t_user add column my_id datetime not null default now()";
System.out.println(execute("C:/db/test.mdb", sql));
*/
} }

(如果你觉得文章对你有帮助,欢迎捐赠,^_^,谢谢!)

================================

©Copyright 蕃薯耀 2020-01-09

https://www.cnblogs.com/fanshuyao/

Mdb文件工具类,UCanAccess使用,Access数据库操作的更多相关文章

  1. Microsoft Access数据库操作类(C#)

    博文介绍的Microsoft Access数据库操作类是C#语言的,可实现对Microsoft Access数据库的增删改查询等操作.并且该操作类可实现对图片的存储,博文的最后附上如何将Image图片 ...

  2. C# ACCESS数据库操作类

    这个是针对ACCESS数据库操作的类,同样也是从SQLHELPER提取而来,分页程序的调用可以参考MSSQL那个类的调用,差不多的,只是提取所有记录的数量的时候有多一个参数,这个需要注意一下! usi ...

  3. 自动扫描FTP文件工具类 ScanFtp.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  4. 读取Config文件工具类 PropertiesConfig.java

    package com.util; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io ...

  5. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  6. Android FileUtil(android文件工具类)

    android开发和Java开发差不了多少,也会有许多相同的功能.像本文提到的文件存储,在Java项目和android项目里面用到都是相同的.只是android开发的一些路径做了相应的处理. 下面就是 ...

  7. Java 实现删除文件工具类

    工具代码 package com.wangbo; import java.io.File; /** * 删除目录或文件工具类 * @author wangbo * @date 2017-04-11 1 ...

  8. HTTP 下载文件工具类

    ResponseUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.File; im ...

  9. Java常用工具类---IP工具类、File文件工具类

    package com.jarvis.base.util; import java.io.IOException;import java.io.InputStreamReader;import jav ...

随机推荐

  1. Hibernate(十)--spring整合hibernate

    结构: Spring和Hibernate整合借助于HibernateTemplate applicationContext.xml <?xml version="1.0" e ...

  2. JS: 随机点名程序与万年历

    随机点名程序 document.write(Math.random()); var stu = ["张三", "王五", "张二", &qu ...

  3. BFPRT(中位数的中位数算法)

    BFPRT(中位数的中位数算法) 类似于快排,但是划分区间的策略不一样. 分组,组内排序: 取出每组的中位数组成一个数组,再取这个数组的中位数: 以取出的中位数作为partition的轴.

  4. python假设一段楼梯共 n(n>1)个台阶,小朋友一步最多能上 3 个台阶,那么小朋友上这段楼 梯一共有多少种方法

    我们先把前四节种数算出来(自己想是哪几类,如果你不会算,那就放弃写代码吧,干一些在街上卖肉夹馍的小生意,也挣得不少) 标号 1    2    3     4 种类 1    2    4     7 ...

  5. P1011 A+B 和 C

    转跳点:

  6. POJ 1655:Balancing Act

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10311   Accepted: 4261 De ...

  7. Postman配置Pre-request scripts预请求对请求进行AES加密

    1.首先,Postman的Pre-request scripts页面右边已经提供了一些模板,这些模板可以设置变量与环境变量,并使用双大括号对变量进行引用 {{info}} 2.对所有POST请求都进行 ...

  8. 【pwnable.kr】blackjack

    又一道pwnable nc pwnable.kr 9009 读题找到源代码在:http://cboard.cprogramming.com/c-programming/114023-simple-bl ...

  9. reduce()、filter()、map()、some()、every()、...展开属性

    reduce().filter().map().some().every()....展开属性   这些概念属于es5.es6中的语法,跟react+redux并没有什么联系,我们直接在https:// ...

  10. 从零开始学C++(0 简介)

    2020年,给自己定一个新目标————开始写技术博客,将之前所学的内容重新复习并整理成一系列的文章,一来可以让自己对这些基础知识更加熟悉,二来方便于以后的复习查阅. 以前自己都是以笔记的形式将知识点记 ...