5 public static void main(String
[] args
)
7 String url
= "jdbc:mysql://localhost:3306/";
8 String dbName
= "test";
9 String driver
= "com.mysql.jdbc.Driver";
10 String userName
= "root";
14 Class
.forName(driver
).newInstance();
15 Connection db
= DriverManager
.getConnection(url
+dbName
,userName
,password
);
16 // System.out.println("Connected to the database");
18 Statement st
= db
.createStatement();
19 st
.executeUpdate("DROP TABLE IF EXISTS person");
20 st
.executeUpdate("DROP TABLE IF EXISTS money");
21 demo_java_gen gen
= new demo_java_gen();
22 gen
.create_person(db
);
25 gen
.add_person(db
,"John","Black");
26 ResultSet rs
= st
.executeQuery("SELECT LAST_INSERT_ID()"); rs
.next();
27 int john
= rs
.getInt(1);
29 gen
.add_person(db
,"Ivan","Petrov");
30 rs
= st
.executeQuery("SELECT LAST_INSERT_ID()"); rs
.next();
31 int ivan
= rs
.getInt(1);
33 gen
.add_person(db
,"Sancho","Alvares");
34 rs
= st
.executeQuery("SELECT LAST_INSERT_ID()"); rs
.next();
35 int sancho
= rs
.getInt(1);
37 // add money relations
38 gen
.add_money(db
,john
,ivan
,200);
39 gen
.add_money(db
,john
,sancho
,100);
40 gen
.add_money(db
,john
,sancho
,250);
41 gen
.add_money(db
,sancho
,ivan
,300);
43 class a1
implements demo_java_gen
.output_4
45 public void callback(String fullname
, int total
)
47 System
.out
.println(fullname
+ " = " + total
);
51 // summarize by person
52 System
.out
.println("Total transfers:");
53 gen
.calc_total(db
,new a1());
55 class a2
implements demo_java_gen
.output_5
57 public void callback(String surname
)
59 System
.out
.println(surname
);
64 System
.out
.println("Donors:");
65 gen
.list_donors(db
,new a2(),"petrov",100);