美好的一天。我想使用pepraredStatement插入数据库。但是,每当我添加数据库部分(连接和pepraredStatement)时,上传' UPLOAD'按钮没有响应。但当我删除任何与数据库相关的内容时,我的所有按钮都正常工作。你可以在这里找到代码http://pastebin.com/euKdWhr2。
我将非常感谢任何帮助或建议。可能我在数据库部分遗漏了一些东西。
public void actionPerformed(ActionEvent ev)
{
String file = fileField.getText();
SetGetQuestionFileName pattern = new SetGetQuestionFileName(file);
ConnectToDatabase database = new ConnectToDatabase();
try
{
///////// check whether textfile is empty or not
if( ev.getActionCommand().equals("UPLOAD"))
{
if(fileField.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,"File field can not be empty!!! Please try again.","ALERT", JOptionPane.ERROR_MESSAGE);
}
else
{
File fi = new File(fileField.getText());
//////////////// perform upload
try
{
String sql = "INSERT INTO testsystem.questionnaire (category_questions, questions, correct_answer)" + "VALUES (?, ?, ?)";
PreparedStatement st = null;
Connection dbconnection = database.getConnection();
st = dbconnection.prepareStatement(sql);
if(fi.getAbsoluteFile().exists())
{
List<String> lines = Files.readAllLines(Paths.get(fileField.getText()), Charset.defaultCharset());
for (int i = 0; i < lines.size(); i+=10)
{
String category = lines.get(i);
System.out.println(category);
String question = lines.get(i+1);
System.out.println(question);
String answers =
lines.get(i+2)+System.lineSeparator()
+lines.get(i+3)+System.lineSeparator()
+lines.get(i+4)+System.lineSeparator()
+lines.get(i+5);
System.out.println(answers);
String correct = lines.get(i+7);
System.out.println("correct answer is: "+correct);
System.out.println("----------------");
st.setString(1, category);
st.setString(2, answers);
st.setString(3, correct);
st.executeUpdate();
}
JOptionPane.showMessageDialog(null,"File has been successfully uploaded in the database.","NOTIFCATION",JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(null,"File could not be found. Please try again","ALERT",JOptionPane.ERROR_MESSAGE);
}
catch(SQLException ex)
{
}
catch(Exception ex)
{
}
答案 0 :(得分:6)
ui线程调用actionPerformed()
方法。如果你在这个线程中执行长时间运行的任务,ui将无法响应,因为ui线程不能再像ui那样重新工作或只是处理用户输入。
考虑使用SwingWorker并阅读有关concurrency in java的教程。
修改强>
我去了拟议的网站,但我并不真正理解你对SwingWorker的意思。我还没有真正习惯线程
以下是如何SwingWorker
和长时间运行的后台任务的工作示例。
public class SwingWorkerExample {
public static class ProgressSimulatorSwingWoker extends SwingWorker<Void, Void> {
private BoundedRangeModel progressModel;
public ProgressSimulatorSwingWoker(BoundedRangeModel progressModel) {
this.progressModel = progressModel;
}
@Override
protected Void doInBackground() throws Exception {
int start = 0;
int end = 100;
progressModel.setMinimum(start);
progressModel.setMaximum(end);
for (int i = start; i <= end; i++) {
progressModel.setValue(i);
Thread.sleep(50);
}
return null;
}
}
public static void main(String[] args) {
JFrame jFrame = new JFrame("SwingWorker example");
jFrame.setSize(640, 150);
jFrame.setLocationRelativeTo(null); // center on screen
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = jFrame.getContentPane();
contentPane.setLayout(new BorderLayout());
JProgressBar jProgressBar = new JProgressBar();
final BoundedRangeModel model = jProgressBar.getModel();
JButton jButton = new JButton("Simulate Progress");
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ProgressSimulatorSwingWoker progressSimulatorSwingWoker = new ProgressSimulatorSwingWoker(model);
progressSimulatorSwingWoker.execute();
}
});
contentPane.add(jProgressBar, BorderLayout.CENTER);
contentPane.add(jButton, BorderLayout.SOUTH);
jFrame.setVisible(true);
}
}