Here you can find the source of execute(Connection conn, String sql, Object[] args)
Executes the given sql string.
Parameter | Description |
---|---|
conn | the database connection |
sql | the sql string to execute |
args | the sql argument objects |
Parameter | Description |
---|---|
SQLException | when error occurs during execution. |
private static void execute(Connection conn, String sql, Object[] args) throws SQLException
//package com.java2s; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class Main { /**/*from ww w . j ava 2 s .co m*/ * <p> * Executes the given sql string. * </p> * @param conn the database connection * @param sql the sql string to execute * @param args the sql argument objects * @throws SQLException when error occurs during execution. */ private static void execute(Connection conn, String sql, Object[] args) throws SQLException { PreparedStatement stmt = null; try { stmt = conn.prepareStatement(sql); for (int i = 0; i < args.length;) { Object obj = args[i++]; if (obj instanceof Long) { stmt.setLong(i, ((Long) obj).longValue()); } else if (obj instanceof Double) { stmt.setDouble(i, ((Double) obj).doubleValue()); } else if (obj instanceof Boolean) { stmt.setBoolean(i, ((Boolean) obj).booleanValue()); } else if (obj instanceof String) { stmt.setString(i, (String) obj); } } stmt.executeUpdate(); } finally { close(stmt); } } /** * <p> * Closes the sql statement. * </p> * @param statement the statement to be closed. */ private static void close(PreparedStatement statement) { if (statement != null) { try { statement.close(); } catch (SQLException e) { // ignore } } } /** * <p> * Closes the sql connection. * </p> * @param connection the connection to be closed. */ private static void close(Connection connection) { if (connection != null) { try { connection.close(); } catch (SQLException e) { // ignore } } } }