类[类java.lang.String]的对象无法转换为[class java.lang.Integer]

时间:2017-11-13 14:27:54

标签: java swing

这是代码:

public viewGUI() {
    initComponents();
    combobox();
    combobox1();
}


private void initComponents() {

    entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("atul?zeroDateTimeBehavior=convertToNullPU").createEntityManager();
    membersQuery = java.beans.Beans.isDesignTime() ? null : entityManager.createQuery("SELECT m FROM Members m");
    membersList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : membersQuery.getResultList();
    cb3 = new javax.swing.JButton();
    cb1 = new javax.swing.JComboBox<>();
    cb2 = new javax.swing.JComboBox<>();
    jButton4 = new javax.swing.JButton();
    jScrollPane2 = new javax.swing.JScrollPane();
    t1 = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("View Members");
    getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

    cb3.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
    cb3.setText("View");
    cb3.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cb3ActionPerformed(evt);
        }
    });
    getContentPane().add(cb3, new org.netbeans.lib.awtextra.AbsoluteConstraints(510, 30, -1, -1));

    cb1.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
    cb1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Select Package-name" }));
    cb1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cb1ActionPerformed(evt);
        }
    });
    getContentPane().add(cb1, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 30, -1, -1));

    cb2.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
    cb2.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Select Session-id" }));
    cb2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            cb2ActionPerformed(evt);
        }
    });
    getContentPane().add(cb2, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 30, -1, -1));

    jButton4.setFont(new java.awt.Font("Tahoma", 3, 12)); // NOI18N
    jButton4.setText("Back");
    jButton4.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton4ActionPerformed(evt);
        }
    });
    getContentPane().add(jButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(600, 30, -1, -1));

    t1.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "member-id", "email", "fname", "lname", "phone", "session-id", "package name"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }
    });
    jScrollPane2.setViewportView(t1);

    getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(60, 80, 660, 340));

    setSize(new java.awt.Dimension(803, 530));
    setLocationRelativeTo(null);
}// </editor-fold>                        

private void cb3ActionPerformed(java.awt.event.ActionEvent evt) {                                    
    DefaultTableModel t=(DefaultTableModel) t1.getModel();
    t.setRowCount(0);
    try{
        Class.forName("java.sql.DriverManager");
        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/atul","root","root");
        Statement st=(Statement) con.createStatement();
        String query="select * from members;";
        ResultSet rs=st.executeQuery(query);
        while(rs.next()){
            String id=rs.getString("member_id");
            String em=rs.getString("email");
            String fn=rs.getString("fname");
            String ln=rs.getString("lname");
            String ph=rs.getString("phone");
            String ses=rs.getString("session_id");
            String pac=rs.getString("pack_name");
            Object row[]={id,em,fn,ln,ph,ses,pac};
            t.addRow(row);
        }
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}                                   

private void cb1ActionPerformed(java.awt.event.ActionEvent evt) {                                    
    DefaultTableModel t=(DefaultTableModel) t1.getModel();
    t.setRowCount(0);
    Object s=(String) cb1.getSelectedItem();
    try{
        Class.forName("java.sql.DriverManager");
        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/atul","root","root");
        Statement st=(Statement) con.createStatement();
        String query="select * from members where pack_name='"+s+"';";
        ResultSet rs=st.executeQuery(query);
        while(rs.next()){
            String id=rs.getString("member_id");
            String em=rs.getString("email");
            String fn=rs.getString("fname");
            String ln=rs.getString("lname");
            String ph=rs.getString("phone");
            String ses=rs.getString("session_id");
            String pac=rs.getString("pack_name");
            Object row[]={id,em,fn,ln,ph,ses,pac};
            t.addRow(row);
        }
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(this, e.getMessage());
    }

}                                   

private void cb2ActionPerformed(java.awt.event.ActionEvent evt) {                                    
    DefaultTableModel t=(DefaultTableModel) t1.getModel();
    t.setRowCount(0);
    Object s=cb2.getSelectedItem();
    try{
        Class.forName("java.sql.DriverManager");
        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/atul","root","root");
        Statement st=(Statement) con.createStatement();
        String query="select * from members where session_id='"+s+"';";
        ResultSet rs=st.executeQuery(query);
        while(rs.next()){
            String id=rs.getString("member_id");
            String em=rs.getString("email");
            String fn=rs.getString("fname");
            String ln=rs.getString("lname");
            String ph=rs.getString("phone");
            String ses=rs.getString("session_id");
            String pac=rs.getString("pack_name");
            Object row[]={id,em,fn,ln,ph,ses,pac};
            t.addRow(row);
        }
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}                                   

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    FirstGUI F= new FirstGUI();
    this.dispose();
    F.setVisible(true);
}                                        
private void combobox() {
    try {
        Class.forName("java.sql.DriverManager");
        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/atul","root","root");
        Statement st=(Statement) con.createStatement();
        String query="select * from packages;";
         ResultSet rs=st.executeQuery(query);
        while(rs.next()){
            String name=rs.getString("pname");
            cb1.addItem(name);
        }
    }
    catch(Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}
private void combobox1() {
    try {
        Class.forName("java.sql.DriverManager");
        Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/atul","root","root");
        Statement st=(Statement) con.createStatement();
        String query="select * from batch;";
         ResultSet rs=st.executeQuery(query);
        while(rs.next()){
            String name=rs.getString("session_id");
            cb2.addItem(name);
        }
    }
    catch(Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}
/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Windows".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(viewGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(viewGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(viewGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(viewGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new viewGUI().setVisible(true);
        }
    });
}                

我正在创建这个jframe来查看我的成员表的所有值。最初成员表的列成员id是数据类型integer并且它工作正常但是现在当我将它更改为数据类型varchar并插入一个字符串'm11'时它给了我java.objeect error。如果我删除该字符串值,它完全正常。谁能告诉我错误是什么? 这是错误:

Exception Description: The object [m11], of class [class java.lang.String], 
could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "m11"
Exception in thread "AWT-EventQueue-0" 
javax.persistence.PersistenceException: Exception [EclipseLink-3001] 
(Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): 
org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [m11], of class [class java.lang.String], 
could not be converted to [class java.lang.Integer].
Internal Exception: java.lang.NumberFormatException: For input string: "m11"
at 


 org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:480)
at apppackage.viewGUI.initComponents(viewGUI.java:41)
at apppackage.viewGUI.<init>(viewGUI.java:25)
at apppackage.viewGUI$6.run(viewGUI.java:261)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311).......

1 个答案:

答案 0 :(得分:1)

  

org.eclipse.persistence.internal.jpa.QueryImpl.getResultList(QueryImpl.java:480)    在apppackage.viewGUI.initComponents(viewGUI.java:41)

错误发生在getResultList(...)方法中,我猜这个方法是从数据库中读取数据并创建TableModel

Class[] types = new Class [] 
{
    java.lang.String.class, java.lang.String.class, java.lang.String.class,
    java.lang.String.class, java.lang.Integer.class, java.lang.String.class,
    java.lang.String.class
}    

请注意您的自定义TableModel代码,您说第5列包含整数数据!

但是您的错误消息显示:

  

类[类java.lang.String]的对象[m11]无法转换为[类java.lang.Integer]。

因此,这告诉我数据库中的数据是String,因此您需要更改getColumnClass(..)方法以返回该列的String,而不是Ingeter。