本文整理匯總了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;
}