想调试一个使用JDBC从数据库读取Blob类型数据的程序,但是试了很多次debug不了其中的while循环
在Servlet实现类的doPost方法中试了一个简单的while循环,发现可以调试
int a=11;
int i=0;
while (++i<a){
System.out.println(i);
}
代码
Servlet doPost
String sql = "SELECT * FROM resource WHERE id = ?";
String[] path = new String[1];
path[0] = "F:\\program design\\JavaWeb\\web\\resource\\2.png";
//下面的方法传入数据库连接,接收数据的类,sql语句以及要将数据库中提取的文件放入的路径和sql语句中?的替代
//返回接收数据的类的实例的集合
ArrayList<Datas> arrayList = JDBC.searchBolb(connection, Datas.class, sql, path, 1);
PrintWriter writer = response.getWriter();
for (Datas d : arrayList) {
writer.println(d);
}
JDBC
//重点请看while循环,其他错误只要能调试while循环我自己慢慢找
public static <T> ArrayList<T> searchBolb(Connection connection, Class<T> clazz, String sql, String[] resourcePath, Object... args) {
//数据库表中最后一个字段为Blob类型
//输出类中封装Blob资源输出地址
PreparedStatement ps = null;
ResultSet result = null;
ResultSetMetaData rsmd = null;
InputStream is = null;
FileOutputStream fos = null;
int resourcePathNumber = 0;
ArrayList<T> collection = new ArrayList<T>();
try {
ps = connection.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i + 1, args[i]);
}
result = ps.executeQuery();
rsmd = result.getMetaData();
while (result.next()) {
int columnCount = rsmd.getColumnCount();
T t = clazz.getDeclaredConstructor().newInstance();
for (int i = 0; i < columnCount - 1; i++) {
Object columnValue = result.getObject(i + 1);
String columnLabel = rsmd.getColumnLabel(i + 1);
Field field = clazz.getDeclaredField(columnLabel);
field.setAccessible(true);
field.set(t, columnValue);
}
Blob blob = result.getBlob(columnCount);
String blobLabel = rsmd.getColumnLabel(columnCount);
Field field = clazz.getDeclaredField(blobLabel);
field.setAccessible(true);
field.set(t, resourcePath);
is = blob.getBinaryStream();
fos = new FileOutputStream(resourcePath[resourcePathNumber++]);
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
collection.add(t);
}
} catch (SQLException | IOException | NoSuchFieldException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
e.printStackTrace();
} finally {
try {
ps.close();
is.close();
fos.close();
} catch (SQLException | IOException e) {
e.printStackTrace();
}
if (collection == null) {
System.out.println("Failed to search");
}
return collection;
}
}
把断点设在144行(while循环第一行),运行到for循环第一行出错(用的f8,不进入方法体)(没有while循环过,这是第一次for循环),这里的错误是结果集关闭,但是显然并没有任何操作使之关闭(我试了另外一个没有问题的方法,报的同样的错误),怀疑是刚开始debug时就已经进行完了while循环,导致结果集关闭
请问需要把断点设在哪里,或者是怎么样才能解决上述问题?3Q