📄 mssqlkjavadao.java
字号:
package com.eline.wap.resource.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.eline.wap.common.jdbc.DBCommonUtils;
import com.eline.wap.common.jdbc.DBSqlManager;
import com.eline.wap.common.model.Page;
import com.eline.wap.common.util.AppLogger;
import com.eline.wap.common.util.SerializeData;
import com.eline.wap.common.util.StringUtils;
import com.eline.wap.resource.exceptions.ResourceDAOSysException;
import com.eline.wap.resource.model.KJava;
import com.eline.wap.resource.model.KJavaCondition;
import com.eline.wap.resource.model.KJavaStorage;
public class MSSqlKJavaDAO extends DBSqlManager implements KJavaDAO {
static private final int TYPE_ADD = 0;
static private final int TYPE_UPDATE = 1;
protected static String[] SEARCH_KJAVA_STATEMENT_FRAGMENTS = {
"SELECT IndexID, ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, SearchKeywords, PropertyNames, PropertyValues FROM TAB_KJAVA WHERE Deleted <> 1 ",
"AND ParentID = ? ",
"AND SearchKeywords = ? ",
"AND IsSearchable = 1 ",
"AND IsActive = 1 ",
"AND DateCreated >= ? ",
"AND DateCreated < ? "
};
public Page searchKJavas(KJavaCondition condition, int start, int count)
throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rst = null;
Page page = new Page();
String sql = SEARCH_KJAVA_STATEMENT_FRAGMENTS[0];
if (condition != null) {
if (condition.getParentId() != -1)
sql += SEARCH_KJAVA_STATEMENT_FRAGMENTS[1];
// 注意: SearchKeywords == null的情况下就不需要判断是否ForceToSearch了
if (condition.getSearchKeywords() != null) {
sql += SEARCH_KJAVA_STATEMENT_FRAGMENTS[3];
if (!condition.isForceToSearch())
sql += SEARCH_KJAVA_STATEMENT_FRAGMENTS[4];
}
if (condition.isActiveOnly())
sql += SEARCH_KJAVA_STATEMENT_FRAGMENTS[5];
if (condition.getDateCreated() != null)
sql += SEARCH_KJAVA_STATEMENT_FRAGMENTS[6];
if (condition.getDateCreated2() != null)
sql += SEARCH_KJAVA_STATEMENT_FRAGMENTS[7];
}
sql += "ORDER BY SortOrder";
AppLogger.debug("MSSqlKJavaDAO.searchKJavas().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
// Set condition parameters
if (condition != null) {
int index = 1;
if (condition.getParentId() != -1)
stmt.setInt(index++, condition.getParentId());
if (condition.getSearchKeywords() != null)
stmt.setString(index++, "%" + condition.getSearchKeywords() + "%");
if (condition.getDateCreated() != null)
stmt.setString(index++, StringUtils.toString(condition.getDateCreated()));
if (condition.getDateCreated2() != null)
stmt.setString(index++, StringUtils.toString(condition.getDateCreated2()));
}
rst = stmt.executeQuery();
int totalRecords = 0;
for (totalRecords = 0; rst.next(); totalRecords ++) {
if ((totalRecords >= start) && (totalRecords < (start + count))) {
KJava item = populateKJavaFormResultSet(rst);
page.getItems().add(item);
}
}
page.setTotalRecords(totalRecords);
} catch (Exception e) {
AppLogger.debug("MSSqlKJavaDAO.searchKJavas().e.getMessage()=" + e.getMessage());
throw new ResourceDAOSysException(e.getMessage());
} finally {
try {
super.closeResultSet(rst);
super.closeStatement(stmt);
super.closeConnection(conn);
} catch (Exception e) {
throw new ResourceDAOSysException(e.getMessage());
}
}
return page;
}
protected KJava populateKJavaFormResultSet(ResultSet rst) throws SQLException {
KJava item = new KJava();
// Fill section properties.
DBCommonUtils.populateSectionFormResultSet(rst, item);
return item;
}
public void createKJava(KJava item) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
String sql = "INSERT INTO TAB_KJAVA (ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable,"
+ "SearchKeywords, PropertyNames, PropertyValues) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
AppLogger.debug("MSSqlKJavaDAO.createKJava().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
fillKJavaParameters(stmt, item, TYPE_ADD);
int rowCount = stmt.executeUpdate();
if (rowCount != 1)
throw new ResourceDAOSysException("effected row count must be 1");
} catch (Exception e) {
AppLogger.debug("MSSqlKJavaDAO.createKJava().e.getMessage()=" + e.getMessage());
throw new ResourceDAOSysException(e.getMessage());
} finally {
try {
super.closeStatement(stmt);
super.closeConnection(conn);
} catch (Exception e) {
throw new ResourceDAOSysException(e.getMessage());
}
}
}
protected void fillKJavaParameters(PreparedStatement stmt, KJava item, int type) throws SQLException {
int index = 1;
try {
if (type == TYPE_ADD) {
stmt.setInt(index++, item.getParentId());
stmt.setInt(index++, item.getSortOrder());
stmt.setString(index++, StringUtils.toString(item.getDateCreated()));
stmt.setString(index++, StringUtils.toString(item.getLastUpdate()));
stmt.setBoolean(index++, item.isActive());
stmt.setBoolean(index++, item.isSearchable());
stmt.setString(index++, item.getSearchKey());
// Serialize datas
SerializeData data = item.deserialize();
stmt.setString(index++, data.getKeys());
stmt.setString(index++, data.getValues());
} else if (type == TYPE_UPDATE) {
stmt.setInt(index++, item.getSortOrder());
stmt.setString(index++, StringUtils.toString(item.getLastUpdate()));
stmt.setBoolean(index++, item.isActive());
stmt.setBoolean(index++, item.isSearchable());
stmt.setString(index++, item.getSearchKey());
// Serialize datas
SerializeData data = item.deserialize();
stmt.setString(index++, data.getKeys());
stmt.setString(index++, data.getValues());
stmt.setInt(index++, item.getIndexId());
}
} catch (SQLException e) {
throw e;
}
}
/**
*
*/
public Page getKJavaStorages(int parentId, int start, int count) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rst = null;
Page page = new Page();
String sql = "SELECT IndexID, ParentID, DeviceModels, DeviceManufacturer, JADFile, FileSize, AccessCount, Description FROM TAB_KJAVASTORAGE WHERE ParentID = ? AND Deleted <> 1";
AppLogger.debug("MSSqlKJavaDAO.getKJavaStorages().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, parentId);
rst = stmt.executeQuery();
int totalRecords = 0;
// 获取所需要的数据
for (totalRecords = 0; rst.next(); totalRecords ++) {
if ((totalRecords >= start) && (totalRecords < (start + count))) {
KJavaStorage item = new KJavaStorage();
item.setIndexId(rst.getInt("IndexID"));
item.setKjavaId(rst.getInt("ParentID"));
item.setDeviceManufacturer(rst.getInt("DeviceManufacturer"));
item.setJadFile(rst.getString("JADFile"));
item.setFileSize(rst.getInt("FileSize"));
item.setAccessCount(rst.getInt("AccessCount"));
item.setDescription(rst.getString("Description"));
item.setDeviceModels(rst.getString("DeviceModels"));
page.getItems().add(item);
}
}
page.setTotalRecords(totalRecords);
} catch (Exception e) {
e.printStackTrace();
AppLogger.debug("MSSqlKJavaDAO.getKJavaStorages().e.getMessage()=" + e.getMessage());
throw new ResourceDAOSysException(e.getMessage());
} finally {
try {
super.closeStatement(stmt);
super.closeConnection(conn);
} catch (Exception e) {
throw new ResourceDAOSysException(e.getMessage());
}
}
return page;
}
public KJavaStorage getKJavaStorage(int indexId) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rst = null;
KJavaStorage item = null;
String sql = "SELECT IndexID, ParentID, DeviceModels, DeviceManufacturer, JADFile, FileSize, AccessCount, Description FROM TAB_KJAVASTORAGE WHERE IndexID = ?";
AppLogger.debug("MSSqlKJavaDAO.getKJavaStorage().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, indexId);
rst = stmt.executeQuery();
// 获取所需要的数据
if (rst.next()) {
item = new KJavaStorage();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -