当前位置: 首页 > 知识库问答 >
问题:

如何显示从mysql到jtext field的空白数据

许茂才
2023-03-14

我制作了一个java程序,其中sql表将绑定到jtable。问题是,当我在具有空/空数据的jtable上单击行时,该行的下一列将不会显示在textfield上。如果空白数据位于第4列,则文本字段将仅显示第1到第3列的数据,而将下一个文本字段留空。显示的错误也是线程“AWT-EventQueue-0”java中的异常。lang.NullPointerException。我正在为这个程序使用这个示例数据库https://www.mysqltutorial.org/wp-content/uploads/2018/03/mysqlsampledatabase.zip

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author User1
 */
public class frmCustomers extends javax.swing.JInternalFrame {

/**
 * Creates new form frmCustomers
 */
private int selectedRow;
private int totalrow;
private int row;


public frmCustomers() {
    initComponents();
    BINDDATA();
    row = 0;
    
    
    btnPrevious.setEnabled(false);
    btnFirst.setEnabled(false);
    btnNext.setEnabled(true);
    btnLast.setEnabled(true);

}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {
    pack();
}// </editor-fold>                        

private void btnDisplayActionPerformed(java.awt.event.ActionEvent evt) {                                           
    BINDDATA();        
}                                          

private void btnFirstActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
}                                        

private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {                                        
    
}                                       

private void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) {                                            
    
}                                           

private void btnLastActionPerformed(java.awt.event.ActionEvent evt) {                                        
    
}                                       

private void btnInsertActionPerformed(java.awt.event.ActionEvent evt) {                                          
    INSERT();
    BINDDATA();
}                                         

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {                                          
    UPDATE();  
    BINDDATA();
}                                         

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                          
    DELETE();
    BINDDATA();
}                                         

private void tblCustomersMouseClicked(java.awt.event.MouseEvent evt) {                                          
    DefaultTableModel table = (DefaultTableModel) tblCustomers.getModel();
    selectedRow = tblCustomers.getSelectedRow();
    
    txtCustomerNumber.setText(table.getValueAt(selectedRow, 0).toString());
    txtCustomerName.setText(table.getValueAt(selectedRow, 1).toString());
    txtContactLastName.setText(table.getValueAt(selectedRow, 2).toString());
    txtContactFirstName.setText(table.getValueAt(selectedRow, 3).toString());
    txtPhone.setText(table.getValueAt(selectedRow, 4).toString());
    txtAddressLine1.setText(table.getValueAt(selectedRow, 5).toString());
    txtAddressLine2.setText(table.getValueAt(selectedRow, 6).toString());
    txtCity.setText(table.getValueAt(selectedRow, 7).toString());
    txtState.setText(table.getValueAt(selectedRow, 8).toString());
    txtPostalCode.setText(table.getValueAt(selectedRow, 9).toString());
    jComboBox1.setSelectedItem(table.getValueAt(selectedRow, 10).toString());
    txtSalesRep.setText(table.getValueAt(selectedRow, 11).toString());
    txtCreditLimit.setText(table.getValueAt(selectedRow, 12).toString());
}                                         

public void INSERT() {
    try  {
        PreparedStatement pst;
        
        String number = txtCustomerNumber.getText().trim();
        String name = txtCustomerName.getText().trim();
        String last = txtContactLastName.getText().trim();
        String first = txtContactFirstName.getText().trim();
        String phone = txtPhone.getText().trim();
        String address1 = txtAddressLine1.getText().trim();
        String address2 = txtAddressLine2.getText().trim();
        String city = txtCity.getText().trim();
        String state = txtState.getText().trim();
        String postal = txtPostalCode.getText().trim();
        String country = (String) jComboBox1.getSelectedItem();
        String salesrep = txtSalesRep.getText().trim();
        String creditlim = txtCreditLimit.getText().trim();
        
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels",
                "root","passweird");
        
        String sql = "INSERT INTO customers values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
        pst = con.prepareStatement(sql);
        pst.setString(1, number);
        pst.setString(2, name);
        pst.setString(3, last);
        pst.setString(4, first);
        pst.setString(5, phone);
        pst.setString(6, address1);
        pst.setString(7, address2);
        pst.setString(8, city);
        pst.setString(9, state);
        pst.setString(10, postal);
        pst.setString(11, country);
        pst.setString(12, salesrep);
        pst.setString(13, creditlim);
        
        pst.execute();
        
     con.close();
    }
    catch (Exception e)
    {
        JOptionPane.showMessageDialog(null, "Error establishing connection!!!" + e.toString());
    }    
}
private void UPDATE(){
    try {
        String number = txtCustomerNumber.getText().trim();
        String name = txtCustomerName.getText().trim();
        String last = txtContactLastName.getText().trim();
        String first = txtContactFirstName.getText().trim();
        String phone = txtPhone.getText().trim();
        String address1 = txtAddressLine1.getText().trim();
        String address2 = txtAddressLine2.getText().trim();
        String city = txtCity.getText().trim();
        String state = txtState.getText().trim();
        String postal = txtPostalCode.getText().trim();
        String country = (String) jComboBox1.getSelectedItem();
        String salesrep = txtSalesRep.getText().trim();
        String creditlim = txtCreditLimit.getText().trim();
         
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels",
                "root","passweird");
        
        Statement st = con.createStatement();
        
        String sql = "UPDATE customers set customerNumber='" + number + "', customerName='" + name
                + "', contactLastName='" + last + "', contactFirstName='" + first + "', phone='" + phone
                + "', addressLine1='" + address1 + "', adressLine2='" + address2 + "', city='" + city
                + "', state='" + state + "', postalCode='" + postal + "', country='" + country 
                + "', salesRepEmployeeNumber='" + salesrep + "', creditLimit='" + creditlim;
        
        int result = st.executeUpdate(sql);
        
        if(result > 0) {
            JOptionPane.showMessageDialog(null, "You have successfully updated the record!!!");
        }
    }
    catch (Exception e) {
    }
}
private void DELETE(){
    try{
        String number = txtCustomerNumber.getText().trim();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels",
                "root","passweird");
        
        Statement st = con.createStatement();
        String sql = " DELETE FROM customers WHERE customerNumber = " + number;
        
        int result = st.executeUpdate(sql);
        
        if (result > 0){
            JOptionPane.showMessageDialog(null, "You have successfully deleted the record!!!");
        }
    }
        catch (Exception e){
    }
}
private void BINDDATA(){
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/classicmodels",
                "root","passweird");
        
        Statement st = con.createStatement();
        String sql = "SELECT * FROM customers";
        ResultSet rs = st.executeQuery(sql);
        
        DefaultTableModel table = (DefaultTableModel) tblCustomers.getModel();
        table.setRowCount(0);
        
        while(rs.next()) {
            Object obj[] = {rs.getInt("customerNumber"), rs.getString("customerName"), rs.getString("contactLastName"), rs.getString("contactFirstName"),
                            rs.getString("phone"), rs.getString("addressLine1"), rs.getString("addressLine2"),
                            rs.getString("city"), rs.getString("state"), rs.getString("postalCode"), rs.getString("country"),
                            rs.getInt("salesRepEmployeeNumber"), rs.getObject("creditLimit"),};
                table.addRow(obj); 
                
            }
            con.close();
            
            totalrow = table.getRowCount();
        }
    catch (Exception e) {
    }
}

