我正在尝试将从ResultSet.getArray()获得的SQL数组转换为String []数组,但是却遇到异常:-
这是我设置PreparedStatement的方式:
<i className={style['test-wrapper']} />
这是我从数据库中检索该数组的方式:
org.postgresql.util.PSQLException: This connection has been closed.
这行pst = conn.prepareStatement("insert into normalposts (authorid,message,images,timestamp) values(?,?,?,?)");
pst.setString(1, u.getAuthorId());
pst.setString(2, u.getMessage());
// Setting the array
if(u.getImages() != null){
// converting String[] to SQL Array
Array sqlArray = conn.createArrayOf("VARCHAR", u.getImages());
// ^this returns a String[]
pst.setArray(3, sqlArray);
sqlArray.free();
} else {
pst.setArray(3, null);
}
pst.setObject(4, LocalDateTime.now());
为什么引发异常,我该如何解决?
更新
我发现了我的错误。发生异常是因为在运行这行代码Array a = rs.getArray("images");
String[] images = (String[])a.getArray(); //this line causes the exception
这些是我所做的更改: 我将conn.close()置于其自己的公共函数中,并在运行有问题的行后在 之后调用了该函数,如下所示:
String[] images = (String[])a.getArray();