ObjectDAOImpl.java
package com.vo;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class ObjectDAOImpl extends JdbcDaoSupport {
private LobHandler lobHandler;
public LobHandler getLobHandler() {
return lobHandler;
}
public void setLobHandler(LobHandler lobHandler ) {
this.lobHandler = lobHandler;
}
@SuppressWarnings("unchecked")
public boolean deleteBySQL(String sql) {
//Display SQL command
System.out.println("正在執行 刪除SQL命令...start");
System.out.println(sql);
try
{
int i = this.getJdbcTemplate().update(sql);
System.out.println("影響記錄筆數:"+i);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("執行 刪除SQL命令...failed");
return false;
}
System.out.println("執行 刪除SQL命令...done");
return true;
}
@SuppressWarnings("unchecked")
public boolean updateBySQL(String sql){
//Display SQL command
System.out.println("正在執行 更新SQL命令...start");
System.out.println(sql);
try
{
int i = this.getJdbcTemplate().update(sql);
System.out.println("影響記錄筆數:"+i);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("執行 更新SQL命令...failed");
return false;
}
System.out.println("執行 更新SQL命令...done");
return true;
}
@SuppressWarnings("unchecked")
public boolean insertBySQL(String sql)
{
//Display SQL command
System.out.println("正在執行 新增SQL命令...start");
System.out.println(sql);
try
{
int i = this.getJdbcTemplate().update(sql);
System.out.println("影響記錄筆數:"+i);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("執行 新增SQL命令...failed");
return false;
}
System.out.println("執行 新增SQL命令...done");
return true;
}
@SuppressWarnings("unchecked")
public List<?> getAll(String sql){
//Display SQL command
System.out.println("正在執行 查詢SQL命令...start");
System.out.println(sql);
//Jdbc Template
System.out.println("執行 查詢SQL命令...done");
return this.getJdbcTemplate().queryForList(sql);
}
/**
* 事務處理,批量更新String[] String是SQL語句
*/
public boolean updateBatch(final List<String> sqlList){
//Display SQL command
System.out.println("正在執行 批次更新SQL命令...start");
try
{
for (String sql:sqlList) {
this.getJdbcTemplate().update(sql);
System.out.println(sql);
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("執行 批次更新SQL命令...failed");
return false;
}
System.out.println("執行 批次更新SQL命令...done");
return true;
}
@SuppressWarnings("unchecked")
public boolean callProcBySQL(String sql) {
//Display SQL command
System.out.println("正在執行 內儲程序 CALL PROCEDURE...start");
System.out.println(sql);
try
{
this.getJdbcTemplate().execute(sql);
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("執行 內儲程序 CALL PROCEDURE...failed");
return false;
}
System.out.println("執行 內儲程序 CALL PROCEDURE...done");
return true;
}
@SuppressWarnings("unchecked")
public boolean updateLobBySQL(String sql,String fileName,final byte[] data) throws IOException {
//Display SQL command
try{
final InputStream blobIs = new ByteArrayInputStream(data);
System.out.println("正在執行 新增/更新 BLOB/CLOB 欄位...start");
System.out.println(sql);
this.getJdbcTemplate().execute(sql, new AbstractLobCreatingPreparedStatementCallback(this.lobHandler)
{
protected void setValues(PreparedStatement ps, LobCreator lobCreator) throws SQLException, DataAccessException
{
lobCreator.setBlobAsBinaryStream(ps, 1, blobIs, (int) data.length);
}
});
blobIs.close();
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("執行 新增/更新 BLOB/CLOB 欄位完成...failed");
return false;
}
System.out.println("執行 新增/更新 BLOB/CLOB 欄位完成...done");
return true;
}
}
全站熱搜