发现原始类型:JComboBox并且找不到符号Class.forName

时间:2016-03-26 17:46:11

标签: java mysql swing combobox jtable

我将我的dosen.java与我的Test5.java连接,我试图让教师的面板插入我们的分数。但是我觉得很简单,就像老师想要插入得分,他只需要找到课程,主题从mapel组合框中出来,并在底部的文本字段中插入成绩。当我把combobox放在那里时,我得到了像raw type combobox这样的错误,然后我的jtable也遇到了问题。

更新

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.table.*;
import java.util.*;
import javax.swing.DefaultComboBoxModel;

public class dosen{
    JPanel panel_dosen, panel_combo, panel_input;
    JTable table;
    JLabel label_input,label_input2;
    JTextField tf_input,tf_input2;
    JButton Insert;
    JComboBox<String> classs, mapel; //first Problem
    ResultSet rs;
    JScrollPane scroll;
    Connection conn = null;
    Statement stmt = null;
    String sql;
    public dosen(){
        table();
        dataInsert();
        panel_dosen = new JPanel();
        panel_dosen.setLayout(new BorderLayout());
        panel_dosen.add(panel_combo, BorderLayout.NORTH);
        panel_dosen.add(scroll, BorderLayout.CENTER);
        panel_dosen.add(panel_input, BorderLayout.SOUTH);
    }
    public void panelCombo(){ //combobox panel
        panel_combo = new JPanel();
        DefaultComboBoxModel classes = new DefaultComboBoxModel(new String[]{"Information System", "Hospitality", "Law", "Management", "Accounting"});
        classs = new JComboBox(classes);
        mapel = new JComboBox();
        classs.addActionListener (new ActionListener () {
            public void actionPerformed(ActionEvent e) {
                if ("Information System".equals(classs.getSelectedItem())){
                    DefaultComboBoxModel ISsub = new DefaultComboBoxModel(new String[]{"Statistic and Probability", "Operating System", "Management Information System", "Inggris-2", "Object Oriented Programming"});
                    mapel.setModel(ISsub);    
                } else {
                    DefaultComboBoxModel Hossub = new DefaultComboBoxModel(new String[]{"Statistic and Probability", "Operating System", "Management Information System", "Inggris-2", "Object Oriented Programming1"});
                    mapel.setModel(Hossub);    
                }
            }
        });
        classs.setSelectedIndex(0);
        mapel.setSelectedIndex(0);
        panel_combo.add(classs);
        panel_combo.add(mapel);
    }
    public void table(){//i dont put panel on table because i dont think it needed
        table = new JTable();
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setFillsViewportHeight(true);
        scroll = new JScrollPane(table);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        DefaultTableModel model = new DefaultTableModel();
        String[] columnNames= {"NIM","Grade","Class"};
        model.setColumnIdentifiers(columnNames);
        table.setModel(model);
        String Classes = "";
        String Grade = "";
        String NIM = "";
        String url = "jdbc:mysql://localhost/kampus";
        String username = "root";
        String password = "abc";

        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url, username, password);
            sql = "select NIM, Grade, Class from nilai where Mapel = '"+ mapel.getSelectedItem() +"' AND Major = '" + classs.getSelectedItem() +"'";
            PreparedStatement ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                NIM = rs.getString("NIM");
                Classes = rs.getString("Kelas");
                Grade = rs.getString("Nilai");
                model.addRow(new Object[]{NIM, Grade, Classes});
            }
        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        } 

    }
    public void dataInsert(){
        panel_input = new JPanel();
        panel_input.setLayout(new GridBagLayout());
        label_input = new JLabel("NIM : ");
        label_input2 = new JLabel("Nilai : ");
        tf_input = new JTextField(15);
        tf_input2 = new JTextField(15);
        Insert = new JButton("Insert");
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.LINE_START;
        panel_input.add(label_input, gbc);
        gbc.gridy++;
        panel_input.add(label_input2, gbc);
        gbc.gridy = 0;
        gbc.gridx++;
        gbc.anchor = GridBagConstraints.LINE_END;
        panel_input.add(tf_input, gbc);
        gbc.gridy++;
        panel_input.add(tf_input2, gbc);
        gbc.gridy++;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        panel_input.add(Insert, gbc);
    }

}

问题

dosen.java:31: warning: [rawtypes] found raw type: DefaultComboBoxModel
DefaultComboBoxModel classes = new DefaultComboBoxModel(new String[]{"Information System", "Hospitality", "Law", "Management", "Accounting"});
            ^
missing type arguments for generic class DefaultComboBoxModel<E>
where E is a type-variable:
 E extends Object declared in class DefaultComboBoxModel

1 个答案:

答案 0 :(得分:2)

String Class = "";更改此变量的名称。因为这行

Class.forName("com.mysql.jdbc.Driver");
forName实例调用的

String方法。在forName中没有名为String的方法。