日期:2014-05-16  浏览次数:21036 次

java 连接加密access 2007
引用
绝对路径

static class MyConnention{
		public static Connection getConnection() throws Exception{
			/**配置数据源后*/
//			String URL="jdbc:odbc:<MS Access Database配置数据源名称>";
//			Connection c=DriverManager.getConnection(URL,"","");
			/**不配置数据源*/
			//jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password]
			String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=f:/data/db1.accdb;pwd=qing";
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			Connection c=DriverManager.getConnection(url1);
			return c;
		}
	}

引用
相对路径   在该类同包目录下建一文件夹放入db1.accdb

static class MyConnention{
		public static Connection getConnection() throws Exception{
/**不配置数据源*/
			//jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password]
			Class theClass=MyConnention.class;
			java.net.URL  u= theClass.getResource("db/db1.accdb");   
            String url=u.getPath().substring(1,u.getPath().length()).replaceAll("%20", " ");

			String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+url+";pwd=qing";
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			Connection c=DriverManager.getConnection(url1);
			return c;
		}
	}

public class StoreSample extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static Connection theConn;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StoreSample() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			try {
				theConn=StoreSample.MyConnention.getConnection();
			} catch (Exception e) {
				System.out.println(e.getMessage());
				e.printStackTrace();
			}
			ResultSet rs;
			Statement stmt;
			String sql="select * from asspay";
			try {
				stmt=theConn.createStatement();
				rs=stmt.executeQuery(sql);
				while (rs.next()) {
					System.out.println(rs.getString("type"));
					
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//response.sendRedirect("/storeRemote.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			doGet(request,response);
	}
	static class MyConnention{
		public static Connection getConnection() throws Exception{
			/**配置数据源后*/
//			String URL="jdbc:odbc:MS Access Database";
//			Connection c=DriverManager.getConnection(URL,"","");
			/**不配置数据源*/
			//jdbc:odbc:DRIVER={Microsoft Access Driver<此处必须有空格 >(*.mdb,<注意此处必须有一空格>*.accdb)};DBQ=<数据库名称路径>[;pwd=password]
			String url1="jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=f:/data/db1.accdb;pwd=qing";
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
			Connection c=DriverManager.getConnection(url1);
			return c;
		}
	}
}

引用
配置ODBC 系统DSN  Xtreme Sample Database 11.5为配置DSN的名称

public class CRSampleDataSourceBean {
	private static ResultSet resultSet = null;
	private static Connection connection = null;
	private static String connectionURL = "jdbc:odbc:Xtreme Sample Database 11.5";

	private  static String databaseClass = "sun.jdbc.odbc.JdbcOdbcDriver";
	private  static String query = "select * from customer";

public