简体   繁体   中英

Using the result of a query as a condition to set background of jtextField in java swing with Mysql

Can Someone Help me to set the background color of a jtextfield using the result of the query statement My code compiled but I didn't have my result for now

public void mouseClicked( MouseEvent arg0 )
    {
        DefaultTableModel model = (DefaultTableModel) table_Vols_Disponibles.getModel();
        // get the selected row index
        int selectedRowIndex = table_Vols_Disponibles.getSelectedRow();

        // set the selected row data into jtextfields
        txt_ID_Vol.setText( model.getValueAt( selectedRowIndex, 0 ).toString() );
        txt_ID_Avion.setText( model.getValueAt( selectedRowIndex, 1 ).toString() );
        txt_Heure_Depart.setText( model.getValueAt( selectedRowIndex, 2 ).toString() );
        txt_Heure_Arrivee.setText( model.getValueAt( selectedRowIndex, 3 ).toString() );
        txt_Duree.setText( model.getValueAt( selectedRowIndex, 4 ).toString() );
        txt_Ville_Depart.setText( model.getValueAt( selectedRowIndex, 5 ).toString() );
        txt_Ville_Arrivee.setText( model.getValueAt( selectedRowIndex, 6 ).toString() );

        // int row =table_Vols_Disponibles.getSelectedRow();
        //  String cell=table_Vols_Disponibles.getModel().getValueAt(row, 0).toString();
        try
        {
            Class.forName( "com.mysql.jdbc.Driver" );
            Connection connection = DriverManager.getConnection( "jdbc:mysql://localhost:3306/billet_avion", "root", "" );
            String sql = "Select siege from sieges where id_vo='" + txt_ID_Vol + "' ";
            PreparedStatement preparedStatement = connection.prepareStatement( sql );
            ResultSet resultSet = preparedStatement.executeQuery( sql );

            String siege1 = txtTest.getText();
            String NBsiege = resultSet.getString( "siege" );
            if( NBsiege == siege1 )
            {
                txtTest.setBackground( Color.green );
            }
        }
        catch( ClassNotFoundException e )
        {
            e.printStackTrace();
        }
        catch( SQLException throwables )
        {
            throwables.printStackTrace();
        }
    }

When you compare Strings you need to compare the content but not their memory addresses. Since == is an operator, it compares memory addresses rather than the content

Change

           if( NBsiege == siege1 )
            {
                txtTest.setBackground( Color.green );
            }

to

        if( siege1.equals(NBsiege) )
        {
            txtTest.setBackground( Color.green );
        }

A suggestion.

  1. Creating the database connection like this in methods is not a good practice. You can use a separate class to do it for you. Please note that creating a database connection is an expensive task. Therefore you should reuse created connection using a Connection pool or similar mechanisam

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