简体   繁体   中英

Java Inheritance/Reuse-ability in swing

package fisheriesdatabase;

import java.sql.*;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FisheriesDatabase {

public static void main(String[] args) {
    JFrame frame=new JFrame("Fish Data Entry");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel=new JPanel();
    frame.add(panel);
    placeComponents(panel);

    frame.setVisible(true);
}

public static void placeComponents(JPanel panel){
    panel.setLayout(null);

    JLabel idlabel=new JLabel("id");
    idlabel.setBounds(10, 10, 80, 25);
    panel.add(idlabel);
    JTextField idtextfield=new JTextField(20);
    idtextfield.setBounds(100, 10, 160, 25);
    panel.add(idtextfield);

    JLabel namelabel=new JLabel("Name");
    namelabel.setBounds(10, 40, 80, 25);
    panel.add(namelabel);        
    JTextField nametextfield=new JTextField(20);
    nametextfield.setBounds(100, 40, 160, 25);
    panel.add(nametextfield);

    JButton button=new JButton("Enter Data");
    button.setBounds(10, 80, 80, 25);
    panel.add(button);

}

public static void connectDB() throws SQLException{
    final String url="jdbc:mysql://localhost:3306";
    final String driver="com.mysql.jdbc.Driver";
    final String dbName="netbeans_test";
    final String uname="root";
    final String pass="";

    Connection conn=null;
    try{
        //Registering the Driver
        Class.forName(driver).newInstance();

        //Open a connection
        conn=DriverManager.getConnection(url+dbName,uname,pass);
        Statement st=conn.createStatement();
        st.executeUpdate("insert into test values('"+idtextfield.getText()+"','"+nametextfield.getText()+"')");            

    }
    catch(ClassNotFoundException | InstantiationException | IllegalAccessException se){
        if(conn==null)
            System.err.println("DATABASE NOT CONNECTED");
        se.printStackTrace();
    }
}
}

The above mentioned code is my beginning to Swing, and as a novice I am just trying to create 2 methods

  • One for the database connectivity
  • Other for the gui
    It shows an error when I try to access the placeComponent()'s properties inside the connectDB(). Can anybody help me out here?

The error is in executeUpdate statement where it is unable to recognize the 'idtextfield' and 'nametextfield'

Thanks!!!

I suggest you start with examples from the Swing tutorial to learn how to better structure your program.

Maybe the section on How to Use Labels would be a good simple example to start with. In this example a panel is used to contain all the components. This will allow you to create instance variables that you can access from any method you implement in the panel class.

Other benefits of starting with a working example:

  1. You get rid of your static methods.

  2. It doesn't use setBounds(). Swing was designed to be used with Layout Managers. See the tutorial section on Layout Managers.

  3. The code will be created on the EDT. See the tutorial section on Concurrency .

Declare / Define both of them at class level:

public class FisheriesDatabase {

JTextField idtextfield=new JTextField(20);
JTextField nametextfield=new JTextField(20);

public static void main(String[] args) {
    ....
    ....
}

public static void placeComponents(JPanel panel){
    panel.setLayout(null);

    .....
}   

There is basically a single problem in your program. You are using 2 methods placeComponents and connectDB() . You have declared and initialised the variables idtextfield and nametextfield in placeComponents() method and tried to access it from the connectDB() method. The scope of the variables is only in the method placeComponents() . So, it gives the error of "unable to recognize the 'idtextfield' and 'nametextfield'".

You could solve this problem in 2 ways.

  1. You could declare them in the class.

  2. You could get the instances from the JPanel instance.

Also in this regard I would like to draw you attention to another thing. You have used Statement to execute query using text that is entered by users. There is a potential of 'SQL Injection'. So it would be better if you used PreparedStatement instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM