demo_java. Need to pass java class name to sqlgg
[sqlgg.git] / demo / demo_java.java
blobd71163af2689fe02d3e683084a113bbf2aedd19a
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);
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);
23 gen.create_money(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);
63 // list donors
64 System.out.println("Donors:");
65 gen.list_donors(db,new a2(),"petrov",100);
67 db.close();
69 catch (Exception e)
71 e.printStackTrace();