jtdao.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 1,523 行 · 第 1/3 页
JAVA
1,523 行
return (this);
} else
return (null);
}
// propagateException:
private Exception propagateException (JtObject obj)
{
Exception ex;
if (obj == null)
return null;
ex = (Exception)
getValue (obj, "objException");
if (ex != null)
setValue (this, "objException", ex);
return (ex);
}
// resetExceptions:
private void resetExceptions ()
{
setValue (this, "objException", null);
if (db != null)
setValue (db, "objException", null);
}
// insert: insert record
private Object insert () {
JtMessage msg;
String query;
Object conn;
PreparedStatement pst;
Object out = null;
msg = new JtMessage ();
if (db == null)
realize ();
attr = getAttributes ();
query = build_insert_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.JtPREPARE_STATEMENT);
msg.setMsgContent (query);
pst = (PreparedStatement) this.sendMessage (db, msg);
if (pst == null)
return (null); // check
setParameters (pst);
msg.setMsgId (JtJDBCAdapter.JtEXECUTE_PREPARED_UPDATE);
msg.setMsgContent (pst);
out = this.sendMessage (db, msg);
/*
msg.setMsgId ("JtUPDATE");
msg.setMsgContent (query);
this.sendMessage (db, msg);
*/
try {
pst.close ();
}
catch (Exception ex) {
handleException (ex);
}
propagateException (db);
if (out == null)
return (null);
if (out instanceof Integer) {
if (((Integer) out).intValue () != 1)
return (null);
else
return (this);
} else
return (null);
}
/**
* Specifies the Database Connection.
*/
public void setConnection (Connection newConnection) {
connection = newConnection;
}
/**
* Returns the Database Connection.
*/
public Connection getConnection () {
return (connection);
}
/**
* Specifies the name of the configuration file.
*/
public void setConfigFile (String configFile) {
this.configFile = configFile;
}
/**
* Returns the name of the configuration file.
*/
public String getConfigFile () {
return (configFile);
}
private Hashtable getAttributes () {
//Object args[];
PropertyDescriptor[] prop;
int i;
//Class p;
Method m;
BeanInfo info = null;
Object value;
Hashtable attr;
attr = new Hashtable ();
if (map_table == null) {
handleError
("JtDAO.getAttributes: attributes/database mapping is missing");
return (null);
}
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();
try {
m = prop[i].getReadMethod ();
if (m == null) {
handleError
("JtDAO: getReadMethod returned null");
return (null);
}
if (getMapping (prop[i].getName()) == null)
continue;
value = m.invoke (this, null);
if (value == null) {
// attr.put (prop[i].getName(), value);
continue;
}
if (value instanceof String) {
attr.put (prop[i].getName(), "'" + value + "'");
//System.out.println ("=" + value);
continue;
}
if (value instanceof Integer ||
value instanceof Long ||
value instanceof Float ||
value instanceof Byte ||
value instanceof Boolean ||
value instanceof Short ||
value instanceof Double ||
value instanceof Character) {
attr.put (prop[i].getName(), value.toString () );
//System.out.println ("=" + value);
continue;
}
if (value instanceof java.util.Date) {
attr.put (prop[i].getName(), value);
continue;
}
} catch (Exception e) {
handleException(e);
return (null);
}
}
return (attr);
}
private boolean validateAttribute (String att_name) {
PropertyDescriptor[] prop;
int i;
// Class p;
BeanInfo info = null;
if (att_name == null)
return (false);
try {
info = Introspector.getBeanInfo(
this.getClass (), this.getClass ().getSuperclass());
} catch(Exception e) {
handleException (e);
return (false);
}
prop = info.getPropertyDescriptors();
for(i = 0; i < prop.length; i++) {
// System.out.print ("Attribute:" +
// prop[i].getName());
// p = prop[i].getPropertyType();
if ((prop[i].getName()).equals (att_name))
return (true);
}
return (false);
}
private void print () {
//Object args[];
PropertyDescriptor[] prop;
int i;
//Class p;
Method m;
BeanInfo info = null;
Object value;
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++) {
// System.out.print ("Attribute:" +
// prop[i].getName());
//p = prop[i].getPropertyType();
try {
m = prop[i].getReadMethod ();
if (m == null) {
handleError
("JtDAO: getReadMethod returned null");
return;
}
value = m.invoke (this, null);
System.out.println (this.getClass ().getName () + "." +
prop[i].getName() + ":" + value);
} catch (Exception e) {
handleException(e);
}
}
}
/**
* Specifies the attribute to be used as the database key.
*/
public void setKey (Object key) {
this.key = key;
}
/**
* Returns the attribute to be used as the database key.
*/
public Object getKey () {
return (key);
}
/**
* Specifies the name of the database table.
*/
public void setTable (String table) {
this.table = table;
}
/**
* Returns the name of the database table.
*/
public String getTable () {
return (table);
}
private void readConfigFile () {
JtMessage msg;
JtObject cf;
if (configFile == null)
return;
msg= new JtMessage ();
msg.setMsgId (JtXMLMsgReader.JtPARSE);
msg.setMsgReplyTo (this);
cf = (JtObject) createObject (JtXMLMsgReader.JtCLASS_NAME, "configFile");
setValue (cf, "uri", configFile);
sendMessage (cf, msg);
}
private String build_key_query () {
StringBuffer query = new StringBuffer ();
String value;
//Object tmp;
//Enumeration keys;
//String att, tmp;
String key_column;
if (key == null || table == null)
return (null);
if (attr == null)
return (null);
value = (String) attr.get (key);
if (value == null) {
handleError ("build_key_query: invalid key (null)");
return (null);
}
key_column = getMapping ((String) key);
if (key_column == null) {
handleError ("build_key_query: invalid mapping for "
+ key);
return (null);
}
query.append ("Select Max (" + key_column + ") from " + table);
handleTrace ("query:" + query);
return (query.toString ());
}
// calculateKey: calculate a new key
private Object calculateKey () {
JtMessage msg;
String query;
Object conn;
ResultSet res;
//Object out;
long ltmp = 0L;
Exception ex1;
msg = new JtMessage ();
if (db == null)
realize ();
attr = getAttributes (); // check
query = build_key_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.JtEXECUTE_QUERY);
msg.setMsgContent (query);
res = (ResultSet) this.sendMessage (db, msg);
ex1 = propagateException (db);
if (ex1 != null)
return (new Long (-1L));
try
{
if (res.next()) {
ltmp = res.getLong(1);
}
}
catch (Exception ex)
{
handleException (ex);
return (new Long (-1L));
}
return (new Long(ltmp+1));
}
void realize () {
//JtMessage msg;
handleTrace ("JtDAO:realize");
if (db != null)
return;
//msg = new JtMessage ();
db = (JtObject) this.createObject (JtJDBCAdapter.JtCLASS_NAME, "db");
// this.setValue (db, "objTrace", "1"); // check
if (configFile != null)
readConfigFile ();
}
void destroy () {
if (db == null)
return;
//sendMessage (db, new JtMessage ("JtCLOSE"));
closeConnection ();
db = null;
}
private static void test () {
JtObject main;
//String query;
JtMessage msg;
main = new JtObject ();
// main.setValue (main, "objTrace", "1");
main.createObject (JtMessage.JtCLASS_NAME, "message");
msg = new JtMessage ();
main.createObject (JtDAO.JtCLASS_NAME, "dao");
// main.setValue ("dao", "objTrace", "1");
msg.setMsgId (JtObject.JtREALIZE);
main.sendMessage ("dao", msg);
}
/**
* Demonstrates the messages processed by JtDAO.
*/
public static void main (String[] args) {
test ();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?