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
);
17 Statement st
= db
.createStatement();
18 st
.executeUpdate("DROP TABLE IF EXISTS person");
19 st
.executeUpdate("DROP TABLE IF EXISTS money");
20 demo_java_gen gen
= new demo_java_gen(db
);
24 gen
.add_person("John","Black");
25 ResultSet rs
= st
.executeQuery("SELECT LAST_INSERT_ID()"); rs
.next();
26 int john
= rs
.getInt(1);
28 gen
.add_person("Ivan","Petrov");
29 rs
= st
.executeQuery("SELECT LAST_INSERT_ID()"); rs
.next();
30 int ivan
= rs
.getInt(1);
32 gen
.add_person("Sancho","Alvares");
33 rs
= st
.executeQuery("SELECT LAST_INSERT_ID()"); rs
.next();
34 int sancho
= rs
.getInt(1);
36 // add money relations
37 gen
.add_money(john
,ivan
,200);
38 gen
.add_money(john
,sancho
,100);
39 gen
.add_money(john
,sancho
,250);
40 gen
.add_money(sancho
,ivan
,300);
42 class a1
implements demo_java_gen
.calc_total_callback
44 public void callback(String fullname
, int total
)
46 System
.out
.println(fullname
+ " = " + total
);
50 // summarize by person
51 System
.out
.println("Total transfers:");
52 gen
.calc_total(new a1());
54 class a2
implements demo_java_gen
.list_donors_callback
56 public void callback(String surname
)
58 System
.out
.println(surname
);
63 System
.out
.println("Donors:");
64 gen
.list_donors("petrov",100,new a2());