1: Do not use the WINDOW Data Source Creation Method to connect directly to the Access database in the JAVA code. Note: jdbc: odbc: driver {MicrosoftAccessDriver (*. mdb)}; this field is null and cannot be missing! Otherwise, an exception occurs. try {Stringurljdbc: odbc: driver {MicrosoftAccessDriver (*. mdb)}; DBQF: ge123
1: Do not use the WINDOW Data Source Creation Method to connect directly to the Access database in the JAVA code. Note: jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; this field is null and cannot be missing! Otherwise, an exception occurs. try {String url = jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; DBQ = F: // ge // 123.
1: Do not use WINDOW to create a data sourceMethod
Directly in the JAVA code and AccessDatabaseConnection
Note: jdbc: odbc: driver = {Microsoft Access Driver (*. mdb)}; there are no spaces here! Otherwise, an exception occurs.
try{String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=F://ge//123.MDB";Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");Connection conn = DriverManager.getConnection(url);System.out.println(conn);Statement sta = conn.createStatement();ResultSet resu = sta.executeQuery("select * from Roche_position");if(resu.next()){System.out.println(resu.getString("cname"));}System.out.println("exit");}catch(Exception e){e.printStackTrace();}
2: Use ODBC to create a WINDOW Data SourceMethod:
Start
> Control Panel
> Management tools
> Data source (ODBC)
> User DSN or system DSN
> Click to add
> Find "Microsoft Access Driver (*. mdb)", select, and click Finish> Custom Data Source Name to create a new or existing AccessDatabase(. Mdb) other options are optional.
> OK
The data source has been created successfully.
The following is the JAVA codeConnectionNow:
Try {Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver"); Connection conn = DriverManager. getConnection ("jdbc: odbc: Data Source Name ","Database"," User Password (if any) ";" System. out. println (conn); Statement sta = conn. createStatement (); ResultSet resu = sta.exe cuteQuery ("select * from Roche_position"); if (resu. next () {System. out. println (resu. getString ("cname");} System. out. println ("exit");} catch (Exception e) {e. printStackTrace ();}}