안녕하세요. pepraredStatement를 사용하여 데이터베이스에 삽입하려고합니다. 그러나 내가 데이터베이스 부분 (connection과 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)
{
}
처리하지 않고 예외를 잡아 내지 마십시오. 아니면 당신이 그들을 다시 던지거나, 최소한, 그들을 기록하십시오. 또한 리스너를 시작하는 스레드에서 단순한 논리를 수행하지 마십시오. 논리가 순간적이지 않은 경우 다른 스레드를 실행하여 처리해야합니다. – SJuan76
스윙은 단일 스레드이므로 장기 실행 코드 또는 차단 코드로 인해 UI를 업데이트 할 수 없게됩니다. [동시성의 동시성] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) 및 [ Worker Threads and SwingWorker] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) – MadProgrammer
왜 catch (SQLException e) {} 및 catch (Exception e) { }'? 무언가 잘못되면, 무엇이 잘못되었는지 알고 싶지 않으십니까? – immibis