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