gpt4 book ai didi

java - 保存到 mysql 数据库时出现 jbutton 问题

转载 作者:行者123 更新时间:2023-12-02 05:16:12 24 4
gpt4 key购买 nike

美好的一天。任何人都可以真正帮助我解决我所面临的数据库问题吗?我想使用 pepraredStatement 插入数据库。但是,每当我添加数据库部分(连接和 pepraredStatement )时,“上传”按钮就会无响应。但是当我删除与数据库相关的任何内容时,我的所有按钮都可以工作。您可以在这里找到代码 http://pastebin.com/euKdWhr2 .

我将非常感谢任何帮助或建议。可能我在数据库部分遗漏了一些东西。

请注意,这不是一个重复的问题,因为我没有找到正确的答案公共(public)无效actionPerformed(ActionEvent ev) { 字符串文件 = fileField.getText(); SetGetQuestionFileName 模式 = new SetGetQuestionFileName(文件); ConnectToDatabase 数据库 = new ConnectToDatabase(); 尝试 {

        ///////// 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)
{

}

最佳答案

Swing 是一个单线程框架。任何长时间运行或阻塞的操作,当在 Swing 的 GUI 线程(事件调度线程)的上下文中执行时,都会阻止它更新屏幕。

看看Concurrency in SwingWorker Threads and SwingWorker了解更多详情

关于java - 保存到 mysql 数据库时出现 jbutton 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26939216/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com