Search code examples
javamysqldatabasejdbcjtextfield

Display database records in jTextField


I want to display some questions and their answers. So to test that I just tried to display the question number and the question but there's

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

error and I don't know what to do next. 1. I want to display next record with the next button. 2. seteditable isn't working (giving error)

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
String qid=jTextField1.setEditable(false);
String qid=jTextField1.getText();
String ques=jTextField2.getText();

 try{       
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb1","root","");
            PreparedStatement ps = con.prepareStatement("select * from qc where qid=1");                          
            ResultSet rs=ps.executeQuery();
            if(rs.next()){
                jTextField1.setText(rs.getString("qid"));
                jTextField2.setText(rs.getString("ques"));
            }
            }           
            catch(ClassNotFoundException | SQLException e)
            {
            System.out.println(e);
            }
}                                        

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
    // TODO add your handling code here:
}  

Solution

  • Make sure, that you have a MySql-Driver in your classpath.

    You can download it here: https://dev.mysql.com/downloads/connector/j/5.0.html

    Configuring your classpath is dependent on your environment. If you are running your code from within eclipse you can follow these steps:

    1. Download jdbc driver
    2. Put jdbc driver (for example mysql-connector-java-5.0.8-bin.jar) into your eclipse project
    3. Rightclick on it, Build Path, Add to Build Path

    Using netbeans:

    1. Download jdbc driver
    2. Put jdbc driver (for example mysql-connector-java-5.0.8-bin.jar) into your netbeans project
    3. Right click on project, Properties, go to Libraries, choose Run-time libraries
    4. Add the jar to the list

    For jumping to the next item, there are several options.

    One solution could look like this:

    Add this code at the top of your class:

    // private JTextField jTextField1;
    // private JTextField jTextField2;
    // ...
    private int currentQid; // add this line to declare a new field of type int
    

    Then inside your actionlistener method:

    PreparedStatement ps = con.prepareStatement("SELECT * FROM qc WHERE qid > ? ORDER BY qid LIMIT 1");
    ps.setInt(1, currentQid);
    ResultSet rs=ps.executeQuery();
    if(rs.next()){
        currentQid = rs.getInt("qid");
        jTextField1.setText(Integer.toString(currentQid));
        jTextField2.setText(rs.getString("ques"));
    }
    

    In detail:

    • SELECT * FROM qc WHERE qid > ? ORDER BY qid LIMIT 1 will select the record, with the qid next to ?
    • ps.setInt(1, currentQid); will set the first parameter of the query (the ?) to a provided value
    • currentQid = rs.getInt("qid"); will save the next qid in the instance field currentQid
    • Integer.toString(currentQid) will convert currentQid to a text
    • jTextField1.setText(...); will display the text inside of jTextField1