Java SQL Update ExecuteUpdate(String sql, List paras)

Here you can find the source of ExecuteUpdate(String sql, List paras)

Description

Execute Update

License

Apache License

Declaration

public static int ExecuteUpdate(String sql, List<String> paras) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import java.sql.SQLException;
import java.util.List;

public class Main {
    private static String dbConnectionStr = "jdbc:sqlserver://127.0.0.1:1433;databasename=cyyz";
    private static String sUser = "sa";
    private static String sPassWord = "Ss123";
    private static String dbDriverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static Connection dbConnection;

    public static int ExecuteUpdate(String sql, List<String> paras) {
        int iRet = 0;
        try {/*from  w ww  .  j  a  v  a2  s.  c  om*/
            GetConnection();
            PreparedStatement stm = dbConnection.prepareStatement(sql);
            if (stm != null) {
                for (int i = 0; i < paras.size(); i++) {
                    stm.setString(i + 1, paras.get(0));
                }
                iRet = stm.executeUpdate(sql);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            EndConnection();
        }
        return iRet;
    }

    private static void GetConnection() throws Exception {
        if (dbConnection == null) {
            Class.forName(dbDriverName);
            dbConnection = DriverManager.getConnection(dbConnectionStr, sUser, sPassWord);
        }
    }

    private static void EndConnection() {
        if (dbConnection != null) {
            try {
                dbConnection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                dbConnection = null;
            }
        }
    }
}

Related

  1. executeUpdate(List paramList, Connection conn, PreparedStatement pstmt)
  2. executeUpdate(PreparedStatement preparedStatement, List paramList)
  3. executeUpdate(PreparedStatement ps)
  4. executeUpdate(String parameterizedSQL, List values, Connection conn)
  5. executeUpdate(String queryStatement, Connection database)
  6. executeUpdateNoCommit(Connection c, String query)
  7. executeUpdatePreparedStm(Connection conn, String preparedStm, Object... sqlParameters)
  8. ExecuteUpdateQuery(String query, Connection con)
  9. executeUpdateSP(Connection conn, String spName, Object... sqlParameters)

  10. HOME | Copyright © www.java2s.com 2016