Java 使用 ucanaccess 连接Access数据库

    • 1 创建 Java 项目
    • 2 创建 Access 文件
    • 3 写测试代码
    • 4 获取 Access 数据库的连接对象
    • 5 查询测试
    • 6 增加测试
    • 7 修改测试
    • 8 删除测试
    • 9 最终结果

如果没有Access的Java驱动包:直接在在 Maven中央仓库 直接搜索 UCanAccess ,然后直接在项目的 pom.xml 中加入依赖。

<!-- https://mvnrepository/artifact/net.sf.ucanaccess/ucanaccess -->
<dependency>
    <groupId>net.sf.ucanaccess</groupId>
    <artifactId>ucanaccess</artifactId>
    <version>4.0.4</version>
</dependency>

1 创建 Java 项目

1 使用 IDE (Eclipse 或者 Intellij 等) 创建一个 Java 项目,在项目中添加 UCanAccess 的驱动jar包,或者直接在项目的 pom.xml 中加入依赖。
2 Access 数据库的操作方式和 Oracle、MySQL 的操作方式一样(如果你想使用JDBC封装的操作可以参考 JDBC封装 )。

2 创建 Access 文件

3 写测试代码

3 下面是我写的用于测试的代码,如果你在项目中使用 Access 作为数据库亦或者只是学习尝试 Access 数据库,你可以在操作数据库时尝试使用预编译(PreparedStatement)来提升数据的安全性。

package com.xu.access;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

/**  
 * 
 * @Title: Access.java   
 * @Package: com.xu.access   
 * @Description: TODO   
 * @author: xuhyacinth     
 * @date: 2019年1月24日 12:47:12   
 * @version: V-1.0.0 
 * @Copyright: 2019 xuhyacinth
 *
 */
public class Access {

	/**
	 * Access数据库Connection
	 */
	private Connection connection;

