-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manager.java
125 lines (115 loc) · 5.6 KB
/
Manager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Definition of a reusable, generic Manager class that will take
// a table name and a Java class and generate a new instance of the
// passed Java class based on the table structure.
// Author: Siomara Pantarotto
// File : Manager.java
// Date : June,30,2000
import java.sql.*;
import java.util.*;
import java.lang.reflect.*;
public class Manager {
private String tableName;
private String className;
private Connection con;
/////////////////////////////////////////////////////////////////////////
// Constructor of class Manager
//
public Manager(Connection con, String tableName, String className ) {
setConnection(con);
setTableName(tableName);
setClassName(className);
}
//////////////////////////////////////////////////////////////////////////
// Set methods
//
public void setConnection(Connection con) { this.con = con; }
public void setTableName(String tableName) { this.tableName = tableName; }
public void setClassName(String className) { this.className = className; }
/////////////////////////////////////////////////////////////////////////
// Get methods
//
public String getTableName() { return tableName; }
public String getClassName() { return className; }
///////////////////////////////////////////////////////////////////////
// Query a DB table using metadata to get all the column names and build
// a SELECT statement to select a unique row that matches a where clause
// passed as parameter. If the row is found, this method generates a vector
// with the desired values and call a methodthat intantiates an object.
// If the row is not found this method invokes the method that uses no
// argument and generates an object with default values.
//
public Object query(String whereClause) {
Object aObject = null;
Vector aVector = new Vector();
try {
ResultSet rs = con.createStatement().executeQuery("SELECT * FROM " + getTableName() + " WHERE Rowid = ''"); // Rowid = '' avoids retriving all rows
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String selectClause = "ROWID";
for (int col = 1; col <= columnCount; col++) {
selectClause = selectClause + ", " + rsmd.getColumnLabel(col);
}
selectClause = "SELECT " + selectClause + " FROM " + getTableName();
if (whereClause != "") {
selectClause = selectClause + " WHERE " + whereClause;
}
rs = con.createStatement().executeQuery(selectClause);
if (rs.next()) {
// Generates a vector with all the values retrieve from the DB
// table row and call method that creates an object using
// constructor that have arguments
aVector.removeAllElements();
for (int col = 1; col <= columnCount + 1; col++) {
aVector.addElement(rs.getString(col));
} Class[] vecArgsClass = new Class[] {Vector.class}; Object[] vecArgsObject = new Object[] {aVector}; Constructor vecArgsConstructor; Class classDefinition = Class.forName(getClassName()); vecArgsConstructor = classDefinition.getConstructor(vecArgsClass); aObject = createObject(vecArgsConstructor, vecArgsObject);
}
else { // Call method that creates an object using NO-argument constructors // since no row matches the where clause that was passed as parameter
aObject = createObject(getClassName());
}
} catch (ClassNotFoundException e) {
System.out.println(e);
} catch (NoSuchMethodException e) {
System.out.println(e);
} catch (Exception e) {
System.out.println(e);
} return aObject;
}
//////////////////////////////////////////////////////////////////////////
// Create an Object Using No-Argument Constructors
//
public Object createObject(String className) { System.out.println ("\nThe constructor used is : " + getClassName() + "()");
Object aObject = null;
try {
Class classDefinition = Class.forName(getClassName());
aObject = classDefinition.newInstance();
System.out.println ("The object created is: " + aObject.toString());
} catch (InstantiationException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
} catch (ClassNotFoundException e) {
System.out.println(e);
}
return aObject;
}
////////////////////////////////////////////////////////////////////////
// Create an Object Using Constructors that Have Arguments
//
public Object createObject(Constructor constructor, Object[] arguments) {
System.out.println ("\nThe constructor used is : " + constructor.toString());
Object aObject = null;
try {
aObject = constructor.newInstance(arguments);
System.out.println ("The object created is: " + aObject.toString());
} catch (InstantiationException e) {
System.out.println(e);
} catch (IllegalAccessException e) {
System.out.println(e);
} catch (IllegalArgumentException e) {
System.out.println(e);
} catch (InvocationTargetException e) {
System.out.println(e);
}
return aObject;
}
}