当前位置: 首页 > 工具软件 > UCanAccess > 使用案例 >

使用 ucanaccess 库连接MS Access 数据库

云京
2023-12-01

 类库介绍:

UCanAccess 可以将mdb 文件的数据操作封装为传统的JDBC数据库操作。

mdb 库连接示例代码:

 

        String path = "D:/demo.mdb";
        Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;

		try {
			Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
		} catch(ClassNotFoundException cnfex) {
			LOGGER.error("Problem in loading or registering MS Access JDBC driver");
			cnfex.printStackTrace();
		}

		try {
			String dbURL = "jdbc:ucanaccess://" + path + ";memory=true";

			connection = DriverManager.getConnection(dbURL, null, "168168");
			statement = connection.createStatement();

			String sql = "select a.ID, a.Name from Person a ";
			resultSet = statement.executeQuery(sql);

			while(resultSet.next()) {
                // POJO create
				Result result = new Result(resultSet.getString(1),
						resultSet.getString(2));
			}
		} catch(SQLException sqlex){
			sqlex.printStackTrace();
		} finally {
			try {
				if(null != connection) {
					resultSet.close();
					statement.close();
					connection.close();
				}
			} catch (SQLException sqlex) {
				sqlex.printStackTrace();
			}
		}

参考:

UCanAccess官网:http://ucanaccess.sourceforge.net/site.html

参考示例:https://www.benchresources.net/jdbc-msaccess-database-connection-steps-in-java-8/

 类似资料: