使用JavaSwing表更新MySQL数据库值

使用JavaSwing表更新MySQL数据库值,java,mysql,swing,jtable,tablemodel,Java,Mysql,Swing,Jtable,Tablemodel,该程序用于从数据库中读取数据。数据库中有三个表pki17、pki18、pkn18。为了查看不同的表,使用JComboBox,它通过更改表的TableModel来工作。同样通过使用tableChanged方法,我使表格可编辑。当表中的单元格值更改时,数据库中的相应值必须更改为 当我同时使用tableChanged和actionPerformed方法时,当我在swing表中编辑单元格时,数据库中的值不会改变。当我删除actionPerformed方法时,我可以通过编辑表单元格来更新数据库 我需要具备

该程序用于从数据库中读取数据。数据库中有三个表pki17、pki18、pkn18。为了查看不同的表,使用JComboBox,它通过更改表的TableModel来工作。同样通过使用tableChanged方法,我使表格可编辑。当表中的单元格值更改时,数据库中的相应值必须更改为

当我同时使用tableChanged和actionPerformed方法时,当我在swing表中编辑单元格时,数据库中的值不会改变。当我删除actionPerformed方法时,我可以通过编辑表单元格来更新数据库

我需要具备这两种能力,使用JComboBox从数据库中选择一个表,并通过编辑swing表中的值来更新数据库值

我认为问题的存在是因为这两种方法都改变了表的TableModel。但我不知道怎么解决它

public class TableCombobox extends JPanel implements ActionListener, TableModelListener  {
static JTable table;
static JComboBox box;
static MyTableModel model;
static Connection con = null;
static Statement stmt = null;

public TableCombobox() throws SQLException {
    super(new BorderLayout());
    table = new JTable(new MyTableModel("pki18"));
    table.setPreferredScrollableViewportSize(new Dimension(500, 400));
    table.setFillsViewportHeight(true);
    table.getModel().addTableModelListener(this);

    JScrollPane scrollPane = new JScrollPane(table);

    JPanel menuPanel = new JPanel();
    menuPanel.setLayout(new BoxLayout(menuPanel, BoxLayout.Y_AXIS));
    menuPanel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1,
            Color.black));

    String[] dalykas = { "Chose groop", "pki17", "pki18", "pkn18" };
    box = new JComboBox(dalykas);
    box.setMaximumSize(new Dimension(150, 25));
    box.setAlignmentX(Component.LEFT_ALIGNMENT);
    box.addActionListener(this);
    box.setSelectedIndex(2);

    menuPanel.add(box);

    JPanel cards = new JPanel(new CardLayout());
    cards.add(scrollPane, "view");
    add(menuPanel, BorderLayout.LINE_START);
    add(cards, BorderLayout.CENTER);
}
public void tableChanged(TableModelEvent e) {
    int row = e.getFirstRow();
    int col = e.getColumn();
    model = (MyTableModel) e.getSource();
    String stulpPav = model.getColumnName(col);
    Object data = model.getValueAt(row, col);
    Object studId = model.getValueAt(row, 0);
    System.out.println("tableChanded works");
    try {
        new ImportData(stulpPav, data, studId);
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    } catch (SQLException e1) {
        e1.printStackTrace();
    }
}

