使用在Jcombobox中选择的值填充JTable [英] populate JTable with the value selected in Jcombobox

查看:222
本文介绍了使用在Jcombobox中选择的值填充JTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JCombobox和JTable

I have a JCombobox and JTable


和数据库


and database

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

jcombobox is filled with the value of first_name of student table from databse. Whenever I select a name from database I want the value of Course name from course and student_score from score table of the database to be displayed in Jtable. Basically I want to display the records of selected in JComboBox in Jtable

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();
        }       
    }
}

推荐答案

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

该代码对变量"n"不执行任何操作.您不能只在字符串中包含"n",因为所有的字符都是字符"n",而不是变量的值.

That code doesn't do anything with the variable "n". You can't just include "n" as part of the String because all the have is the character "n", not the value of the variable.

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

You were given a link to a tutorial on using a PreparedStatement in your last question (Populate JTable with the value in JCombobox).

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

So where do you use the "?" which indicates you want to provide a dynamic value to the SQL statement?

您的基本代码应为:

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

现在,变量"n"的值将替换?"在SQL字符串中.

Now the value of the variable "n" will replace the "?" in the SQL string.

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

Assuming the rest of your SQL is correct then it should work.

这篇关于使用在Jcombobox中选择的值填充JTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