-1

我有一个 JCombobox 和 JTable 在此处输入图像描述


和数据库
在此处输入图像描述

jcombobox 填充了数据库中学生表的 first_name 的值。每当我从数据库中选择一个名称时,我希望课程名称中的课程名称和数据库分数表中的 student_score 值显示在 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();
        }       
    }
}
4

1 回答 1

0
String n=(String)comboBox.getSelectedItem();
String sql="SELECT .... WHERE student.first_name=n)";

该代码对变量“n”没有任何作用。您不能只包含“n”作为字符串的一部分,因为所有的都是字符“n”,而不是变量的值。

PreparedStatement您在上一个问题中获得了一个关于使用 a 的教程的链接(使用JCombobox 中的值填充 JTable)。

那么你在哪里使用“?” 这表示您要为 SQL 语句提供动态值?

您的基本代码应该是:

String sql="SELECT ..... where student.first_name = ?)";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1, n);
ResultSet rs = ps.executeQuery();

现在变量“n”的值将替换“?” 在 SQL 字符串中。

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

于 2019-08-13T14:12:35.867 回答