Java 用Jcombobox中选择的值填充JTable

Java 用Jcombobox中选择的值填充JTable,java,mysql,swing,jtable,jcombobox,Java,Mysql,Swing,Jtable,Jcombobox,我有一个JCombobox和JTable 和数据库 jcombobox由数据库中学生表的first_name值填充。 每当我从数据库中选择一个名称时,我都希望在Jtable中显示来自课程的课程名称和来自数据库分数表的学生_分数的值。 基本上,我想在Jtable中显示JComboBox中选中的记录 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import ja

我有一个JCombobox和JTable


和数据库

jcombobox由数据库中学生表的first_name值填充。 每当我从数据库中选择一个名称时,我都希望在Jtable中显示来自课程的课程名称和来自数据库分数表的学生_分数的值。 基本上,我想在Jtable中显示JComboBox中选中的记录

import java.awt.BorderLayout;
import java.awt.EventQueue;    
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder; 
import net.proteanit.sql.DbUtils;  
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.*;
import java.sql.*;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.PopupMenuEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ShowScore extends JFrame {

    private JPanel contentPane;
    private JTable table;
    private JComboBox comboBox;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ShowScore frame = new ShowScore();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    Connection con=null;
    Score s=new Score();

    public ShowScore() {
        con=MyConnection.getConnection();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 1238, 761);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblScoreSheet = new JLabel("Score Sheet");
        lblScoreSheet.setFont(new Font("Times New Roman", Font.BOLD, 28));
        lblScoreSheet.setBounds(500, 33, 155, 50);
        contentPane.add(lblScoreSheet);

        JPanel panel = new JPanel();
        panel.setBounds(24, 259, 720, 426);
        contentPane.add(panel);
        panel.setLayout(null);

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(12, 13, 675, 452);
        panel.add(scrollPane);

        comboBox = new JComboBox();
        comboBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                table = new JTable();
                scrollPane.setViewportView(table);
                try {
                    String n=(String)comboBox.getSelectedItem();
                    String sql="SELECT score.student_id, student.first_name, student.last_name,course.subject, score.student_score FROM ((score INNER JOIN student ON score.student_id=student.id)\r\n" + 
                            "                   INNER JOIN course ON score.course_id=course.cid WHERE student.first_name=n)";
                    PreparedStatement ps=con.prepareStatement(sql);
                    ResultSet rs=ps.executeQuery();
                    table.setModel(DbUtils.resultSetToTableModel(rs));

                    JLabel lblName = new JLabel("Name:");
                    lblName.setFont(new Font("Times New Roman", Font.PLAIN, 18));
                    lblName.setBounds(102, 107, 56, 16);
                    contentPane.add(lblName);

                } catch (Exception e1) {
                    e1.printStackTrace();
                }               
            }
        });
        s.fillScoreCombo(comboBox); 
        comboBox.setBounds(171, 105, 260, 27);
        contentPane.add(comboBox);

        table = new JTable();
        scrollPane.setViewportView(table);
        try {
            String n=(String)comboBox.getSelectedItem();
            String sql="SELECT score.student_id, student.first_name, student.last_name,course.subject, score.student_score FROM ((score INNER JOIN student ON score.student_id=student.id)\r\n" + 
                    "                   INNER JOIN course ON score.course_id=course.cid)";
            PreparedStatement ps=con.prepareStatement(sql);
            ResultSet rs=ps.executeQuery();
            table.setModel(DbUtils.resultSetToTableModel(rs));

            JLabel lblName = new JLabel("Name:");
            lblName.setFont(new Font("Times New Roman", Font.PLAIN, 18));
            lblName.setBounds(102, 107, 56, 16);
            contentPane.add(lblName);

        } catch (Exception e1) {
            e1.printStackTrace();
        }       
    }
}
该代码对变量“n”没有任何作用。不能只将“n”作为字符串的一部分,因为所有的字符串都是字符“n”,而不是变量的值

您在上一个问题()中获得了一个关于使用
PreparedStatement
的教程链接

那么,您在哪里使用“?”来表示希望为SQL语句提供动态值呢

您的基本代码应该是:

String sql="SELECT ..... where student.first_name = ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, n);
ResultSet rs = ps.executeQuery();
现在,变量“n”的值将替换SQL字符串中的“?”

假设SQL的其余部分是正确的,那么它应该可以工作

“请帮我做这个”不是问题。请更好地描述你的问题。当SQL查询不起作用时,请使用SQL(或mysql)标记创建一个问题。当您遇到Swing UI问题时,请创建一个没有数据库访问权限的简单示例(),以便我们更好地理解和再现您的问题。1)Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。2) 很好的编辑删除请求帮助和修改标题-但是你的问题是什么?猜测“如何使用JComboBox中选择的值填充JTable?”如果是,请编辑以添加它。
String sql="SELECT ..... where student.first_name = ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, n);
ResultSet rs = ps.executeQuery();