// Variables declaration - do not modify                     
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnDisplay;
private javax.swing.JButton btnFirst;
private javax.swing.JButton btnInsert;
private javax.swing.JButton btnLast;
private javax.swing.JButton btnNext;
private javax.swing.JButton btnPrevious;
private javax.swing.JButton btnUpdate;
private javax.swing.JComboBox<String> jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblCustomers;
private javax.swing.JTextField txtAddressLine1;
private javax.swing.JTextField txtAddressLine2;
private javax.swing.JTextField txtCity;
private javax.swing.JTextField txtContactFirstName;
private javax.swing.JTextField txtContactLastName;
private javax.swing.JTextField txtCreditLimit;
private javax.swing.JTextField txtCustomerName;
private javax.swing.JTextField txtCustomerNumber;
private javax.swing.JTextField txtPhone;
private javax.swing.JTextField txtPostalCode;
private javax.swing.JTextField txtSalesRep;
private javax.swing.JTextField txtState;
// End of variables declaration                   
}

共有1个答案

郤飞英
2023-03-14

检查您是否从数据库中获得了null,如果是,请在表的相关字段中输入一个空字符串。

 类似资料:
  • 试图制作一个简单的应用程序,从服务器获取JSON数据,并在自定义列表中显示它们,非常简单的事情。 但当我运行应用程序时,它显示的是白色空白屏幕,但没有数据。它也没有显示任何错误,我假设如果有任何错误,它不会在我的手机中运行。但不显示获取的数据。 下面是类 我发现的其他问题与我的问题不匹配,否则不会添加这个问题。

  • 我已经在本地机器上成功安装了 CakePHP 3。我使用WAMP服务器(http: // localhost /)。一切正常。当我通过FTP将相同的文件传输到Web服务器时 http://example.com/ cakephp 3不起作用,它会显示白色空白页。Web服务器(example.com)使用php 5.6版本。问题出在哪里?怎么了?

  • 本文向大家介绍tensorboard显示空白的解决,包括了tensorboard显示空白的解决的使用技巧和注意事项,需要的朋友参考一下 ubuntu 14.04 + python3.4 + chrome, 在浏览器中查看tensorboard, 发现出了graph,其他的数据都是空白。 通过分析,发现js中如下一些错误 Uncaught SyntaxError: Block-scoped decl

  • 我已经用php5 fpm设置了一个nginx服务器。当我尝试加载网站时,我得到一个没有错误的空白页面。Html页面可以很好地使用,但php却不行。我尝试在php中打开显示错误。但是没有运气。php5-fpm。日志没有产生任何错误,nginx也没有。 nginx。形态 编辑 这是我的nginx错误日志:

  • 我在这里遵循GLUT教程。我将第一个示例“YAT”复制并粘贴到xcode中,稍微修改了包含,发现出现了一个空白窗口。未修改的代码设置为显示线框茶壶。对我来说,这不会发生。 但是,如果我取消对创建金牛座的注释,则金牛座和茶壶的一部分会出现在窗口中。在//draw下,似乎还有一些随机的非注释行和注释行的组合,它们实际上会导致对象出现……或者只是一个空白屏幕。 P、 我知道每个人都说不要使用GLUT,但

  • 我正在尝试使用 iReport 设计器(两者都具有空数据源)从主报告(report1.jrxml)创建一个子报告(report1_subreport3.jrxml)。主报表详细信息带包含静态文本(“主报表”),子报表元素和子报表在其相应的详细信息带中包含静态文本(“子报表”) 但单击主报表的预览选项卡仅显示静态文本“主报表”,而不显示子报表(“子报表”)的静态文本。 我还浏览了http://com