साथ मैं इस बारे में जानकारी प्राप्त करने की कोशिश की, लेकिन खाली हाथ आए हैं:बनाना कक्षाएं गतिशील जावा
मैं यह संभव है प्रतिबिंब या प्रॉक्सी का उपयोग कर जावा में गतिशील रूप से एक वर्ग बनाने के लिए, लेकिन मैं बाहर नहीं मिल सकता है इकट्ठा किस तरह। मैं एक साधारण डेटाबेस ढांचे को कार्यान्वित कर रहा हूं जहां मैं प्रतिबिंब का उपयोग कर SQL क्वेरी बनाते हैं। विधि डेटाबेस फ़ील्ड के साथ ऑब्जेक्ट को पैरामीटर के रूप में प्राप्त करती है और उस पर आधारित क्वेरी बनाता है। लेकिन यह बहुत उपयोगी होगा अगर मैं वस्तु को गतिशील रूप से भी बना सकता हूं इसलिए मुझे प्रत्येक तालिका के लिए एक साधारण डेटा रैपर ऑब्जेक्ट की आवश्यकता नहीं होगी।
गतिशील कक्षाओं को केवल साधारण फ़ील्ड (String
, Integer
, Double
) की आवश्यकता होगी, उदाहरण के लिए
public class Data {
public Integer id;
public String name;
}
क्या यह संभव है और मैं यह कैसे करूँगा?
संपादित करें: यह मैं यह कैसे प्रयोग करेंगे है:
/** Creates an SQL query for updating a row's values in the database.
*
* @param entity Table name.
* @param toUpdate Fields and values to update. All of the fields will be
* updated, so each field must have a meaningful value!
* @param idFields Fields used to identify the row(s).
* @param ids Id values for id fields. Values must be in the same order as
* the fields.
* @return
*/
@Override
public String updateItem(String entity, Object toUpdate, String[] idFields,
String[] ids) {
StringBuilder sb = new StringBuilder();
sb.append("UPDATE ");
sb.append(entity);
sb.append("SET ");
for (Field f: toUpdate.getClass().getDeclaredFields()) {
String fieldName = f.getName();
String value = new String();
sb.append(fieldName);
sb.append("=");
sb.append(formatValue(f));
sb.append(",");
}
/* Remove last comma */
sb.deleteCharAt(sb.toString().length()-1);
/* Add where clause */
sb.append(createWhereClause(idFields, ids));
return sb.toString();
}
/** Formats a value for an sql query.
*
* This function assumes that the field type is equivalent to the field
* in the database. In practice this means that this field support two
* types of fields: string (varchar) and numeric.
*
* A string type field will be escaped with single parenthesis (') because
* SQL databases expect that. Numbers are returned as-is.
*
* If the field is null, a string containing "NULL" is returned instead.
*
* @param f The field where the value is.
* @return Formatted value.
*/
String formatValue(Field f) {
String retval = null;
String type = f.getClass().getName();
if (type.equals("String")) {
try {
String value = (String)f.get(f);
if (value != null) {
retval = "'" + value + "'";
} else {
retval = "NULL";
}
} catch (Exception e) {
System.err.println("No such field: " + e.getMessage());
}
} else if (type.equals("Integer")) {
try {
Integer value = (Integer)f.get(f);
if (value != null) {
retval = String.valueOf(value);
} else {
retval = "NULL";
}
} catch (Exception e) {
System.err.println("No such field: " + e.getMessage());
}
} else {
try {
String value = (String) f.get(f);
if (value != null) {
retval = value;
} else {
retval = "NULL";
}
} catch (Exception e) {
System.err.println("No such field: " + e.getMessage());
}
}
return retval;
}
वर्ग के ArrayList बनाने हूँ मुझे नहीं लगता है कि जावा कि के लिए सही उपकरण है, ग्रूवी बेहतर IMO अनुकूल हो पाती या नहीं। –
व्यक्तिगत रूप से मुझे जावा मॉडल रखने में समस्या दिखाई नहीं देती है जो आपके डेटाबेस मॉडल पर मैप करता है। SQL क्वेरी बनाने के दौरान, SQL स्ट्रिंग बनाने के बजाय, SQL इंजेक्शन से बचने के लिए प्रीपेयरस्टेटमेंट का उपयोग करना सुनिश्चित करें। – JeeBee