java connection access user name, password abnormal Decoding not supported solve

By Java ucanaccesson the Accessdatabase .accdbfile 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 .accdba 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 .accdbthe 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 UCanAccessseen

To open an encrypted .accdbfile, also you need to jackcess-encryptrely 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 Keyis jackcessopener, they pass the keyof valueacquiring 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.

Guess you like

Origin www.cnblogs.com/zenghi-home/p/11641050.html