Java第九次作业

时间:2022-03-17 21:59:56

《Java技术》第XX次作业

(一)学习总结

1.用思维导图对本周的学习内容进行总结。

参考资料: XMind。

Java第九次作业

2.通过实验内容中的具体实例说明在执行executeUpdate()方法和executeQuery()方法中使用动态参数时,为什么要使用PreparedStatement接口而不使用Statement,比较使用两种接口的不同之处。

代码:

import java.io.*;
public class test01 {
public static void main(String args[]) {
FileInputStream in = null;
FileOutputStream out = null;
File fSource = new File("e:" + File.separator + "PowerDesigner"+ File.separator + "2.png");
File fDest = new File("e:" + File.separator + "PowerDesigner" + File.separator
+ "1.png");
if (!fSource.exists()) {
System.out.println("源文件不存在");
System.exit(1);
}
if (!fDest.getParentFile().exists()) {
fDest.getParentFile().mkdirs();
}
try {
in = new FileInputStream(fSource);
out = new FileOutputStream(fDest);
byte[] b = new byte[1024];
int len = 0;
long begintime = System.currentTimeMillis();
while ((len = in.read(b)) != -1) {
out.write(b,0,len);
}
long endtime = System.currentTimeMillis();
System.out.println("文件拷贝完成,耗时" + (endtime - begintime) + "毫秒");
} catch (Exception e) {
System.out.println("文件操作失败");
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

执行结果:

使用Statement文件拷贝,是一个字节一个字节的拷贝,耗时1430毫秒;

使用PreparedStatement文件拷贝,先将内容拷贝到缓冲流中,耗时31毫秒

(二)实验总结

  • 程序设计思路:根据老师给出的程序范例,更改上次作业中相应的部分,包括数据库

  • 实验问题分析:

问题:数据库连接不上

原因:数据库要打开并“登录”才能连接上

解决方案:打开数据库,连接到服务器

(三)代码托管

  • 码云commit历史截图
    Java第九次作业