May*_*yur 2 java mysql arrays jdbc prepared-statement
我在我的prepare语句中遇到错误java.sql.SQLFeatureNotSupportedException.我正在使用Mysql数据库.
以下是我的代码.
class tmp {
public static void main(String arg[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost/sample", "root", "root");
PreparedStatement pst = conn
.prepareStatement("select * from userinfo where firstname in(?)");
String[] Parameter = { "user1", "Administrator" };
Array sqlArray = conn.createArrayOf("VARCHAR", Parameter);
pst.setArray(1, sqlArray);
ResultSet rs = pst.executeQuery();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于Mysql-
在Mysql中无法设置数组.
而不是你可以在循环中形成(?,?,..)的查询以及设置值的相同方式.
String[] Parameter = { "user1", "Administrator" };
String query = "select * from userinfo where firstname in (";
String temp = "";
for(i = 0; i < Parameter.length; i++) {
temp += ",?";
}
temp = temp.replaceFirst(",", "");
temp += ")";
query = query + temp;
PreparedStatement pst = conn.prepareStatement(query);
Run Code Online (Sandbox Code Playgroud)
所以查询变成了
select * from userinfo where firstname in (?,?)
并使用循环传递值.
对于Oracle-
ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor("CHAR_ARRAY", conn);
String[] Parameter = { "user1", "Administrator" };
java.sql.Array sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content);
.
.
pstmt.setArray(1, sqlArray);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10327 次 |
| 最近记录: |