📄 databasetest.java
字号:
/*
* DatabaseTest.java E.L. 2001-08-26
*/
import javax.swing.*;
import java.util.*;
import myLibrary.*; // Person and Database
class DatabaseTest {
public static void main(String[] args) throws Exception {
String userName = JOptionPane.showInputDialog("User Name: ");
String password = JOptionPane.showInputDialog("Password: ");
Database db = new Database(userName, password);
System.out.println("Get all:");
Person aPerson = null;
ArrayList all = db.getAll();
for (int i = 0; i < all.size(); i++) {
aPerson = (Person) all.get(i);
System.out.println(aPerson);
}
System.out.println("Will store Ann Morris in the database:");
Person thePerson = db.registerNewPerson("Ann", "Morris");
int idNo = thePerson.getPersIdent();
System.out.println("She got ident. no.: " + idNo);
System.out.println("All the persons, after Ann Morris are stored:");
all = db.getAll();
for (int i = 0; i < all.size(); i++) {
aPerson = (Person) all.get(i);
System.out.println(aPerson);
}
System.out.println("Updates her first name from Ann to Greta:");
thePerson.setFirstName("Greta");
if (db.updateName(thePerson)) System.out.println("Her first name is updated.");
else System.out.println("Not possible to update the first name");
System.out.println("Updates her last name from Morris to Bull:");
thePerson.setLastName("Bull");
if (db.updateName(thePerson)) System.out.println("Her last name is updated");
else System.out.println("Not possible to update the last name");
System.out.println("Will delete the newly inserted person from the database");
if (db.deletePerson(idNo)) System.out.println("Person " + idNo + " is deleted");
else System.out.println("Person " + idNo + " is not deleted");
System.out.println("All the persons in the database after updates and deleting:");
all = db.getAll();
for (int i = 0; i < all.size(); i++) {
aPerson = (Person) all.get(i);
System.out.println(aPerson);
}
db.closeTheConnection();
System.exit(0);
}
}
/* Example Run, the person table contains the data from the script in section 20.2.
** From the Database class: A connection to the database is established.
Get all:
** From the Database class: select * from person order by lastName, firstName
100: Brown, Edward
101: Green, Ann Margaret
102: Johnson, John
Will store Ann Morris in the database:
** From the Database class: insert into person values(103, 'ANN', 'MORRIS')
She got ident. no.: 103
All the persons, after Ann Morris are stored:
** From the Database class: select * from person order by lastName, firstName
100: Brown, Edward
101: Green, Ann Margaret
102: Johnson, John
103: Morris, Ann
Updates her first name from Ann to Greta:
** From the Database class: update person set firstname = 'GRETA', lastname = 'MORRIS' where identno = 103
Her first name is updated.
Updates her last name from Morris to Bull:
** From the Database class: update person set firstname = 'GRETA', lastname = 'BULL' where identno = 103
Her last name is updated
Will delete the newly inserted person from the database
** From the Database class: delete from person where identno = 103
Person 103 is deleted
All the persons in the database after updates and deleting:
** From the Database class: select * from person order by lastName, firstName
100: Brown, Edward
101: Green, Ann Margaret
102: Johnson, John
** From the Database class: The connection to the database is closed.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -