📄 mssqlpicturedao.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.Picture;
import com.eline.wap.resource.model.PictureCondition;
public class MSSqlPictureDAO extends DBSqlManager implements PictureDAO {
static private final int TYPE_ADD = 0;
static private final int TYPE_UPDATE = 1;
public Picture getPicture(int pictureId) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rst = null;
Picture item = null;
String sql = "SELECT IndexID, ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, "
+ "SearchKeywords, PropertyNames, PropertyValues FROM TAB_PICTURE "
+ "WHERE IndexID = ?";
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, pictureId);
rst = stmt.executeQuery();
if (rst.next())
item = populatePictureFormResultSet(rst);
} catch (Exception e) {
AppLogger.debug("MSSqlPictureDAO.getPicture().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 item;
}
public Page getPictures(int parentId, int start, int count)
throws ResourceDAOSysException {
// TODO Auto-generated method stub
return null;
}
protected static String[] SEARCH_PICTURES_STATEMENT_FRAGMENTS = {
"SELECT IndexID, ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable, SearchKeywords, PropertyNames, PropertyValues FROM TAB_PICTURE WHERE Deleted <> 1 ",
"AND ParentID = ? ",
"AND SearchKeywords = ? ",
"AND IsSearchable = 1 ",
"AND IsActive = 1 ",
"AND DateCreated >= ? ",
"AND DateCreated < ? "
};
public Page searchPictures(PictureCondition condition, int start, int count)
throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rst = null;
Page page = new Page();
String sql = SEARCH_PICTURES_STATEMENT_FRAGMENTS[0];
if (condition != null) {
if (condition.getParentId() != -1)
sql += SEARCH_PICTURES_STATEMENT_FRAGMENTS[1];
// 注意: SearchKeywords == null的情况下就不需要判断是否ForceToSearch了
if (condition.getSearchKeywords() != null) {
sql += SEARCH_PICTURES_STATEMENT_FRAGMENTS[2];
if (!condition.isForceToSearch())
sql += SEARCH_PICTURES_STATEMENT_FRAGMENTS[3];
}
if (condition.isActiveOnly())
sql += SEARCH_PICTURES_STATEMENT_FRAGMENTS[4];
if (condition.getDateCreated() != null)
sql += SEARCH_PICTURES_STATEMENT_FRAGMENTS[5];
if (condition.getDateCreated2() != null)
sql += SEARCH_PICTURES_STATEMENT_FRAGMENTS[6];
}
sql += "ORDER BY SortOrder";
AppLogger.debug("MSSqlPictureDAO.searchPictures().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))) {
Picture item = populatePictureFormResultSet(rst);
page.getItems().add(item);
}
}
page.setTotalRecords(totalRecords);
} catch (Exception e) {
AppLogger.debug("MSSqlPictureDAO.searchPictures().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;
}
public void createPicture(Picture item) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
String sql = "INSERT INTO TAB_PICTURE (ParentID, SortOrder, DateCreated, LastUpdate, IsActive, IsSearchable,"
+ "SearchKeywords, PropertyNames, PropertyValues) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
AppLogger.debug("MSSqlPictureDAO.createPicture().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
fillPictureParameters(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("MSSqlPictureDAO.createPicture().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());
}
}
}
public void updatePicture(Picture item) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
String sql = "UPDATE TAB_PICTURE SET SortOrder = ?, LastUpdate = ?, IsActive = ?, IsSearchable = ?,"
+ "SearchKeywords = ?, PropertyNames = ?, PropertyValues = ? "
+ "WHERE IndexID = ?";
AppLogger.debug("MSSqlPictureDAO.updatePicture().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
fillPictureParameters(stmt, item, TYPE_UPDATE);
int rowCount = stmt.executeUpdate();
if (rowCount != 1)
throw new ResourceDAOSysException("effected row count must be 1");
} catch (Exception e) {
AppLogger.debug("MSSqlPictureDAO.updatePicture().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());
}
} }
public void deletePicture(int pictureId) throws ResourceDAOSysException {
Connection conn = null;
PreparedStatement stmt = null;
String sql = "UPDATE TAB_PICTURE SET Deleted = 1 WHERE IndexID = ?";
AppLogger.debug("MSSqlPictureDAO.deletePicture().sql=" + sql);
try {
conn = super.getDBConnection();
stmt = conn.prepareStatement(sql);
stmt.setInt(1, pictureId);
int rowCount = stmt.executeUpdate();
if (rowCount != 1)
throw new ResourceDAOSysException("effected row count must be 1");
} catch (Exception e) {
AppLogger.debug("MSSqlPictureDAO.deletePicture().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 Picture populatePictureFormResultSet(ResultSet rst) throws SQLException {
Picture item = new Picture();
// Fill section properties.
DBCommonUtils.populateSectionFormResultSet(rst, item);
return item;
}
protected void fillPictureParameters(PreparedStatement stmt, Picture 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());
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());
SerializeData data = item.deserialize();
stmt.setString(index++, data.getKeys());
stmt.setString(index++, data.getValues());
stmt.setInt(index++, item.getIndexId());
}
} catch (SQLException e) {
throw e;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -