test for single row (fails)
[sqlgg.git] / demo / demo_java.java
blobe0f0cefb7f95a55a040f0b99b65662d8ced097b3
1 import java.sql.*;
3 public class demo_java
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";
11 String password = "";
12 try
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);
21 gen.create_person();
22 gen.create_money();
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);
62 // list donors
63 System.out.println("Donors:");
64 gen.list_donors("petrov",100,new a2());
66 db.close();
68 catch (Exception e)
70 e.printStackTrace();