文本文件和数据库问题的 Jbutton 问题 [英] Jbutton issue with text file and databse issue

查看:32
本文介绍了文本文件和数据库问题的 Jbutton 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我正在尝试使其工作.我必须阅读包含问题的文本文件的内容并将其保存到数据库 testsystem 中.

I've the following code which i'm trying to make work . I have to read the content of a text file , which contains questions and save it to the database testsystem .

我面临的主要问题是它没有插入到数据库中.我不确定它是否正在读取文本文件.

The main problem that i'm facing is that it's not inserted in the database. and i'm not sure whether it is reading the textfile or not.

我得到了 GameDroids 的帮助,根据他上面的解释,我设法得到了这个,但现在它不仅插入而且我的上传按钮变得没有响应,它现在不会触发任何事件.请我真的需要帮助.
任何帮助将不胜感激.

I have received help from GameDroids , from what he explained above , i managed to get this , but now it's not only inserting and my upload button has become unresponsive , it doesn't trigger any event now. please i really need help.
any help will be glady appreciated .

例如,对于下面的问题,我将在我的 Category_questions 中加入:收藏,对于我的问题:以下哪个不是现实生活"收藏的例子?对于我的 quest_opt_a :您在纸牌游戏中持有的纸牌.等等,对于正确的选项答案,我应该将 d 插入到数据库表中.

For example for the bellow question, i shall have in my Category_questions : Collections, and for my questions : Which of these is not an example of a "real-life" collection? and for my quest_opt_a : a. The cards you hold in a card game. etc and for correct_option_answer i shall have d inserted in the database table.

这是个问题:

Collections 以下哪一项不是现实生活"集合的例子?一种.您在纸牌游戏中持有的纸牌.湾您最喜欢的歌曲存储在您的计算机中.C.足球队的球员.d.一本书的页数.

Collections Which of these is not an example of a "real-life" collection? a. The cards you hold in a card game. b. Your favorite songs stored in your computer. c. The players on a soccer team. d. The number of pages in a book.

d.

public void actionPerformed(ActionEvent ev)
{
    String file = fileField.getText();

    SetGetQuestionFileName setGet = new SetGetQuestionFileName(file);


    try
    {    
        ConnectToDatabase database = new ConnectToDatabase();

                             // prepare the query
String query = "INSERT INTO questionnaire (category_questions, questions, quest_opt_a,quest_opt_b, quest_opt_c,  quest_opt_d, correct_option_answer ) VALUES (?, ?, ?, ?, ?, ?, ?)"; 

                PreparedStatement preparedStmt = null;
                                    database.getConnection();   
                        preparedStmt = database.getConnection().prepareStatement(query);

        if(ev.getActionCommand().equals("UPLOAD"))
        {
             // JOptionPane.showMessageDialog(null,"File field can't be empty. Try again","ERROR",JOptionPane.INFORMATION_MESSAGE);  

            File filePathString = new File(file);

                // load the file
             Scanner scanner = new Scanner(file);

                 //fis = new FileInputStream(filePathString);

                if(file.length() == 0)
                {
                    JOptionPane.showMessageDialog(null,"File field can't be empty. Try again","ERROR",JOptionPane.INFORMATION_MESSAGE);
                }

                else
                {
                    if(filePathString.exists() && filePathString.isFile())
                    {


                // read the file line by line
                while (scanner.hasNextLine()) 
                    {
                String line = scanner.nextLine();

             String[] questionAnswer = line.split("?");//[ // line.split["?"] will split the line into two strings, right at the ? and put those two strings into an array. 
            String question = questionAnswer[0];  // so [0] will access the first element in that array - the question 
             String[] answers = questionAnswer[1].split(","); // now we split the string after the ? into many strings divided by comma


                        // create the mysql insert preparedstatement

         preparedStmt.setString(1, "JDBC");   
         preparedStmt.setString(2, question);  
         preparedStmt.setString(3, answers[0]);     
         preparedStmt.setString(4, answers[1]);      
         preparedStmt.setString(5, answers[2]);       
            preparedStmt.setString(6, answers[3]);      
         preparedStmt.setString(7, answers[4]);      

             preparedStmt.executeUpdate();


                        }

            //  database.disconnect();
                            JOptionPane.showMessageDialog(null,"File successfuly uploaded","INFORMATION",JOptionPane.INFORMATION_MESSAGE);
                            fileField.setText("");
                }               

        }

        if(!filePathString.exists() && !filePathString.isFile())

                                JOptionPane.showMessageDialog(null,"File coudn't be found. Do check file name.","ERROR",JOptionPane.INFORMATION_MESSAGE);

    }


}

catch(Exception ex)
        {

        }

    if(ev.getActionCommand().equals("LOGOUT"))
        {
            System.exit(0);
        }

    }   

推荐答案

您可以使用 ActionListenerJFrame 或任何其他视图类扩展您的 JPanel 或任何其他视图类code> 如果需要,但通常为 ActionListener 创建一个单独的类是个好主意.

You can extend your JPanel or JFrame or any other view class with ActionListener if you want, but it is generally a good idea to create a separate class for the ActionListener.

public class MyActionListener extends ActionListener{

   String myVariable;

   public MyActionListner(String variableINeedForTheAction){    // pass the parameters you need in the constructor or getter method
      this.myVariable = variableINeedForTheAction;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
         this.myVariable = "actionPerformed";     // then, when the action gets performed you can access that variable
   }
}


public class MyFrame extends JFrame{
   //some code
   JButton myButton = new JButton();
   String text = "";
   MyActionListner buttonListener = new MyActionListener(text);   // instantiate your own action listener class and pass the variables you need for performing the action
   myButton.addActionListener(buttonListener);       // add the listener to some button
}

所有这些都是为了将​​您的视图与程序中的控制器分开.假设您在另一个框架或面板的某处有另一个按钮,并且您希望它执行与该按钮相同的操作,那么您可以简单地注册另一个动作侦听器实例,而无需复制整个代码.

All this is done to separate your view from the controllers in your program. Imagine you have another button somewhere in another frame or panel and you want it to do the same thing as this button, then you can simply register another instance of your action listener, without having to copy the whole code.

无论如何,我的猜测是您在执行的操作中做了太多工作,并且整个 GUI 变得无响应,因为您正在从光盘读取文件并将其内容放入您的 actionPerformed 方法中的数据库中.

Anyway, my guess is that you are doing too much work in your performed action and that the complete GUI becomes unresponsive because you are reading a file from disc and putting its content into a database in your actionPerformed method.

可以尝试的是使用线程来处理您的所有上传/读取/写入内容:

What you could try is using a Thread to handle all your upload / read / write stuff:

public void actionPerformed(ActionEvent e){
    Thread myThread = new Thread(new Runnable(){
        public void run(){
            System.out.println(Thread.currentThread().getName() + " is running");

            File filePathString = new File(file);
            // load the file
            Scanner scanner = new Scanner(file);
            //... do the reading and writing here

            System.out.println(Thread.currentThread().getName() + " is finished"); 
        }
    }, "Upload Thread");
    myThread.start();
}

这篇关于文本文件和数据库问题的 Jbutton 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