import java.io.Writer;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.sql.CLOB;
import com.icss.common.CommonConfig;
/**
* <title>kmdp</title>
* <description>更新大字段专用类</description>
* @author yinjin
* <copyright></copyright>
* @version 1.0
* 2005-7-29 15:06:18
*/
public class LobUpdateUtil
{
/**
* 用于weblogic环境下的clob字段更新,在其他web环境下可以不用这个方法
* @param content clob字段的内容
* @param tableName 表名
* @param fieldName Clob字段名
* @param sqlWhere 带where关键字的where条件
* @author 殷晋
*/
public static void updateClob(String content,String tableName,String fieldName,String sqlWhere)
{
String sqlSelect = "SELECT "+ fieldName +" FROM "+ tableName + sqlWhere + " FOR UPDATE ";
String sqlUpdate = "UPDATE "+ tableName +" SET "+ fieldName +" = empty_clob() "+ sqlWhere;
Connection conn = null;
try {
conn = getConnection();
conn.setAutoCommit(false);
PreparedStatement pst = conn.prepareStatement(sqlUpdate);
pst.executeUpdate();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sqlSelect);
if(rs.next())
{
CLOB m_clob = ((oracle.jdbc.OracleResultSet)rs).getCLOB(fieldName.toUpperCase());
Writer wr = m_clob.getCharacterOutputStream();
wr.write(content);
wr.flush();
wr.close();
}
conn.commit();
rs.close();
pst.close();
st.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
colseConnection(conn);
}
}
/**
* 获取数据库链接,根据conmon_config.xml配置
* @return 数据库链接
*/
private static Connection getConnection()
{
try {
String driverClassName = CommonConfig.getValue("uwf.database.defaultProvider.driver");
String dbUrl = CommonConfig.getValue("uwf.database.defaultProvider.serverURL");
String username = CommonConfig.getValue("uwf.database.defaultProvider.username");
String password = CommonConfig.getValue("uwf.database.defaultProvider.password");
Class.forName(driverClassName);
Connection conn = null;
conn = DriverManager.getConnection(dbUrl, username, password);
return conn;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
/**
* 关闭数据库连接
* @param conn 等待关闭的数据库链接
*/
private static void colseConnection(Connection conn)
{
try {
if(conn != null && !conn.isClosed())
{
conn.close();
conn = null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
conn = null;
}
}
}