Java 使用 ucanaccess 連接Access數據庫
如果沒有Access的Java驅動包:直接在在 Maven中央倉庫 直接搜索 UCanAccess ,然後直接在項目的 pom.xml 中加入依賴。
<!-- https://mvnrepository.com/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;
}
========》刪除數據結果如下