@@ -42,7 +42,7 @@ public class DemoSQLExecutor extends AbstractSQLExecutor {
42
42
} catch (ClassNotFoundException e ) {
43
43
e .printStackTrace ();
44
44
}
45
-
45
+
46
46
try { //加载驱动程序
47
47
Class .forName ("org.postgresql.Driver" );
48
48
Log .d (TAG , "成功加载 PostgresSQL 驱动!" );
@@ -65,6 +65,8 @@ public int executeUpdate(@NotNull SQLConfig config) throws Exception {
65
65
}
66
66
67
67
68
+ //TODO 根据不同数据库来分组存 connection 和 statement,例如 Map<database, connection>,
69
+ // 解决一次请求中有2个以上不同数据库类型导致后面的查询都用第一个的数据库类型
68
70
private Connection connection = null ;
69
71
private PreparedStatement statement = null ;
70
72
/**
@@ -76,7 +78,6 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
76
78
if (connection == null || connection .isClosed ()) {
77
79
Log .i (TAG , "select connection " + (connection == null ? " = null" : ("isClosed = " + connection .isClosed ()))) ;
78
80
79
-
80
81
if (DemoSQLConfig .DATABASE_POSTGRESQL .equalsIgnoreCase (config .getDatabase ())) { //PostgreSQL 不允许 cross-database
81
82
connection = DriverManager .getConnection (config .getDBUri () + "/" + config .getSchema (), config .getDBAccount (), config .getDBPassword ());
82
83
}
@@ -85,14 +86,14 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti
85
86
+ config .getDBAccount () + "&password=" + config .getDBPassword ());
86
87
}
87
88
}
88
-
89
+
89
90
statement = connection .prepareStatement (config .getSQL (config .isPrepared ())); //创建Statement对象
90
91
List <Object > valueList = config .isPrepared () ? config .getPreparedValueList () : null ;
91
-
92
+
92
93
if (valueList != null && valueList .isEmpty () == false ) {
93
-
94
+
94
95
for (int i = 0 ; i < valueList .size (); i ++) {
95
-
96
+
96
97
if (DemoSQLConfig .DATABASE_POSTGRESQL .equalsIgnoreCase (config .getDatabase ())) {
97
98
statement .setObject (i + 1 , valueList .get (i )); //PostgreSQL JDBC 不支持隐式类型转换 tinyint = varchar 报错
98
99
}
0 commit comments