【问题标题】:Saving to mysql database with jbutton issue使用 jbutton 问题保存到 mysql 数据库
【发布时间】:2015-01-12 09:20:09
【问题描述】:

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

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

这不是一个重复的问题,因为我没有找到正确的答案 公共无效actionPerformed(ActionEvent ev) { 字符串文件 = fileField.getText(); SetGetQuestionFileName 模式 = new SetGetQuestionFileName(file); ConnectToDatabase 数据库 = 新的 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)
                {

                }

【问题讨论】:

    标签: java swing jdbc swingworker


    【解决方案1】:

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

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

    【讨论】:

    • 我真的在努力学习 java,其中一个我没有真正得到的部分是线程,所以如果你不介意你可以从我的代码部分指导我
    • 我现在不在我的电脑前,你可以考虑快速破解一下,看看你能得到什么,我稍后会尝试发布一个小例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 2022-11-30
    • 2021-04-28
    • 2017-05-12
    • 2016-08-01
    • 2010-12-04
    • 1970-01-01
    相关资源
    最近更新 更多