Analog phone book system, New: Backup Data and restore backup

Source: Internet
Author: User

Added two new features on top of the original system to back up data and restore backups:

Here are the specific implementations of the two features:

 Public voidSaveAll ()//back up all data{List<Person> List1 =NewArrayList (); Try {            //Load Database driverClass.forName ("Oracle.jdbc.driver.OracleDriver"); //get a connection to a databaseConnection conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:orcl", "text0816",                    "666666"); //get the declaration of the Operational DatabaseStatement st =conn.createstatement (); String SQL= "SELECT * FROM Person"; ResultSet RS=st.executequery (SQL);  while(Rs.next ()) {//instantiating the person classPerson per =NewPerson (); //adding information to the Per objectPer.setid (Rs.getint ("id")); Per.setname (Rs.getstring ("Name")); Per.setage (Rs.getint ("Age")); Per.setsex (Rs.getstring ("Sex")); Per.settelnum (Rs.getlong ("Telnum")); Per.setaddress (Rs.getstring ("Address")); //add a Per object to the list collectionList1.add (per); } File File=NewFile ("E:/dianhuabu"); if(!file.exists ())            {File.mkdirs (); } ObjectOutputStream out=NewObjectOutputStream (NewFileOutputStream ("E:/dianhuabu/oldmessage.obj"));            Out.writeobject (List1);                        Out.close (); System.out.println ("Data Save Complete ..."); //System.out.println (cards); //Freeing ResourcesRs.close ();            St.close ();        Conn.close (); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); }    }
 Public voidRecoverall ()//Restore Backup    {        Try{ObjectInputStream in=NewObjectInputStream (NewFileInputStream ("E:/dianhuabu/oldmessage.obj")); List<Person> List1 = (list<person>) In.readobject (); //Load Database driverClass.forName ("Oracle.jdbc.driver.OracleDriver"); //get a connection to a databaseConnection conn = Drivermanager.getconnection ("Jdbc:oracle:thin: @localhost: 1521:orcl", "text0816",                    "666666"); //get the declaration of the Operational DatabaseStatement st =conn.createstatement ();  for(person p:list1) {String SQL= "INSERT into person values ('" +p.getid () + "', '" +p.getname () + "', '" +p.getage ()+ "', '" +p.getsex () + "', '" +p.gettelnum () + "', '" +p.getaddress () + "')";                        St.executeupdate (SQL); } System.out.println ("Recovery backup succeeded!" "); //Freeing ResourcesSt.close ();        Conn.close (); } Catch(Exception e) {//TODO Auto-generated catch blockE.printstacktrace (); }    }

The test results are as follows:

Back up all the data first:

Then delete all the data;

At this point, all the data in the database has been emptied

Finally, we restore the backup:

Now look at the data in the database;

Analog phone book system, New: Backup Data and restore backup

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.