why this code is not working?,

error: non-static variable jTextField1 cannot be referenced from a static context

try{
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/sample", "", "");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT name FROM testtable");
            
            while (rs.next()){
                String n = rs.getString("name");               
                jTextField1.setText(n);
                 }            
            stmt.close();
            con.close();                  
            
        }catch(Exception e){

Dani AI

Generated

You are getting that error because jTextField1 is an instance field but the code that touches it runs in a static context. As noted, move the DB work into an instance method of your frame (or pass the text field into a helper) and update the UI from there. Also note: a while (rs.next()) loop keeps overwriting the field, so you will only ever see the last row. If you want just one row, use LIMIT 1 or a single if (rs.next()).

class MyForm extends javax.swing.JFrame {
  private javax.swing.JTextField jTextField1 = new javax.swing.JTextField(20);

  void loadName() {
    new Thread(() -> {                        // run DB work off the EDT
      String value = "";
      try (java.sql.Connection c = java.sql.DriverManager.getConnection(
               "jdbc:mysql://localhost:3306/sample", "user", "pass");
           java.sql.PreparedStatement ps = c.prepareStatement(
               "select name from testtable limit 1");
           java.sql.ResultSet rs = ps.executeQuery()) {
        if (rs.next()) value = rs.getString(1);
      } catch (java.sql.SQLException ex) {
        ex.printStackTrace();                 // or show a dialog/log
      }
      final String text = value;
      javax.swing.SwingUtilities.invokeLater(() -> jTextField1.setText(text));
    }).start();
  }
}

A few quick sanity checks:

  • Keep UI fields non-static; call loadName() from your form’s constructor or a button handler.
  • Always call rs.next() before reading columns; ’s sketch misses that, which will throw an exception.
  • For multiple rows, populate a JTable via a TableModel instead of a single JTextField.
  • NetBeans GUI Builder will not fix static/instance issues; it just generates UI code. You still wire up the query logic yourself. If you are still stuck, post the surrounding class as requested so folks can point to the exact static context.

Recommended Answers

All 7 Replies

Probably because the variable jTextField1 is not static and the method where you put that code is static: This is wrong:

class A {
  private int a = 5; // non static variable

  private static void method() {
     System.out.println(a);
  }
}

The method is static, so you cannot use the non-static variable.
There are 2 good solutions:
Make the method non-static:

class A {
  private int a = 5; // non static variable

  private void method() {
     System.out.println(a);
  }
}

Leave it static and add an argument to the method and then call it with that argument

class A {
  private int a = 5; // non static variable

  private static void print(int i) {
     System.out.println(i);
  }


  // calling the method from somewhere else:
  {
    print(a);

  }
}

ahh.. i see.. still can't do it.

is there anyway that I can use netbeans act like visual studio?,
i mean as easy as visual studio, because I don't think I can display data on textfields directly, unlikely in visual studio

ahh.. i see.. still can't do it.

is there anyway that I can use netbeans act like visual studio?,
i mean as easy as visual studio, because I don't think I can display data on textfields directly, unlikely in visual studio

What have you tried? Is jTextField1 static or not. It shouldn't be static. The gui builder has nothing to do with the error. You write the code for executing the query.

I am not sure. But you can try it....

String SQL = "SELECT * FROM sample "; 
Statement stmt =  con.createStatement();
 stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);
 jTextField1.setText(rs.getString(String.valueOf("column name from SQL table")));
commented: Useless advice. Has nothing to do with the real problem -3

I think there is something wrong in your code(line-7).
I will try to solve.
Thank you.

jTextField1.setText(rs.getString(String.valueOf("column name from SQL table")));

That is not going to solve the problem. It is exactly the same with the code aldeene posted.

aldeene - If you haven't solved your problem you can post some more of your code

Can you post your full code aldeene?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.