JAva接続暗号化アクセス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;
		}
	}

参照
相対パス   このクラスの同パッケージディレクトリの下にフォルダを作成してdb 1.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 static void main(String[] args) {
		CreateDat cd=new CreateDat();
		try {
			ResultSet rs=cd.getResultSet();
			while(rs.next()){
				System.out.println(rs.getString("Customer Name"));
			}
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
		
	}
	
	static class CreateDat{
		public CreateDat() {
			try {
				// Ensure database class exists
				Class.forName(databaseClass);
				// Create a connection
				connection = DriverManager.getConnection(connectionURL, "", "");
			} catch (ClassNotFoundException ex) {
				System.out
						.println("Ensure that database driver class is installed.");
				ex.printStackTrace();
			} catch (SQLException ex) {
				System.out.println("SQL Exception #" + ex.getErrorCode() + " : "
						+ ex.getLocalizedMessage());
				ex.printStackTrace();
			}
		}

		/*
		 * Any function that returns a java.sql.ResultSet within the Java class can
		 * be used in Crystal Reports to return report data. The function can also
		 * be parameterized to 'feed' in specific values to the function when
		 * querying.
		 */
		public ResultSet getResultSet() throws SQLException {
			// Create an SQL statement to execute
			Statement statement = connection.createStatement(
					ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			// Execute the select statement to populate the ResultSet
			resultSet = statement.executeQuery(query);
			return resultSet;
		}
	}