public void actionPerformed(ActionEvent event) {        
        JComboBox cb = (JComboBox) event.getSource();
        String pav = (String) cb.getSelectedItem();
        if (pav != "Chose groop") {
            try {
                model = new MyTableModel(pav);
                table.setModel(model);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

private static void GUI() throws SQLException {
    JFrame frame = new JFrame("E-gradebook");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new TableCombobox());
    frame.pack();
    frame.setSize(800, 400);
    frame.setVisible(true);
}

public static void main(String[] args) throws SQLException {
    try {

        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection("jdbc:mysql://localhost/pki18",
                "root", "");
        GUI();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e1) {
        e1.printStackTrace();
    } finally {
        if (stmt != null)
            stmt.close();
    }
}

static Connection getConnection() {
    return con;
}
}


public class ImportData {
static Connection con = TableCombobox.getConnection();

public ImportData(String a, Object b, Object c)
        throws ClassNotFoundException, SQLException {
    Statement stmt = null;
    try {

        String stulpPav = a;
        String duom = b.toString();
        String studId = c.toString();
        System.out.println(duom);
        con.setAutoCommit(false);
        stmt = con.createStatement();
        stmt.addBatch("update pki18 set " + stulpPav + " = " + duom
                + " where studento_id = " + studId + ";");
        stmt.executeBatch();
        con.commit();
    } catch (BatchUpdateException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (stmt != null)
            stmt.close();
        con.setAutoCommit(true);
        System.out.println("Data was imported to database");
    }
}   
}


public class MyTableModel extends AbstractTableModel{
static int rowCount;
static Object data [][];
static String columnNames [];

public  MyTableModel(String grupName) throws SQLException{
    String query ="select Studento_id, vardas_pavarde, 1_semestras,"+
            " 2_semestras, egzaminas, bendras_balas "+
            "from pki18." + grupName;

    ResultSet rs ;
    Connection con = TableCombobox.getConnection();

    Statement stmt = null;
    stmt = con.createStatement();
    rs = stmt.executeQuery(query);

    rs.last();
    rowCount = rs.getRow();
    data = new Object[rowCount][6];
    rs = stmt.executeQuery(query);
    for (int iEil = 0; iEil < rowCount; iEil++){
        rs.next();                  
        data[iEil][0] = rs.getLong("Studento_id");
        data[iEil][1] = rs.getString("Vardas_Pavarde");
        data[iEil][2]  = rs.getByte("1_semestras");
        data[iEil][3] = rs.getByte("2_semestras");
        data[iEil][4]  = rs.getByte("Egzaminas");
        data[iEil][5] = rs.getByte("bendras_balas");                    
    }

     String[] columnName  = {"Asmens_kodas","Vardas_Pavarde","1_Semestras"
            ,"2_Semestras","Egzaminas","Bendras_Balas"};
     columnNames = columnName;
}   
public int getColumnCount(){
    return columnNames.length;
}
public int getRowCount(){
    return data.length;
}
public String getColumnName(int col){
    return columnNames[col];
}
public Object getValueAt(int row, int col){
    return data[row][col];
}
public Class getColumnClass(int col){
    return getValueAt(0, col).getClass();
}
public boolean isCellEditable(int row, int col){
    return true;
}
public void setValueAt(Object value, int row, int col){
    data[row][col] = value;
    fireTableCellUpdated(row, col);
}
}
公共类TableCombobox扩展JPanel实现ActionListener、TableModelListener{
静态JTable表;
静态JComboBox;
静态MyTable模型;
静态连接con=null;
静态语句stmt=null;
public TableCombobox()引发SQLException{
超级(新边框布局());
表=新的JTable(新的MyTableModel(“pki18”);
table.setPreferredScrollableViewportSize(新维度(500400));
表.setFillsViewPerthweight(真);
table.getModel().addTableModelListener(此);
JScrollPane scrollPane=新的JScrollPane(表);
JPanel menuPanel=新的JPanel();
menuPanel.setLayout(新的BoxLayout(menuPanel,BoxLayout.Y_轴));
menuPanel.setBorder(BorderFactory.CreateMateBorder(0,0,0,1,
颜色(黑色);
字符串[]dalykas={“选择groop”、“pki17”、“pki18”、“pkn18”};
box=新的JComboBox(dalykas);
框。设置最大尺寸(新尺寸(150,25));
box.setAlignmentX(组件左对齐);
box.addActionListener(这个);
框。设置所选索引(2);
菜单面板添加(框);
JPanel cards=新的JPanel(新的CardLayout());
添加(滚动窗格,“视图”);
添加(菜单面板、边框布局、行_开始);
添加(卡片、边框布局、中心);
}
公共作废表已更改(TableModelEvent e){
int row=e.getFirstRow();
int col=e.getColumn();
model=(MyTableModel)e.getSource();
字符串stulpPav=model.getColumnName(col);
对象数据=model.getValueAt(行、列);
对象studId=model.getValueAt(行,0);
System.out.println(“表格化作品”);
试一试{
新的导入数据(stulpPav、数据、studId);
}捕获(ClassNotFoundException e1){
e1.printStackTrace();
}捕获(SQLException e1){
e1.printStackTrace();
}
}
已执行的公共无效actionPerformed(ActionEvent事件){
JComboBox cb=(JComboBox)event.getSource();
String pav=(String)cb.getSelectedItem();
如果(pav!=“选择groop”){
试一试{
模型=新的MyTableModel(pav);
表2.setModel(model);
}捕获(SQLE异常){
e、 printStackTrace();
}
}
}
私有静态void GUI()引发SQLException{
JFrame=新JFrame(“电子成绩册”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setContentPane(新的TableCombobox());
frame.pack();
框架设置尺寸(800400);
frame.setVisible(true);
}
公共静态void main(字符串[]args)引发SQLException{
试一试{
Class.forName(“com.mysql.jdbc.Driver”);
con=DriverManager.getConnection(“jdbc:mysql://localhost/pki18",
"根",;
GUI();
}捕获(SQLE异常){
e、 printStackTrace();
}捕获(ClassNotFoundException e1){
e1.printStackTrace();
}最后{
如果(stmt!=null)
stmt.close();
}
}
静态连接getConnection(){
返回con;
}
}
公共类导入数据{
静态连接con=TableCombobox.getConnection();
公共导入数据(字符串a、对象b、对象c)
抛出ClassNotFoundException、SQLException{
语句stmt=null;
试一试{
字符串stulpPav=a;
字符串duom=b.toString();
字符串studId=c.toString();
系统输出打印项次(duom);
con.setAutoCommit(假);
stmt=con.createStatement();
stmt.addBatch(“更新pki18集”+stulpPav+“=”+duom
+“其中studento_id=“+studId+”;”);
stmt.executeBatch();
con.commit();
}捕获(BatchUpdateException e){
e、 printStackTrace();
}捕获(SQLE异常){
e、 printStackTrace();
}最后{
如果(stmt!=null)
stmt.close();
con.setAutoCommit(真);
System.out.println(“数据已导入数据库”);
}
}   
}
公共类MyTableModel扩展了AbstractTableModel{
静态整数行数;
静态对象数据[];
静态字符串列名称[];
公共MyTableModel(字符串grupName)引发SQLException{
String query=“选择Studento\u id,vardas\u pavarde,1\u semestras,”+
2塞梅斯特拉斯、埃格扎米纳斯、本德拉斯和巴拉斯+
“来自pki18。”+grupName;
结果集rs;
Connection con=TableCombobox.getConnection();
语句stmt=null;
stmt=con.createStatement();
rs=stmt.executeQuery(查询);
rs.last();
rowCount=rs.getRow();
数据=新对象[行计数][6];
rs=stmt.executeQuery(查询);
对于(int-iEil=0;iEil