Java reads data from the ACCESS database and inserts it into the javadb database table

Source: Internet
Author: User

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 ());
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.