【发布时间】:2017-05-06 09:09:57
【问题描述】:
我遇到了一个问题,据我所知,整个代码一直有效,直到pst.executeQuery 我只是不明白为什么会出现这个问题。我有一个更新/插入方法,没有关于它们的问题。我正在使用标签来显示图像,所以是photoLabel.setIcon();还是我完全偏离了轨道。我有一个用于将图像加载到标签中的文件选择器的按钮,保存按钮(此功能)应该只需在名为 Images 的 12 列/字段中写入数据库。
请注意 - System.out.Println用于测试目的,只是
System.out.println("Working 5");
不会显示,因此我知道它与 pst.executeQuery() 有关。我试过在网上搜索,使用不同的 .execute 方法,我试过提交连接等等。唉,没有运气。
@Override
public void actionPerformed(ActionEvent e) {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
// String s = null;
try {
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:employeeDatabase.sqlite");
//connection.setAutoCommit(false);
System.out.println("Working 1");
InputStream is = new FileInputStream(new File(s));
System.out.println("Working 2");
String sql = "insert into employees(Images) values(?)";
PreparedStatement pst = connection.prepareStatement(sql);
System.out.println("Working 3");
pst.setBlob(12, is);
System.out.println("Working 4");
pst.executeQuery();
connection.commit();
System.out.println("Working 5");
JOptionPane.showMessageDialog(null, "Data Inserted");
}
}
catch ( Exception e1 ) {
JOptionPane.showMessageDialog(null, "Error");
}
}});
这是通过使用 JFileChooser 来选择图像的方法
uploadImage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.JPG", "jpg","gif","png");
fileChooser.addChoosableFileFilter(filter);
fileChooser.addChoosableFileFilter(filter);
fileChooser.setMultiSelectionEnabled(false);
int result = fileChooser.showSaveDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
File selectedFile = fileChooser.getSelectedFile();
String path = selectedFile.getAbsolutePath();
photoLabel.setIcon(ResizeImage(path));
s = path;
}
else if(result == JFileChooser.CANCEL_OPTION){
System.out.println("No Data");
}
}
});
编辑 -
我的意思是,我没有出错,程序没有中断。只是图像不会上传到数据库,代码System.out.println("Working 5"); 不会打印到控制台。所以它似乎在那一点上被卡住/冻结了。
【问题讨论】:
-
定义不工作
-
@ScaryWombat 我没有收到任何错误,程序没有中断。只是不会将图像上传到数据库和代码 System.out.println("Working 5");不打印到控制台。
-
在您的代码中,您有一个
try- 您是否忽略了问题? -
@ScaryWombat 是的,忘记在我的原始帖子中包含 catch 异常。我现在已经添加了。它似乎只能到达
pst.executeQuery,然后到达catch。