jtdaoadapter.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 2,310 行 · 第 1/4 页
JAVA
2,310 行
package Jt.DAO;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.InputStream;
//import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.Connection;
//import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
//import java.text.DateFormat;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.jdom.Element;
import Jt.JtAdapter;
import Jt.JtCollection;
import Jt.JtFactory;
import Jt.JtJDBCAdapter;
import Jt.JtMessage;
import Jt.JtObject;
import Jt.xml.JDOMAdapter;
/**
* Implements the Data Access Object design pattern (DAO).
* This is Jt native implementation. Additional DAO implemetations (strategies)
* are supported as well. For instance, one is provided for Hibernate DAO integration.
* Please refer to the JtDAOStrategy class.
*/
public class JtDAOAdapter extends JtAdapter {
private static final long serialVersionUID = 1L;
public static final String JtINSERT = "JtINSERT";
public static final String JtCREATE = "JtCREATE";
public static final String JtFIND = "JtFIND";
public static final String JtREAD = "JtREAD";
public static final String JtUPDATE = "JtUPDATE";
public static final String JtDELETE = "JtDELETE";
public static final String JtBEGIN_TRANSACTION = "JtBEGIN_TRANSACTION";
public static final String JtCOMMIT = "JtCOMMIT";
public static final String JtROLLBACK = "JtROLLBACK";
public static final String JtEXECUTE_QUERY = "JtEXECUTE_QUERY";
public static final String JtMAP_ATTRIBUTE = "JtMAP_ATTRIBUTE";
public static final String JtCALCULATE_KEY = "JtCALCULATE_KEY";
public static final String JtCLOSE_CONNECTION = "JtCLOSE_CONNECTION";
public static final String JtFIND_RECORDS = "JtFIND_RECORDS";
public static final String JtREAD_MAPPING_STREAM = "JtREAD_MAPPING_STREAM";
public static final String JtCLEAR = "JtCLEAR";
public static final String JtCLASS_NAME = JtDAOAdapter.class.getName();
private boolean autocommit = true;
private JtObject db = null; // JDBC adapter
private Hashtable attr;
private String configFile = "hibernate.cfg.xml"; // default configuration file
Hashtable classTable = new Hashtable ();
private String datasource;
private String user;
private String password;
private String url;
private String driver;
private JtFactory factory = new JtFactory ();
private boolean initted = false;
/**
* Returns the value of the autocommit attribute.
*/
public boolean getAutocommit() {
return autocommit;
}
/**
* Specifies if the transaction should be automatically commited
* after each operation.
* @param autocommit boolean property
*/
public void setAutocommit(boolean autocommit) {
this.autocommit = autocommit;
}
/**
* Returns the Datasource logical name (JNDI).
*/
public String getDatasource() {
return datasource;
}
/**
* Specifies the Datasource logical name (if any).
* @param datasource Datasource logical name
*/
public void setDatasource(String datasource) {
this.datasource = datasource;
}
/**
* Returns the database user.
*/
public String getUser() {
return user;
}
/**
* Specifies the database user.
* @param user user
*/
public void setUser(String user) {
this.user = user;
}
/**
* Returns the database driver.
*/
public String getDriver() {
return driver;
}
/**
* Specifies the database driver.
* @param driver driver
*/
public void setDriver(String driver) {
this.driver = driver;
}
/**
* Returns the URL of the database.
*/
public String getUrl() {
return url;
}
/**
* Specifies the URL of the database.
* @param url url
*/
public void setUrl(String url) {
this.url = url;
}
/**
* Returns the database password.
*/
public String getPassword() {
return password;
}
/**
* Specifies the database password.
* @param password password
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 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 boolean isKeyAttribute (List keys, String att) {
Iterator it;
DAOMapping mapping;
if (keys == null || keys.isEmpty() || att == null)
return false;
it = keys.iterator();
while (it.hasNext()) {
mapping = (DAOMapping) it.next();
if (att.equals(mapping.getAttribute()))
return true;
}
return false;
}
private int setParameters (PreparedStatement pst, Hashtable map_table, List keys)
{
Enumeration attrs;
String att;
//String value;
Object tmp;
int i = 1;
java.sql.Timestamp tmp1;
if (pst == null)
return i;
if (map_table == null)
return i;
if (attr == null)
return i;
// keys = attr.keys ();
attrs = map_table.keys ();
//i = 1;
while (attrs.hasMoreElements ()) {
att = (String) attrs.nextElement ();
// check the list of keys to skip
if (keys != null) {
if (isKeyAttribute (keys, att))
continue;
}
tmp = (Object) attr.get (att);
if (tmp instanceof java.util.Date) {
tmp1 = new
java.sql.Timestamp (((java.util.Date)tmp).getTime ());
try {
pst.setTimestamp (i, tmp1);
} catch (Exception ex) {
handleException (ex);
}
i++;
//System.out.println ("setParametes: " + tmp1);
}
}
return i;
}
private boolean setKeyValue (PreparedStatement pst, int pos, Object keyValue) {
java.sql.Timestamp tmp1;
if (pst == null || (pos <= 0) || keyValue == null)
return false;
if (keyValue instanceof java.util.Date) {
tmp1 = new
java.sql.Timestamp (((java.util.Date)keyValue).getTime ());
try {
pst.setTimestamp (pos, tmp1);
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
} else if (keyValue instanceof String) {
try {
pst.setString (pos, (String) keyValue);
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
} else if (keyValue instanceof Boolean) {
try {
pst.setBoolean (pos, ((Boolean) keyValue).booleanValue());
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
} else if (keyValue instanceof Integer) {
try {
pst.setInt (pos, ((Integer) keyValue).intValue());
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
} else if (keyValue instanceof Byte) {
try {
pst.setByte (pos, ((Byte) keyValue).byteValue());
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
} if (keyValue instanceof Double) {
try {
pst.setDouble (pos, ((Double) keyValue).doubleValue());
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
} if (keyValue instanceof Long) {
try {
pst.setLong (pos, ((Long) keyValue).longValue());
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
} if (keyValue instanceof Short) {
try {
pst.setShort (pos, ((Short) keyValue).shortValue());
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
} if (keyValue instanceof Float) {
try {
pst.setFloat (pos, ((Float) keyValue).floatValue());
} catch (Exception ex) {
handleException (ex);
return false;
}
return true;
}
handleError ("unknown type");
return false;
}
private boolean setKeyParameters (PreparedStatement pst, List keys, Object key,
int index, boolean simple)
{
//String att;
//String value;
//Object tmp;
int i;
//java.sql.Date tmp1;
int cnt;
String keyAttr;
Object keyValue;
if (pst == null)
return false;
if (keys == null)
return false;
if (key == null)
return false;
if (index < 1)
return false;
// keys = attr.keys ();
if (simple)
if (setKeyValue (pst, index, key))
return true;
else
return false;
cnt = keys.size();
for (i = 1; i <= cnt; i++) {
keyAttr = ((DAOMapping) keys.get(i - 1)).getAttribute();
//if (cnt == 1) {
// if (!setKeyValue (pst, i, key))
// return false;
//} else {
keyValue = factory.getValue(key, keyAttr);
if (keyValue == null)
return false;
if (!setKeyValue (pst, index++, keyValue))
return false;
//}
}
return true;
}
/**
* Insert DAO
*/
private Object insert (Object obj) {
JtMessage msg;
String query;
//Object conn;
PreparedStatement pst;
Object out = null;
msg = new JtMessage ();
if (obj == null) {
handleError ("insert: invalid parameter obj (null)");
return (null);
}
//if (db == null)
// realize ();
attr = getAttributes (obj);
query = buildInsertQuery (obj);
if (query == null || db == null)
return (null);
msg.setMsgId (JtJDBCAdapter.JtPREPARE_STATEMENT);
msg.setMsgContent (query);
pst = (PreparedStatement) factory.sendMessage (db, msg);
if (propagateException (db) != null) {
return null;
}
if (pst == null)
return (null); // check
setParameters (pst, retrieveMappingTable (obj.getClass().getName()), null);
msg.setMsgId (JtJDBCAdapter.JtEXECUTE_PREPARED_UPDATE);
msg.setMsgContent (pst);
out = factory.sendMessage (db, msg);
if (propagateException (db) != null) {
return null;
}
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 (out);
} else
return (null);
}
/**
* Update DAO
*/
private Object update (Object obj) {
JtMessage msg;
String query;
//Object conn;
Object out;
List keys;
int index;
msg = new JtMessage ();
PreparedStatement pst = null;
if (obj == null) {
handleError ("update: invalid parameter obj (null)");
return (null);
}
//if (db == null)
// realize ();
attr = getAttributes (obj);
query = buildUpdateQuery (obj);
if (query == null || db == null)
return (null);
msg.setMsgId (JtJDBCAdapter.JtPREPARE_STATEMENT);
msg.setMsgContent (query);
pst = (PreparedStatement) factory.sendMessage (db, msg);
if (propagateException (db) != null)
return (null);
if (pst == null)
return (null); // check
keys = retrieveKeys (obj.getClass().getName());
if (keys == null || keys.isEmpty())
return null;
index = setParameters (pst, retrieveMappingTable (obj.getClass().getName()),
keys);
if (!setKeyParameters (pst, keys, obj, index, false))
return null;
msg.setMsgId (JtJDBCAdapter.JtEXECUTE_PREPARED_UPDATE);
msg.setMsgContent (pst);
out = factory.sendMessage (db, msg);
if (propagateException (db) != null)
return (null);
try {
pst.close ();
}
catch (Exception ex) {
handleException (ex);
}
if (out instanceof Integer) {
if (((Integer) out).intValue () != 1)
return (null);
else
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?