Java DataSource update(DataSource dataSource, String sql, Object params[])

Here you can find the source of update(DataSource dataSource, String sql, Object params[])

Description

update

License

Open Source License

Parameter

Parameter Description
dataSource a parameter
sql a parameter
params a parameter

Declaration

public static int update(DataSource dataSource, String sql, Object params[]) 

Method Source Code


//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();
            }
        }
    }
}

Related

  1. registerSpringDataSource(String name, DataSource dataSource)
  2. selectCount(DataSource ds, String tableName)
  3. setUpJndiDatasource()
  4. sqlExe(DataSource dataSource, String sql)
  5. tableRecommendationExistsHsqdb(DataSource dataSource)
  6. update_Db(DataSource ds, String query)
  7. waitUntilDatabaseIsAvailable(DataSource dataSource, int timeoutSeconds)