How does Java operate access databases? Please refer to the following example: [if you do not know why, an error is reported when you insert the code, so you can paste it directly]
Package com. Ria. utils. Common;
Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. resultset;
Import java. SQL. resultsetmetadata;
Import java. SQL. statement;
Import java. util. arraylist;
Import java. util. hashmap;
Import java. util. List;
Import java. util. Map;
Public class msaccessdbutils {
Private Static msaccessdbutils instance = NULL;
Public static msaccessdbutils getinstance (){
If (instance = NULL ){
Instance = new msaccessdbutils ();
}
Return instance;
}
Private msaccessdbutils (){}
// Mdbfile = D:/xxx. mdb is called first
Public void loadconfig (string mdbfile, string user, string psw) throws exception {
Mdb_file = mdbfile;
User = user;
Pwd = psw;
Loaddriver ();
}
Public void loadconfig (string mdbfile) throws exception {
Mdb_file = mdbfile;
Loaddriver ();
}
Private Static string dirverclass = "Sun. JDBC. ODBC. jdbcodbcdriver ";
// JDBC: ODBC: Driver = {Microsoft Access driver *. mdb)}; DBQ = northwind. MDB
// JDBC: ODBC: Driver = {Microsoft Access Driver (*. mdb)}; DBQ = C: // test. MDB
Private Static string url =
"JDBC: ODBC: Driver = {Microsoft Access Driver (*. mdb)}; DBQ = ";
Private Static string mdb_file = NULL; // Access File [absolute path here]
Private Static string user = "";
Private Static string Pwd = "";
Private Static connection conn;
Private Static statement stmt;
Private Static resultset RS;
Private void loaddriver () throws exception {
Try {
Class. forname (dirverclass );
} Catch (exception e ){
Throw E;
}
}
// Create a connection that cannot be rolled
Public static void connect () throws exception {
Try {
System. Out. println (URL + mdb_file );
System. Out. println (User );
System. Out. println (PWD );
Conn = drivermanager. getconnection (URL + mdb_file, user, PWD );
Stmt = conn. createstatement ();
} Catch (exception e ){
Throw E;
}
}
Public static void connect (Boolean autocommit) throws exception {
Try {
Conn = drivermanager. getconnection (URL + mdb_file, user, PWD );
Conn. setautocommit (autocommit );
Stmt = conn. createstatement ();
} Catch (exception e ){
Throw E;
}
}
// Create a rolling connection
Public static void connect2 () throws exception {
Try {
Conn = drivermanager. getconnection (URL + mdb_file, user, PWD );
Stmt = conn. createstatement (resultset. type_scroll_insensitive,
Resultset. concur_read_only );
} Catch (exception e ){
Throw E;
}
}
// Close the connection
Public static void close () throws exception {
Try {
If (RS! = NULL ){
Rs. Close ();
Rs = NULL;
}
If (stmt! = NULL ){
Stmt. Close ();
Stmt = NULL;
}
If (Conn! = NULL ){
Conn. Close ();
Conn = NULL;
}
} Catch (exception e ){
Throw E;
}
}
// Query statement
Public static list executequery (string SQL) throws exception {
List L = new arraylist ();
Try {
If (stmt = NULL ){
Connect ();
}
Rs = stmt.exe cutequery (SQL );
L = orgresultset4list (RS );
// Close ();
} Catch (exception e ){
Throw E;
}
Return L;
}
Public static int executeupdate (string SQL) throws exception {
Try {
If (stmt = NULL ){
Connect ();
}
Int res = stmt.exe cuteupdate (SQL );
// Close ();
Return res;
} Catch (exception e ){
Throw E;
}
}
Public static int [] executeupdate (string [] SQL) throws exception {
Try {
If (stmt = NULL ){
Connect (false );
}
For (INT I = 0; I <SQL. length; I ++ ){
Stmt. addbatch (SQL [I]);
}
Int [] res = stmt.exe cutebatch ();
Conn. Commit ();
// Close ();
Return res;
} Catch (exception e ){
Throw E;
}
}
Private Static list orgresultset4list (resultset RS) throws exception {
Try {
Resultsetmetadata rsmd = Rs. getmetadata ();
Int Cols = rsmd. getcolumncount ();
List L = new arraylist ();
Map recordmap = NULL;
While (Rs. Next ()){
Recordmap = new hashmap ();
For (INT I = 0; I <Cols; I ++ ){
Recordmap. Put (string) (rsmd. getcolumnname (I + 1). tolowercase (), RS. GetObject (I + 1 ));
}
L. Add (recordmap );
}
Return L;
}
Catch (exception ex ){
// Ex. printstacktrace ();
Throw ex;
}
}
Public static void main (string [] ARGs ){
If (ARGs! = NULL & args. length> 0 ){
System. Out. println ("ARGs [0] =" + ARGs [0]);
Try {
Msaccessdbutils. getinstance (). loadconfig (ARGs [0], "", "");
} Catch (exception ex ){
Ex. printstacktrace ();
System. Exit (-1 );
}
Msaccessdbutils. getinstance (). Go ();
} Else {
System. Exit (0 );
}
}
Void go (){
Try {
For (int K = 0; k <3; k ++ ){
String SQL =
"Insert into leave_words (cmp_name, tell, web_url, ADDR, mail, conts, notes) values ";
SQL + =
"('Company name 2', '123456', 'HTTP: // sdfa.com ', 'third place for Beijing Big SDF chopper, 'aa @ aa.com', 'Doing well ', '[none]') ";
Int res = msaccessdbutils.getinstance(cmd.exe cuteupdate (SQL );
System. Out. println ("insert =" + Res );
SQL = "select * From leave_words ";
List L = msaccessdbutils.getinstance(cmd.exe cutequery (SQL );
Msaccessdbutils. getinstance (). Close ();
If (L! = NULL ){
System. Out. println ("=" + (L. Get (0). tostring ());
Java. Text. simpledateformat df =
New java. Text. simpledateformat ("yyyy-mm-dd hh: mm: SS w e ");
Map rowmap;
For (INT I = 0; I <L. Size (); I ++ ){
Rowmap = (MAP) L. Get (I );
Java. util. Date crt_time =
New java. util. Date (Java. SQL. timestamp) rowmap. Get (
"Crt_time"). gettime ());
System. Out. println (DF. Format (crt_time ));
}
// Java. SQL. Date d = new java. SQL. Date ();
}
}
} Catch (exception ex ){
Ex. printstacktrace ();
System. Exit (-1 );
}
}
}