Here you can find the source of update(DataSource dataSource, String sql, Object params[])
Parameter | Description |
---|---|
dataSource | a parameter |
sql | a parameter |
params | a parameter |
public static int update(DataSource dataSource, String sql, Object params[])
//package com.java2s; //License from project: Open Source License import javax.sql.DataSource; import java.sql.*; public class Main { /**/*from ww w . j a v a 2 s . c o m*/ * update * * @param dataSource * @param sql * @param params */ public static int update(DataSource dataSource, String sql, Object params[]) { Connection connection = getConn(dataSource); PreparedStatement preparedStatement = null; int ret = 0; try { preparedStatement = connection.prepareStatement(sql); if (params != null) { for (int i = 0; i < params.length; i++) { preparedStatement.setObject(i + 1, params[i]); } } ret = preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { release(connection, preparedStatement, null); } return ret; } private static Connection getConn(DataSource dataSource) { try { return dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } /** * release * @param connection * @param preparedStatement * @param resultSet */ public static void release(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (preparedStatement != null) { try { preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } } } }