jtdao.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 1,523 行 · 第 1/3 页
JAVA
1,523 行
//value = (String) attr.get (key);
}
query.append (") values (");
keys = map_table.keys ();
while (keys.hasMoreElements ()) {
key = (String) keys.nextElement ();
tmp = attr.get (key);
if (tmp == null) {
query.append ("null");
} else if (tmp instanceof String) {
value = (String) attr.get (key); // check
query.append (value);
} else if (tmp instanceof java.util.Date) {
query.append ("?");
}
//value = (String) attr.get (key);
//query.append (value);
if (keys.hasMoreElements ())
query.append (",");
}
query.append (")");
handleTrace ("query:" + query);
return (query.toString ());
}
// find: find record
private JtObject find () {
JtMessage msg;
String query;
Object conn;
ResultSet res;
JtObject jout;
msg = new JtMessage ();
if (db == null)
realize ();
attr = getAttributes (); // check
query = build_select_query ();
if (query == null || db == null)
return (null);
conn = this.getValue (db, "connection");
// Connect to the data source
if (conn == null) {
msg.setMsgId (JtJDBCAdapter.JtCONNECT);
this.sendMessage (db, msg);
if (propagateException (db) != null)
return (null);
}
msg.setMsgId (JtJDBCAdapter.JtEXECUTE_QUERY);
msg.setMsgContent (query);
res = (ResultSet) this.sendMessage (db, msg);
if (propagateException (db) != null)
return (null);
if (res == null)
return (null);
try {
if (!res.next()) // check
return (null);
} catch (Exception e){
handleException (e);
return (null);
}
jout = map (res);
try {
res.close ();
}
catch (Exception ex) {
handleException (ex);
}
return (jout);
}
// find: find records based on a query
private JtObject findRecords (String query) {
JtMessage msg;
Object conn;
ResultSet res;
JtObject obj;
JtCollection col;
msg = new JtMessage ();
col = new JtCollection ();
if (db == null)
realize ();
attr = getAttributes (); // check
//query = build_select_query ();
if (query == null || db == null)
return (null);
conn = this.getValue (db, "connection");
// Connect to the data source
if (conn == null) {
msg.setMsgId (JtJDBCAdapter.JtCONNECT);
this.sendMessage (db, msg);
if (propagateException (db) != null)
return (null);
}
msg.setMsgId (JtJDBCAdapter.JtEXECUTE_QUERY);
msg.setMsgContent (query);
res = (ResultSet) this.sendMessage (db, msg);
if (propagateException (db) != null)
return (null);
if (res == null)
return (null);
msg.setMsgId (JtCollection.JtADD);
try {
while (res.next()) {
obj = (JtObject) this.getClass().newInstance ();
((JtDAO) obj).setKey (key);
((JtDAO) obj).setTable (table);
obj = mapRow (obj, res);
if (obj == null)
continue; // check
msg.setMsgContent (obj);
sendMessage (col, msg);
}
} catch (Exception e) {
handleException (e);
return (null);
}
try {
res.close ();
}
catch (Exception ex) {
handleException (ex);
}
return (col);
}
// delete: delete record
private Object delete () {
JtMessage msg;
String query;
Object conn;
//ResultSet res;
Object out;
msg = new JtMessage ();
if (db == null)
realize ();
attr = getAttributes (); // check
query = build_delete_query ();
if (query == null || db == null)
return (null);
conn = this.getValue (db, "connection");
// Connect to the data source
if (conn == null) {
msg.setMsgId (JtJDBCAdapter.JtCONNECT);
this.sendMessage (db, msg);
//return (propagateException (db));
}
msg.setMsgId (JtJDBCAdapter.JtUPDATE);
msg.setMsgContent (query);
out = this.sendMessage (db, msg);
propagateException (db);
if (out instanceof Integer) {
if (((Integer) out).intValue () != 1)
return (null);
else
return (this);
} else
return (null);
}
private String getRSValue (ResultSet rs, String column)
{
String value = null;
if (rs == null || column == null)
return (null);
try {
value = rs.getString (column);
} catch (Exception e) {
handleException (e);
}
return (value);
}
// getRSDateValue
private String getRSDateValue (ResultSet rs, String column)
{
String value = null;
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
if (rs == null || column == null)
return (null);
try {
java.sql.Date date = rs.getDate (column);
if (date == null)
return (null); // check
value = df.format (date); // check
//value = rs.getString (column);
} catch (Exception e) {
handleException (e);
}
return (value);
}
private String getRSBooleanValue (ResultSet rs, String column)
{
String value = null;
if (rs == null || column == null)
return (null);
try {
boolean b = rs.getBoolean (column);
//if (b == null)
// return (null); // check
if (b)
return ("true");
else
return ("false");
//value = b.toString ();
} catch (Exception e) {
handleException (e);
}
return (value);
}
private JtObject map (ResultSet rs) {
//Object args[];
PropertyDescriptor[] prop;
int i;
Class p;
BeanInfo info = null;
String value; // check
String column;
try {
info = Introspector.getBeanInfo(
this.getClass (), this.getClass ().getSuperclass());
} catch(Exception e) {
handleException (e);
return (null);
}
prop = info.getPropertyDescriptors();
for(i = 0; i < prop.length; i++) {
//System.out.print ("Attribute:" +
//prop[i].getName());
p = prop[i].getPropertyType();
column = getMapping (prop[i].getName());
if (column == null) {
continue;
}
if (p.getName().equals ("java.util.Date") ){
value = getRSDateValue (rs, column);
} else if (p.getName().equals ("boolean") ){
value = getRSBooleanValue (rs, column);
} else // check
value = getRSValue (rs, column);
setValue (this, prop[i].getName(), value);
}
return (this);
}
private JtObject mapRow (JtObject obj, ResultSet rs) {
//Object args[];
PropertyDescriptor[] prop;
int i;
Class p;
BeanInfo info = null;
String value; // check
String column;
try {
info = Introspector.getBeanInfo(
obj.getClass (), obj.getClass ().getSuperclass());
} catch(Exception e) {
handleException (e);
return (null);
}
prop = info.getPropertyDescriptors();
for(i = 0; i < prop.length; i++) {
//System.out.print ("Attribute:" +
//prop[i].getName());
p = prop[i].getPropertyType();
column = getMapping (prop[i].getName());
if (column == null) {
continue;
}
if (p.getName().equals ("java.util.Date") ){
value = getRSDateValue (rs, column);
} else if (p.getName().equals ("boolean") ){
value = getRSBooleanValue (rs, column);
} else // check
value = getRSValue (rs, column);
setValue (obj, prop[i].getName(), value);
}
return (obj);
}
private void clear () {
//Object args[];
PropertyDescriptor[] prop;
int i;
BeanInfo info = null;
Class p;
String value = null;
try {
info = Introspector.getBeanInfo(
this.getClass (), this.getClass ().getSuperclass());
} catch(Exception e) {
handleException (e);
return;
}
prop = info.getPropertyDescriptors();
for(i = 0; i < prop.length; i++) {
if (prop[i].getName().equals (key))
continue;
//handleTrace ("JtDAO.clear:" + prop[i].getName());
p = prop[i].getPropertyType();
if (p.getName().equals ("java.lang.String") ){
value = null;
} else if (p.getName().equals ("int") ||
p.getName().equals ("float") ||
p.getName().equals ("double") ||
p.getName().equals ("long") ||
p.getName().equals ("short") ||
p.getName().equals ("byte")) {
value = "0";
} else if (p.getName().equals ("java.util.Date")) {
value = null;
} else if (p.getName().equals ("boolean")) {
value = "false";
} else if (p.getName().equals ("char")) {
value = "\0"; //check
} else {
handleWarning ("JtDAO.clear:unknown type:" +
p.getName());
continue;
}
setValue (this, prop[i].getName(), value); // check
}
}
// update: update record
private Object update () {
JtMessage msg;
String query;
Object conn;
Object out;
msg = new JtMessage ();
PreparedStatement pst = null;
if (db == null)
realize ();
attr = getAttributes ();
query = build_update_query ();
if (query == null || db == null)
return (null);
conn = this.getValue (db, "connection");
// Connect to the data source
if (conn == null) {
msg.setMsgId (JtJDBCAdapter.JtCONNECT);
this.sendMessage (db, msg);
}
/*
msg.setMsgId ("JtUPDATE");
msg.setMsgContent (query);
this.sendMessage (db, msg);
*/
msg.setMsgId (JtJDBCAdapter.JtPREPARE_STATEMENT);
msg.setMsgContent (query);
pst = (PreparedStatement) this.sendMessage (db, msg);
if (propagateException (db) != null)
return (null);
if (pst == null)
return (null); // check
setParameters (pst);
if (propagateException (db) != null)
return (null);
msg.setMsgId (JtJDBCAdapter.JtEXECUTE_PREPARED_UPDATE);
msg.setMsgContent (pst);
out = this.sendMessage (db, msg);
try {
pst.close ();
}
catch (Exception ex) {
handleException (ex);
}
if (propagateException (db) != null)
return (null);
if (out instanceof Integer) {
if (((Integer) out).intValue () != 1)
return (null);
else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?