jtdao.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 1,523 行 · 第 1/3 页
JAVA
1,523 行
package Jt;
import java.sql.*;
import java.text.*;
import java.lang.reflect.*;
import java.beans.*;
import java.util.*;
import Jt.DAO.JtDAOAdapter;
import Jt.xml.JtXMLMsgReader;
/**
* Implements the Data Access Object pattern (DAO). This class is being deprecated.
* Please refer to Jt.DAO.JtDAOAdapter.
*/
public class JtDAO extends JtObject {
public static final String JtCLASS_NAME = JtDAO.class.getName();
private static final long serialVersionUID = 1L;
private transient Connection connection = null;
int n = 0;
JtObject db = null;
String insert_query = null;
//Hashtable attr;
Hashtable attr;
private Object key = null; // key field
private String table;
private Hashtable map_table = null;
private String configFile = null;
public JtDAO () {
}
void map_attribute (String at, String column) {
if (at == null || column == null)
return;
if (map_table == null)
map_table = new Hashtable ();
if (!validateAttribute (at)) {
handleError ("JtDAO.map_attribute: invalid attribute mapping:"
+ at);
return;
}
map_table.put (at, column);
}
String getMapping (String at) {
String column;
if (at == null)
return (null);
if (map_table == null)
return (null);
column = (String) map_table.get (at);
/*
if (column == null)
return (at);
*/
return (column);
}
private Object closeConnection () {
if (db == null) {
handleWarning ("closeConnection: database object is null");
} else
handleTrace ("closeConnection: closing a db connection");
return (sendMessage (db, new JtMessage (JtJDBCAdapter.JtCLOSE)));
}
// When dealing with DataSources, close the connection
private void closeDataSourceConnection () {
String datasource;
if (db == null)
return;
datasource = (String) getValue (db, "datasource");
if (datasource != null)
sendMessage (db, new JtMessage (JtJDBCAdapter.JtCLOSE));
}
/**
* Process object messages.
* <ul>
* <li> JtINSERT - Insert an object
* <li> JtFIND - Find an object
* <li> JtUPDATE - Update an object
* <li> JtDELETE - Delete an object
* <li> JtMAP_ATTRIBUTE - Map attribute to database column
* <li> JtREMOVE - Remove the JtDAO. Release resources including the database connection
* </ul>
*/
public Object processMessage (Object event) {
//String content;
//String query;
JtMessage e = (JtMessage) event;
Object reply;
if (e == null || (e.getMsgId() == null))
return (null);
resetExceptions ();
// establish a connection
if (e.getMsgId().equals(JtObject.JtREALIZE)) {
realize ();
return (null);
}
// insert record
if (e.getMsgId().equals(JtDAOAdapter.JtINSERT) ||
e.getMsgId().equals(JtDAOAdapter.JtCREATE)) {
reply = insert ();
closeDataSourceConnection ();
return (reply);
}
if (e.getMsgId().equals(JtDAOAdapter.JtFIND) ||
e.getMsgId().equals(JtDAOAdapter.JtREAD)) {
clear ();
reply = find ();
// When dealing with DataSources, close the connection after
// each operation
closeDataSourceConnection ();
return (reply);
}
if (e.getMsgId().equals(JtDAOAdapter.JtUPDATE)) {
reply = update ();
closeDataSourceConnection ();
return (reply);
}
if (e.getMsgId().equals(JtDAOAdapter.JtCLEAR)) {
clear ();
return (null);
}
if (e.getMsgId().equals(JtObject.JtPRINT)) {
print ();
return (null);
}
if (e.getMsgId().equals(JtDAOAdapter.JtMAP_ATTRIBUTE)) {
map_attribute ((String) e.getMsgContent (),
(String) e.getMsgData ());
return (null);
}
if (e.getMsgId().equals(JtDAOAdapter.JtDELETE)) {
reply = delete ();
closeDataSourceConnection ();
return (reply);
}
if (e.getMsgId().equals(JtDAOAdapter.JtCALCULATE_KEY)) {
reply = calculateKey ();
closeDataSourceConnection ();
return (reply);
}
if (e.getMsgId().equals(JtDAOAdapter.JtCLOSE_CONNECTION)) {
return (closeConnection ());
}
if (e.getMsgId().equals(JtDAOAdapter.JtFIND_RECORDS)) {
return (findRecords ((String) e.getMsgContent ()));
}
if (e.getMsgId().equals(JtObject.JtREMOVE)) {
destroy ();
return (null);
}
return (super.processMessage (event));
}
// build_select_query: build select query
private String build_select_query () {
StringBuffer query = new StringBuffer ();
String value;
//Object tmp;
Enumeration keys;
String att, tmp;
String key_column;
if (map_table == null)
return (null);
if (key == null || table == null)
return (null);
if (attr == null)
return (null);
value = (String) attr.get (key);
if (value == null) {
handleError ("build_select_query: invalid key (null)");
return (null);
}
key_column = getMapping ((String) key);
if (key_column == null) {
handleError ("build_select_query: invalid mapping for "
+ key);
return (null);
}
// query.append ("Select * from "
// + table + " where "); //check
// if (map_table == null)
// keys = attr.keys ();
// else
keys = map_table.keys ();
if (!keys.hasMoreElements ())
return (null);
query.append ("Select ");
while (keys.hasMoreElements ()) {
att = (String) keys.nextElement ();
tmp = getMapping (att);
if (tmp == null) {
handleError ("build_select_query: invalid mapping for "
+ att);
return (null);
}
query.append (tmp);
if (keys.hasMoreElements ())
query.append (",");
}
query.append (" from " + table + " where ");
query.append (key_column);
query.append ("=");
query.append (value);
handleTrace ("query:" + query);
return (query.toString ());
}
// build_delete_query: build delete query
private String build_delete_query () {
StringBuffer query = new StringBuffer ();
Object value;
//Object tmp;
String key_column;
if (attr == null)
return (null);
if (key == null || table == null)
return (null);
if (map_table == null)
return (null);
value = attr.get (key);
//value = (Object) getValue (this, key);
if (value == null) {
handleError ("build_delete_query: invalid key (null)");
return (null);
}
key_column = getMapping ((String) key);
if (key_column == null) {
handleError ("build_delete_query: invalid mapping for "
+ key);
return (null);
}
query.append ("Delete from "
+ table + " where "); //check
query.append (key_column);
//query.append (key);
query.append ("=");
query.append (value);
handleTrace ("query:" + query);
return (query.toString ());
}
// build_update_query: build update query
private String build_update_query () {
StringBuffer query = new StringBuffer ();
Enumeration keys;
String att;
String value;
Object tmp;
String column;
String key_column;
boolean first = true;
if (attr == null)
return (null);
if (key == null || table == null)
return (null);
if (map_table == null)
return (null);
keys = map_table.keys ();
if (!keys.hasMoreElements ())
return (null);
query.append ("Update " + table + " Set ");
while (keys.hasMoreElements ()) {
att = (String) keys.nextElement ();
if (att.equals (key))
continue; // check
tmp = (Object) attr.get (att);
if (!((tmp == null) || tmp instanceof String ||
tmp instanceof java.util.Date)) {
handleError ("build_update_query:invalid type for "
+ att);
return (null);
}
column = getMapping (att);
if (column == null) {
handleError ("build_update_query: invalid mapping for " +
att);
return (null);
}
if (first) {
first = false;
} else
query.append (", ");
query.append (column);
if (tmp == null) {
query.append ("="+null);
} else if (tmp instanceof String) {
value = (String) attr.get (att);
query.append ("="+value);
} else if (tmp instanceof java.util.Date) {
query.append ("= ?");
}
/*
if (keys.hasMoreElements ())
query.append (",");
*/
//value = (String) attr.get (key);
}
query.append (" where ");
value = (String) attr.get (key);
if (value == null) {
handleError ("build_update_query: invalid key (null)");
return (null);
}
key_column = getMapping ((String) key);
if (key_column == null) {
handleError ("build_update_query: invalid mapping for "
+ key);
return (null);
}
query.append (key_column + "=" + value);
handleTrace ("query:" + query);
return (query.toString ());
}
void setParameters (PreparedStatement pst)
{
Enumeration keys;
String att;
//String value;
Object tmp;
int i;
java.sql.Date tmp1;
if (pst == null)
return;
if (map_table == null)
return;
if (attr == null)
return;
// keys = attr.keys ();
keys = map_table.keys ();
i = 1;
while (keys.hasMoreElements ()) {
att = (String) keys.nextElement ();
if (att.equals (key))
continue;
tmp = (Object) attr.get (att);
if (tmp instanceof java.util.Date) {
tmp1 = new
java.sql.Date (((java.util.Date)tmp).getTime ());
try {
pst.setDate (i, tmp1);
} catch (Exception ex) {
handleException (ex);
}
i++;
}
}
}
// build_insert_query: build insert query
private String build_insert_query () {
StringBuffer query = new StringBuffer ();
Enumeration keys;
String key;
String value;
Object tmp;
String attn;
if (attr == null || table == null)
return (null);
if (map_table == null)
return (null);
keys = map_table.keys ();
if (!keys.hasMoreElements ())
return (null);
query.append ("Insert into " + table + " (");
while (keys.hasMoreElements ()) {
key = (String) keys.nextElement ();
//attn = (String) map_table.get (key);
attn = getMapping (key);
if (attn == null) {
handleError ("build_update_query: invalid mapping for " +
attn);
return (null);
}
query.append (attn);
if (keys.hasMoreElements ())
query.append (",");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?