응답이없는 jbutton에 대한 스레드 구현을 도와 줄 사람이 있습니까? pepraredStatement를 사용하여 데이터베이스에 삽입하려고합니다. 그러나 내가 데이터베이스 부분 (connection과 pepraredStatement)을 추가 할 때마다 'UPLOAD'버튼이 반응하지 않게됩니다. 하지만 데이터베이스와 관련된 모든 것을 제거하면 모든 버튼이 작동합니다. 여기에서 코드 http://pastebin.com/euKdWhr2을 찾을 수 있습니다.응답이없는 jbutton이 동시성을 요청합니다.
정말 도움이나 제안을 부탁드립니다. 스레드에 대한 설명이 있지만 여전히 내 코드에 포함 할 수 없습니다.
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)
{
}
넣어 :-)하는 데 도움이됩니다. –
문제점 : GUI 스레드 (EDT - 이벤트 디스패처 스레드 호출)에서 시간이 많이 소요되는 작업을 제공합니다. 예를 들어 [SwingWorker] (https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingWorker.html)를 사용하여 데이터베이스 코드를 다른 스레드로 이동하기 만하면됩니다. [여기] (https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html)를 읽으면 문제를 더 잘 이해할 수 있습니다. –
그게 내가 지금 오류있어 java.lang.NullPointerException 강사에서 $ MyAction.actionPerformed (Lecturer.java:183) 및 줄 183이 하나의 st = dbconnection.prepareStatement (sql); – mkwilfreid