By Java ucanaccess
on the Access
database .accdb
file connection:
public static Connection getConn() {
try {
String dbURL = "jdbc:ucanaccess://" +
"C:\\Users\\f1334185\\Documents\\数据库4.accdb";
return DriverManager.getConnection(dbURL);
} catch (Exception e) {
System.out.println("AccessDB connection fail");
e.printStackTrace();
}
return null;
}
In .accdb
a file did not set a password, you can access:
@Autowired
private ObjectMapper objectMapper;
@Test
public void contextLoads() throws SQLException, JsonProcessingException {
Connection conn = dbUtil.getConn();
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery("select * from tb_user");
ResultSetMetaData metaData = resultSet.getMetaData();
int columnCount = metaData.getColumnCount();
List<Map<String, Object>> list = new ArrayList<>();
while (resultSet.next()){
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < columnCount; i++) {
String columnName = metaData.getColumnName(i + 1);
Object object = resultSet.getObject(columnCount);
map.put(columnName, object);
}
list.add(map);
}
System.out.println(objectMapper.writeValueAsString(list));
}
Encrypt .accdb
the file, set the user name and password in the code:
public static Connection getConn() {
try {
String dbURL = "jdbc:ucanaccess://" +
"C:\\Users\\f1334185\\Documents\\数据库4.accdb";
return DriverManager.getConnection(dbURL, "aa", "aa");
} catch (Exception e) {
System.out.println("AccessDB connection fail");
e.printStackTrace();
}
return null;
}
Visit again, it will throw an exception error:
UCAExc:::4.0.4 Decoding not supported. Please choose a CodecProvider which supports reading the current database encoding.
By official document UCanAccess
seen
To open an encrypted .accdb
file, also you need to jackcess-encrypt
rely on. All add-dependent:
<dependency>
<groupId>com.healthmarketscience.jackcess</groupId>
<artifactId>jackcess-encrypt</artifactId>
<version>2.1.4</version>
</dependency>
The program is running, or throwing the same error. Through the document, Google search and other discovery, also need to create a new category:
public class JackcessOpener implements JackcessOpenerInterface {
@Override
public Database open(File file, String s) throws IOException {
DatabaseBuilder builder = new DatabaseBuilder(file);
builder.setAutoSync(false);
builder.setCodecProvider(new CryptCodecProvider(s));
builder.setReadOnly(false);
return builder.open();
}
}
But did not explain how to use this class, no way, only to see the source code, and eventually found:
It can be seen that if there Key
is jackcessopener
, they pass the key
of value
acquiring instance reflected. So, get connected to modify:
public static Connection getConn() {
try {
String dbURL = "jdbc:ucanaccess://" +
"C:\\Users\\f1334185\\Documents\\数据库4.accdb";
Properties pro = new Properties();
pro.put("user", "aa");
pro.put("password", "aa");
pro.put("jackcessopener", "com.example.demo.JackcessOpener");
return DriverManager.getConnection(dbURL, pro);
} catch (Exception e) {
System.out.println("AccessDB connection fail");
e.printStackTrace();
}
return null;
}
Run the program, no error, and obtain the data.