编程语言
首页 > 编程语言> > java连接access的用户名、密码异常Decoding not supported解决

java连接access的用户名、密码异常Decoding not supported解决

作者:互联网

Java通过ucanaccessAccess数据库.accdb文件连接:

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;
 }

.accdb文件没设置密码时,可以正常访问:

@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));
}

加密.accdb文件后,在代码设置用户名,密码:

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;
}

再次访问,会错误抛出异常:

UCAExc:::4.0.4 Decoding not supported.  Please choose a CodecProvider which supports reading the current database encoding.

通过官方文档UCanAccess看到

要打开加密的.accdb文件,还需要jackcess-encrypt依赖。所有添加依赖:

<dependency>
    <groupId>com.healthmarketscience.jackcess</groupId>
    <artifactId>jackcess-encrypt</artifactId>
    <version>2.1.4</version>
</dependency>

程序运行,还是抛同样错误。再通过文档、Google搜索等发现,还需要新建类:

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();
    }
}

但该类却没说明怎么去使用,没办法,只能看源码了,最终发现:

可看到,如果有该Keyjackcessopener的,便通过该keyvalue反射获取实例。所以,修改获取连接 :

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;
}

运行程序,不再报错,并获取的数据。

标签:accdb,return,String,supported,Decoding,new,java,public,dbURL
来源: https://www.cnblogs.com/zenghi-home/p/11641050.html