	static {
		try {
			Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");//加载ucanaccess驱动
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
	}

	/**
	 * Java 获取 Access 数据库连接(Connection)
	 * <table border="1" cellpadding="8"> 
	 * <tr><td colspan="2" align="center">Java 获取  Access 数据库连接(Connection) 注释 </td></tr> 
	 * <tr><th>输入参数</th><th>参数解释</th></tr> 
	 * <tr><td align="center">path</td><td>Access文件的相对或者绝对路径(支持*.mdb和*.accdb数据库文件)</td></tr> 
	 * <tr><td align="center">user</td><td>用户账号(如果没有就写"")</td></tr> 
	 * <tr><td align="center">pwd</td><td>密码密码(如果没有就写"")</td></tr> 
	 * </table>
	 * @param path Access文件的相对或者绝对路径(支持*.mdb和*.accdb数据库文件)
	 * @param user 用户账号(如果没有就写"")
	 * @param pwd  密码密码(如果没有就写"")
	 * @return: Access 的 Connection
	 * @date: 2019年1月24日 12:47:12
	 */
	public Connection getAccessConnection(String path, String user, String pwd) {
		try {
			//获取Access数据库连接(Connection)
			this.connection = DriverManager.getConnection("jdbc:ucanaccess://" + path, user, pwd);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
		return this.connection;
	}

	public static void main(String[] args) throws Exception {
		Access access=new Access();
		Connection connection = access.getAccessConnection("F:\\Access\\test.accdb", "", "");
		access.select(connection);
	}

	/**
	 * Access插入(使用了预编译)
	 *
	 * @param connection 连接
	 * @return 受影响的行数
	 * @throws Exception 异常
	 * @date: 2019年1月24日 12:47:12
	 */
	public int insert(Connection connection) throws Exception {
		// ? 是 JDBC 预编译的占位符
		PreparedStatement statement=connection.prepareStatement("insert into student(id,name,address,age) values(?,?,?,?)");
		statement.setInt(0, 1);//学生编号
		statement.setString(1, "赵六");//学生姓名
		statement.setString(2, "湖南省、衡阳市、珠晖区1");//学生住址
		statement.setInt(3, 20);//学生年龄
		int result = statement.executeUpdate();
		statement.close();
		connection.close();
		return result;
	}

	/**
	 * Access删除
	 *
	 * @param connection 连接
	 * @return 受影响的行数
	 * @throws Exception 异常
	 * @date: 2019年1月24日 12:47:12
	 */
	public int delete(Connection connection) throws Exception {
		Statement statement = connection.createStatement();
		int result = statement.executeUpdate("delete from student where id=3");
		statement.close();
		connection.close();
		return result;
	}

	/**
	 * Access更新
	 *
	 * @param connection 连接
	 * @return 受影响的行数
	 * @throws Exception 异常
	 * @date: 2019年1月24日 12:47:12
	 */
	public int update(Connection connection) throws Exception {
		Statement statement = connection.createStatement();
		int result = statement.executeUpdate("update student set address='湖南省、衡阳市、珠晖区' where id=1");
		statement.close();
		connection.close();
		return result;
	}

	/**
	 * Access查询
	 *
	 * @param connection 连接
	 * @throws Exception 异常
	 * @date: 2019年1月24日 12:47:12
	 */
	public void select(Connection connection) throws Exception {
		Statement statement = connection.createStatement();
		ResultSet result = statement.executeQuery("select * from student");
		while (result.next()) {
			System.out.print(result.getString("id") + "\t");
			System.out.print(result.getString("name") + "\t");
			System.out.print(result.getString("address") + "\t");
			System.out.print(result.getString("age") + "\t");
			System.out.print(result.getString("birthday") + "\t");
			System.out.println();
		}
		statement.close();
		connection.close();
	}

}


4 获取 Access 数据库的连接对象

4 如何获取 Access 数据库的连接,我已经将获取Access数据库的连接封装成为一个方法,你可以直接通过调用这个方法获取Access数据库的连接。

5 查询测试

/**
 * Access查询
 *
 * @param connection 连接
 * @throws Exception 异常
 * @date: 2019年1月21日 下午8:47:15
 */
public void select(Connection connection) throws Exception {
	Statement statement = connection.createStatement();
	ResultSet result = statement.executeQuery("select * from student");
	while (result.next()) {
		System.out.print(result.getString("id") + "\t");
		System.out.print(result.getString("name") + "\t");
		System.out.print(result.getString("address") + "\t");
		System.out.print(result.getString("age") + "\t");
		System.out.print(result.getString("birthday") + "\t");
		System.out.println();
	}
	statement.close();
	connection.close();
}

========》查询数据结果如下

6 增加测试

/**
 * Access插入(使用了预编译)
 *
 * @param connection 连接
 * @return 受影响的行数
 * @throws Exception 异常
 * @date: 2019年1月24日 12:47:12
 */
public int insert(Connection connection) throws Exception {
	// ? 是 JDBC 预编译的占位符
	PreparedStatement statement=connection.prepareStatement("insert into student(id,name,address,age) values(?,?,?,?)");
	statement.setInt(0, 1);//学生编号
	statement.setString(1, "赵六");//学生姓名
	statement.setString(2, "湖南省、衡阳市、珠晖区1");//学生住址
	statement.setInt(3, 20);//学生年龄
	int result = statement.executeUpdate();
	statement.close();
	connection.close();
	return result;
}

========》增加数据结果如下

7 修改测试

/**
 * Access更新
 *
 * @param connection 连接
 * @return 受影响的行数
 * @throws Exception 异常
 * @date: 2019年1月21日 下午8:47:15
 */
public int update(Connection connection) throws Exception {
    Statement statement = connection.createStatement();
    int result = statement.executeUpdate("update student set address='广东省、深圳市、南山区' where id=1");
    statement.close();
    connection.close();
    return result;
}

========》修改数据结果如下

8 删除测试

/**
 * Access删除
 *
 * @param connection 连接
 * @return 受影响的行数
 * @throws Exception 异常
 * @date: 2019年1月21日 下午8:47:15
 */
public int delete(Connection connection) throws Exception {
    Statement statement = connection.createStatement();
    int result = statement.executeUpdate("delete from student where id=3");
    statement.close();
    connection.close();
    return result;
}

========》删除数据结果如下

9 最终结果

更多推荐

Java 使用 ucanaccess 连接Access数据库