mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve JdbcUtils
This commit is contained in:
@@ -275,7 +275,7 @@ public class MappingEngine {
|
||||
int updateCount = JdbcUtils.executeUpdate(conn, rawSql, context.getParameters());
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
|
||||
public void insert(Connection conn, String sql, Object... parameters) throws SQLException {
|
||||
insert(conn, sql, Arrays.asList(parameters));
|
||||
}
|
||||
@@ -311,7 +311,7 @@ public class MappingEngine {
|
||||
JdbcUtils.close(conn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int delete(String sql, Object... parameters) throws SQLException {
|
||||
return delete(sql, Arrays.asList(parameters));
|
||||
}
|
||||
@@ -335,7 +335,35 @@ public class MappingEngine {
|
||||
JdbcUtils.close(conn);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void insertToTable(String tableName, Map<String, Object> map) throws SQLException {
|
||||
StringBuilder sql = new StringBuilder() //
|
||||
.append("insert into ") //
|
||||
.append(tableName) //
|
||||
.append("("); //
|
||||
|
||||
int nameCount = 0;
|
||||
for (String name : map.keySet()) {
|
||||
if (nameCount > 0) {
|
||||
sql.append(",");
|
||||
}
|
||||
sql.append(name);
|
||||
nameCount++;
|
||||
}
|
||||
sql.append(") values (");
|
||||
for (int i = 0; i < nameCount; ++i) {
|
||||
if (i != 0) {
|
||||
sql.append(",");
|
||||
}
|
||||
sql.append("?");
|
||||
}
|
||||
sql.append(")");
|
||||
|
||||
List<Object> parameters = new ArrayList<Object>(map.values());
|
||||
|
||||
insert(sql.toString(), parameters);
|
||||
}
|
||||
|
||||
public void insert(String sql, Object... parameters) throws SQLException {
|
||||
insert(sql, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import java.sql.Statement;
|
||||
import java.sql.Types;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -509,6 +510,10 @@ public final class JdbcUtils {
|
||||
throw new SQLException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
public static int executeUpdate(DataSource dataSource, String sql, Object... parameters) throws SQLException {
|
||||
return executeUpdate(dataSource, sql, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
public static int executeUpdate(DataSource dataSource, String sql, List<Object> parameters) throws SQLException {
|
||||
Connection conn = null;
|
||||
@@ -565,6 +570,11 @@ public final class JdbcUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Map<String, Object>> executeQuery(DataSource dataSource, String sql, Object... parameters)
|
||||
throws SQLException {
|
||||
return executeQuery(dataSource, sql, Arrays.asList(parameters));
|
||||
}
|
||||
|
||||
public static List<Map<String, Object>> executeQuery(DataSource dataSource, String sql, List<Object> parameters)
|
||||
throws SQLException {
|
||||
Connection conn = null;
|
||||
@@ -639,4 +649,47 @@ public final class JdbcUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertToTable(DataSource dataSource, String tableName, Map<String, Object> data)
|
||||
throws SQLException {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = dataSource.getConnection();
|
||||
insertToTable(conn, tableName, data);
|
||||
} finally {
|
||||
close(conn);
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertToTable(Connection conn, String tableName, Map<String, Object> data) throws SQLException {
|
||||
String sql = makeInsertToTableSql(tableName, data.keySet());
|
||||
List<Object> parameters = new ArrayList<Object>(data.values());
|
||||
execute(conn, sql, parameters);
|
||||
}
|
||||
|
||||
public static String makeInsertToTableSql(String tableName, Collection<String> names) {
|
||||
StringBuilder sql = new StringBuilder() //
|
||||
.append("insert into ") //
|
||||
.append(tableName) //
|
||||
.append("("); //
|
||||
|
||||
int nameCount = 0;
|
||||
for (String name : names) {
|
||||
if (nameCount > 0) {
|
||||
sql.append(",");
|
||||
}
|
||||
sql.append(name);
|
||||
nameCount++;
|
||||
}
|
||||
sql.append(") values (");
|
||||
for (int i = 0; i < nameCount; ++i) {
|
||||
if (i != 0) {
|
||||
sql.append(",");
|
||||
}
|
||||
sql.append("?");
|
||||
}
|
||||
sql.append(")");
|
||||
|
||||
return sql.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.alibaba.druid.bvt.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class JdbcUtilsTest extends TestCase {
|
||||
|
||||
private DruidDataSource dataSource;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
dataSource = new DruidDataSource();
|
||||
dataSource.setMinIdle(1);
|
||||
dataSource.setUrl("jdbc:h2:mem:test;");
|
||||
dataSource.setTestOnBorrow(false);
|
||||
|
||||
JdbcUtils.execute(dataSource, "CREATE TABLE user (id INT, name VARCHAR(40))");
|
||||
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
JdbcUtils.execute(dataSource, "DROP TABLE user");
|
||||
JdbcUtils.close(dataSource);
|
||||
}
|
||||
|
||||
public void test_curd() throws Exception {
|
||||
{
|
||||
List<Map<String, Object>> list = JdbcUtils.executeQuery(dataSource, "select * from user");
|
||||
Assert.assertEquals(0, list.size());
|
||||
}
|
||||
{
|
||||
Map<String, Object> data = new HashMap<String, Object>();
|
||||
data.put("id", 123);
|
||||
data.put("name", "高傲的羊");
|
||||
JdbcUtils.insertToTable(dataSource, "user", data);
|
||||
}
|
||||
{
|
||||
List<Map<String, Object>> list = JdbcUtils.executeQuery(dataSource, "select * from user");
|
||||
Assert.assertEquals(1, list.size());
|
||||
Map<String, Object> data = list.get(0);
|
||||
|
||||
Assert.assertEquals(123, data.get("ID"));
|
||||
Assert.assertEquals("高傲的羊", data.get("NAME"));
|
||||
}
|
||||
{
|
||||
List<Map<String, Object>> list = JdbcUtils.executeQuery(dataSource, "select id \"id\", name \"name\" from user");
|
||||
Assert.assertEquals(1, list.size());
|
||||
Map<String, Object> data = list.get(0);
|
||||
|
||||
Assert.assertEquals(123, data.get("id"));
|
||||
Assert.assertEquals("高傲的羊", data.get("name"));
|
||||
}
|
||||
{
|
||||
JdbcUtils.executeUpdate(dataSource, "delete from user");
|
||||
}
|
||||
{
|
||||
List<Map<String, Object>> list = JdbcUtils.executeQuery(dataSource, "select * from user");
|
||||
Assert.assertEquals(0, list.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user