A piece of code implements the function of reading table data from the Access database, and then inserting all the data into the javadb database table in sequence, similar to copying.
Import java. SQL .*;
String SQL _insert = NULL; // define SQL _insert as a global variable to print the SQL _insert content in the catch part when an error occurs, which is helpful for debugging.
Resultset rs = NULL;
Try {
Class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
String url = "JDBC: ODBC: Driver = {Microsoft Access driver" +
"(*. Mdb, *. accdb)}; DBQ = path to database \ database1.accdb ";// Enter the actual address in red.
Connection con = drivermanager. getconnection (URL );
System. Out. println ("connected! ");
Statement stmt = NULL;
// SQL query command
String SQL = "select * from data ";
Stmt = con. createstatement ();
// Stmt.exe cute (SQL );
Rs = stmt.exe cutequery (SQL );
String driver = "org. Apache. Derby. JDBC. clientdriver"; // in Derby. Jar
// String dbname = "embeddeddb ";
String dburl = "JDBC: Derby: // localhost: 1527/passwords; territory = zh_cn; user = admin; Password = 123456"; // Create = true indicates that a database is created when it does not exist.
Try {
Class. forname (driver );
Connection conn = drivermanager. getconnection (dburl); // start the Embedded Database
Statement ST = conn. createstatement ();
While (Rs. Next ()){
Byte [] BTS = NULL;
String des = NULL;
String usname = NULL;
String psw = NULL;
String tips = NULL;
String Other = NULL;
String id = new string (Rs. getbytes ("ID"), "GBK ");
Int id = integer. parseint (ID );
BTS = Rs. getbytes ("Description ");
If (BTS! = NULL)
Des = new string (BTS, "GBK ");
BTS = Rs. getbytes ("username ");
If (BTS! = NULL)
Usname = new string (BTS, "GBK ");
BTS = Rs. getbytes ("password ");
If (BTS! = NULL)
Psw = new string (BTS, "GBK ");
BTS = Rs. getbytes ("Tips ");
If (BTS! = NULL)
Tips = new string (BTS, "GBK ");
BTS = Rs. getbytes ("Others ");
If (BTS! = NULL)
Other = new string (BTS, "GBK ");
SQL _insert = "insert into app. psw (ID, description, username, password, tips, others) Values
("+ ID +", '"+ des +"', '"+ usname +"', '"+ psw +"', '"+ tips + "', '"+ other + "')";
St.exe cuteupdate (SQL _insert); // insert a data entry
}
} Catch (exception e ){
System. Out. println (SQL _insert );
E. printstacktrace ();
}
Con. Close ();
} Catch (sqlexception e ){
System. Out. println ("SQL exception:" + E. tostring ());
} Catch (classnotfoundexception CE ){
System. Out. println ("class not found exception:" +
Ce. tostring ());
}