本文整理汇总了Java中org.springframework.jdbc.support.JdbcUtils.closeStatement方法的典型用法代码示例。如果您正苦于以下问题:Java JdbcUtils.closeStatement方法的具体用法?Java JdbcUtils.closeStatement怎么用?Java JdbcUtils.closeStatement使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.jdbc.support.JdbcUtils
的用法示例。
在下文中一共展示了JdbcUtils.closeStatement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getNextKey
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
/**
* Executes the SQL as specified by {@link #getSequenceQuery()}.
*/
@Override
protected long getNextKey() throws DataAccessException {
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
ResultSet rs = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
rs = stmt.executeQuery(getSequenceQuery());
if (rs.next()) {
return rs.getLong(1);
}
else {
throw new DataAccessResourceFailureException("Sequence query did not return a result");
}
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not obtain sequence value", ex);
}
finally {
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
示例2: executeSchemaScript
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
/**
* Execute the given schema script on the given JDBC Connection.
* <p>Note that the default implementation will log unsuccessful statements
* and continue to execute. Override the {@code executeSchemaStatement}
* method to treat failures differently.
* @param con the JDBC Connection to execute the script on
* @param sql the SQL statements to execute
* @throws SQLException if thrown by JDBC methods
* @see #executeSchemaStatement
*/
protected void executeSchemaScript(Connection con, String[] sql) throws SQLException {
if (sql != null && sql.length > 0) {
boolean oldAutoCommit = con.getAutoCommit();
if (!oldAutoCommit) {
con.setAutoCommit(true);
}
try {
Statement stmt = con.createStatement();
try {
for (String sqlStmt : sql) {
executeSchemaStatement(stmt, sqlStmt);
}
}
finally {
JdbcUtils.closeStatement(stmt);
}
}
finally {
if (!oldAutoCommit) {
con.setAutoCommit(false);
}
}
}
}
示例3: loadTablePrimaryKeys
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
private List<String> loadTablePrimaryKeys(String tableName)throws Exception{
DataSource ds=this.getJdbcTemplate().getDataSource();
Connection con = DataSourceUtils.getConnection(ds);
List<String> primaryKeyList=new ArrayList<String>();
Statement stmt = null;
ResultSet rs=null;
try{
DatabaseMetaData metaData = con.getMetaData();
rs = metaData.getPrimaryKeys(null, null, tableName.toUpperCase());
while (rs.next()) {
primaryKeyList.add(rs.getString("COLUMN_NAME"));
}
}finally{
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
JdbcUtils.closeConnection(con);
}
return primaryKeyList;
}
示例4: execute
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
public Object execute(StatementCallback action) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
Connection conToUse = con;
if (getNativeJdbcExtractor() != null &&
getNativeJdbcExtractor().isNativeConnectionNecessaryForNativeStatements()) {
conToUse = getNativeJdbcExtractor().getNativeConnection(con);
}
stmt = conToUse.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
applyStatementSettings(stmt);
Statement stmtToUse = stmt;
if (getNativeJdbcExtractor() != null) {
stmtToUse = getNativeJdbcExtractor().getNativeStatement(stmt);
}
Object result = action.doInStatement(stmtToUse);
handleWarnings(stmt);
return result;
}
catch (SQLException ex) {
// Release Connection early, to avoid potential connection pool deadlock
// in the case when the exception translator hasn't been initialized yet.
JdbcUtils.closeStatement(stmt);
stmt = null;
DataSourceUtils.releaseConnection(con, getDataSource());
con = null;
throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
示例5: getNextKey
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
@Override
protected synchronized long getNextKey() throws DataAccessException {
if (this.nextValueIndex < 0 || this.nextValueIndex >= getCacheSize()) {
/*
* Need to use straight JDBC code because we need to make sure that the insert and select
* are performed on the same connection (otherwise we can't be sure that @@identity
* returnes the correct value)
*/
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
this.valueCache = new long[getCacheSize()];
this.nextValueIndex = 0;
for (int i = 0; i < getCacheSize(); i++) {
stmt.executeUpdate("insert into " + getIncrementerName() + " default values");
ResultSet rs = stmt.executeQuery("select @@identity");
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("@@identity failed after executing an update");
}
this.valueCache[i] = rs.getLong(1);
}
finally {
JdbcUtils.closeResultSet(rs);
}
}
long maxValue = this.valueCache[(this.valueCache.length - 1)];
stmt.executeUpdate("delete from " + getIncrementerName() + " where " + getColumnName() + " < " + maxValue);
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not increment identity", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
return this.valueCache[this.nextValueIndex++];
}
示例6: closeQuietly
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
static final void closeQuietly(Statement st) {
if (Env.SingletonHolder.getInstance().isSpring()) {
if (st != null) JdbcUtils.closeStatement(st);
} else {
if (st != null) {
try {
st.close();
} catch (SQLException e) {
}
}
}
}
示例7: execute
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
@Override
public <T> T execute(StatementCallback<T> action) throws DataAccessException {
Assert.notNull(action, "Callback object must not be null");
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
Connection conToUse = con;
if (this.nativeJdbcExtractor != null &&
this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements()) {
conToUse = this.nativeJdbcExtractor.getNativeConnection(con);
}
stmt = conToUse.createStatement();
applyStatementSettings(stmt);
Statement stmtToUse = stmt;
if (this.nativeJdbcExtractor != null) {
stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);
}
T result = action.doInStatement(stmtToUse);
handleWarnings(stmt);
return result;
}
catch (SQLException ex) {
// Release Connection early, to avoid potential connection pool deadlock
// in the case when the exception translator hasn't been initialized yet.
JdbcUtils.closeStatement(stmt);
stmt = null;
DataSourceUtils.releaseConnection(con, getDataSource());
con = null;
throw getExceptionTranslator().translate("StatementCallback", getSql(action), ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
示例8: query
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
/**
*
* */
public Map<String, Map<String, Object>> query(String sql, String key) {
try {
Map<String, Map<String, Object>> resultMap = new KeyMap<Map<String, Object>>();
Connection conn = DataSourceUtils.getConnection(dataSource);
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
while (rs.next()) {
Map<String, Object> itemMap = new KeyMap<Object>();
for (int x = 0; x < rsmd.getColumnCount(); x++) {
itemMap.put(rsmd.getColumnLabel(x + 1), rs.getObject(x + 1));
}
resultMap.put(StringTools.toString(itemMap.get(key)), itemMap);
}
} catch (Exception e) {
MyDogLog.getLog().error(" query ", e);
} finally {
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(ps);
DataSourceUtils.releaseConnection(conn, dataSource);
}
return resultMap;
} finally {
DataSourceUtils.releaseConnection(DataSourceUtils.getConnection(dataSource), dataSource);
}
}
示例9: loadDbTables
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
@DataProvider
public Collection<TableDef> loadDbTables() throws Exception{
Collection<TableDef> result=new ArrayList<TableDef>();
DataSource ds=this.getJdbcTemplate().getDataSource();
Connection con = DataSourceUtils.getConnection(ds);
Statement stmt = null;
ResultSet rs=null;
try{
DatabaseMetaData metaData = con.getMetaData();
String url=metaData.getURL();
String schema=null;
if(url.toLowerCase().contains("oracle")){
schema=metaData.getUserName();
}
rs = metaData.getTables(null,schema, "%",new String[] { "TABLE" });
while (rs.next()) {
TableDef table=new TableDef();
table.setName(rs.getString("TABLE_NAME"));
table.setPrimaryKeys(loadTablePrimaryKeys(table.getName(),metaData));
result.add(table);
}
}finally{
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
JdbcUtils.closeConnection(con);
}
return result;
}
示例10: getNextKey
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
@Override
protected synchronized long getNextKey() throws DataAccessException {
if (this.nextValueIndex < 0 || this.nextValueIndex >= getCacheSize()) {
/*
* Need to use straight JDBC code because we need to make sure that the insert and select
* are performed on the same connection (otherwise we can't be sure that @@identity
* returns the correct value)
*/
Connection con = DataSourceUtils.getConnection(getDataSource());
Statement stmt = null;
try {
stmt = con.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
this.valueCache = new long[getCacheSize()];
this.nextValueIndex = 0;
for (int i = 0; i < getCacheSize(); i++) {
stmt.executeUpdate(getIncrementStatement());
ResultSet rs = stmt.executeQuery(getIdentityStatement());
try {
if (!rs.next()) {
throw new DataAccessResourceFailureException("Identity statement failed after inserting");
}
this.valueCache[i] = rs.getLong(1);
}
finally {
JdbcUtils.closeResultSet(rs);
}
}
stmt.executeUpdate(getDeleteStatement(this.valueCache));
}
catch (SQLException ex) {
throw new DataAccessResourceFailureException("Could not increment identity", ex);
}
finally {
JdbcUtils.closeStatement(stmt);
DataSourceUtils.releaseConnection(con, getDataSource());
}
}
return this.valueCache[this.nextValueIndex++];
}
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:41,代码来源:AbstractIdentityColumnMaxValueIncrementer.java
示例11: close
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
public void close(boolean closeConnection) {
JdbcUtils.closeResultSet(rs);
rs = null;
JdbcUtils.closeStatement(ps);
ps = null;
if (closeConnection) {
JdbcUtils.closeConnection(con);
}
con = null;
}
示例12: doUnlock
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
@Override
protected void doUnlock() {
Integer locked = this.locked.get();
if (locked == null || locked != 1) {
return;
}
//
Integer unlocked = null;
Connection conn = null;
ResultSet rs = null;
PreparedStatement ps = null;
try {
conn = this.connection.get();
ps = conn.prepareStatement(this.unlockSql);
// lock id
ps.setString(1, this.id);
//
rs = ps.executeQuery();
if (rs.next()) {
unlocked = rs.getInt(1);
if (rs.wasNull()) {
unlocked = null;
}
}
//
if (unlocked == null || unlocked == 0) {
// 無法釋放鎖, 有可能是重複釋放, 只寫log, 不拋出ex
LOGGER.warn("Could not release lock: " + id);
}
} catch (Throwable e) {
throw new DistributedLockException("Could not release lock: " + id, e);
} finally {
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(ps);
this.connection.set(null);
// conn return to pool
JdbcUtils.closeConnection(conn);
this.locked.set(null);
}
}
示例13: listMonitor
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
@Override
public List<Monitor> listMonitor()
{
List<Monitor> monitors = new ArrayList<Monitor>();
Connection con = null;
ResultSet rs = null;
PreparedStatement stmt = null;
try
{
con = dataSource.getConnection();
con.setReadOnly(true);
stmt = con.prepareStatement("select * from t_monitor");
rs = stmt.executeQuery();
while(rs.next()) {
monitors.add(createMonitor(rs));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
JdbcUtils.closeConnection(con);
}
return monitors;
}
示例14: queryMonitorJsonList
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
@Override
public String queryMonitorJsonList()
{
StringBuilder json = new StringBuilder(1000);
Connection con = null;
ResultSet rs = null;
PreparedStatement stmt = null;
try
{
con = dataSource.getConnection();
con.setReadOnly(true);
stmt = con.prepareStatement("select * from t_monitor");
rs = stmt.executeQuery();
while(rs.next()) {
json.append(buildJson(rs));
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
JdbcUtils.closeResultSet(rs);
JdbcUtils.closeStatement(stmt);
JdbcUtils.closeConnection(con);
}
return json.toString();
}
示例15: doUpdate
import org.springframework.jdbc.support.JdbcUtils; //导入方法依赖的package包/类
public int doUpdate(SBulletin bul) throws Exception{
String sql = "update s_bulletin " +
" set BULLETIN_TITLE = ?, " +
// " CREATE_DATE = ?, " +
" BULLETIN_CONTENT= ?, " +
// " BULLETIN_PUBLISHER= ?, " +
// " STATUS= ?, " +
// " OPTR_ID= ?, " +
" EFF_DATE= ?, " +
" EXP_DATE= ? " +
" where bulletin_id = ?";
Connection conn = this.getConnection();
PreparedStatement pst = null;
int result = 0;
try{
pst = conn.prepareStatement(sql);
pst.setString(1, bul.getBulletin_title());//BULLETIN_TITLE
C3P0NativeJdbcExtractor cp30NativeJdbcExtractor = new C3P0NativeJdbcExtractor();
OracleConnection oracleConn = (OracleConnection) cp30NativeJdbcExtractor.getNativeConnection(pst.getConnection());
CLOB clob = oracle.sql.CLOB.createTemporary(oracleConn, false, oracle.sql.CLOB.DURATION_SESSION);
clob.putString(1,bul.getBulletin_content());
pst.setClob(2, clob);//BULLETIN_CONTENT
pst.setDate(3, new java.sql.Date(bul.getEff_date().getTime()));//EFF_DATE
pst.setDate(4, new java.sql.Date(bul.getExp_date().getTime()));//EXP_DATE
pst.setString(5, bul.getBulletin_id());
result = pst.executeUpdate();
}catch (SQLException ex) {
JdbcUtils.closeStatement(pst);
pst = null;
DataSourceUtils.releaseConnection(conn, getDataSource());
conn = null;
throw ex;
}finally {
JdbcUtils.closeStatement(pst);
DataSourceUtils.releaseConnection(conn, getDataSource());
}
return result;
}