org.joyqueue.nsr.sql.util.DBUtils Maven / Gradle / Ivy
The newest version!
package org.joyqueue.nsr.sql.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* DBUtils
* author: gaohaoxiang
* date: 2019/8/1
*/
public class DBUtils {
public static String insert(Connection connection, String sql, Object... params) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
fillParams(preparedStatement, params);
preparedStatement.execute();
ResultSet generatedKeys = preparedStatement.getGeneratedKeys();
while (generatedKeys.next()) {
return generatedKeys.getString(1);
}
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static int update(Connection connection, String sql, Object... params) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
fillParams(preparedStatement, params);
return preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static int delete(Connection connection, String sql, Object... params) {
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql);
fillParams(preparedStatement, params);
return preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static List