使用mysql填充JTable

时间:2013-12-29 21:49:12

标签: java mysql sql swing jtable

我正在制作这个从头开始显示来自mysql数据库的信息的小程序(没有拖放)。 好的,所以我尝试使用NetBeans with Drag and Drop方法练习如何用来自mysql数据库的信息填充JTable,它就像一个魅力。 但是当我对其进行硬编码时(如果是这个术语)它将无法正常工作。 这是我的代码。对不起,如果它凌乱或我仍然是一个菜鸟。

    import java.sql.*;
    import java.awt.Dimension;
    import javax.swing.*;
    import net.proteanit.sql.DbUtils;
    public class Main extends JFrame{

    Connection conn = null;
    ResultSet rs  = null;
    PreparedStatement pst = null;



public JLabel label1,label2,label3,background;
public JButton button1 , button2 , button3;
public JTable table = new JTable();

    //query for updating the JTable

public void UpdateJTable(){
        String sql = "SELECT name , TypeDebt , amount , DateDebt,Due_Date from btable";
        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            table.setModel(DbUtils.resultSetToTableModel(rs));
        }catch(Exception xe){
            JOptionPane.showMessageDialog(null, "error");

        }
    }

    //WindowEvent code
    private void formWindowOpened(java.awt.event.WindowEvent evt){
         conn = sqlconnection.ConnectDb();
         UpdateJTable();
     }
    //constructor to be called in the main method
    public Main(){
        init();
    }

    //method for the GUI of the program
public void init(){


        // Images for the buttons
    ImageIcon img  = new ImageIcon("C:\\Users\\janyjan\\Pictures\\addbutton.jpg");
    ImageIcon img2  = new ImageIcon("C:\\Users\\janyjan\\Pictures\\searchbutton.jpg");
    ImageIcon img3  = new ImageIcon("C:\\Users\\janyjan\\Pictures\\deletebutton.jpg");

    // frame components/attributes
    setLayout(null);
    getContentPane();
    setTitle("DEBT LIST");
    setResizable(false);
    JLabel background=new JLabel(new ImageIcon("C:\\Users\\janyjan\\Pictures\\background.jpg"));
    background.setBounds(0,0,700,600);
    add(background);

    setVisible(true);
    setSize(700,600);
    getContentPane();
    setLocationRelativeTo(null);

    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowOpened(java.awt.event.WindowEvent evt) {
            formWindowOpened(evt);
        }
    });
            //Button attributes - Location and Designs
    button1 = new JButton();
    button1.setBounds(30,130,150,30);
button1.setIcon(img);
background.add(button1);

    button2 = new JButton();
    button2.setBounds(30,90,150,30);
    button2.setIcon(img2);
    background.add(button2);

    button3 = new JButton();
    button3.setBounds(30,170,150,30);
    button3.setIcon(img3);
    background.add(button3);



   //Jtable attribute and location
    JTable table = new JTable();
    table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][]{
                {null , null , null , null , null},
                {null , null , null , null , null},
                {null , null , null , null , null},
                {null , null , null , null , null},
                {null , null , null , null , null},
                {null , null , null , null , null},
                {null , null , null , null , null},
                {null , null , null , null , null},
                {null , null , null , null , null}
            },
            new String []{
                "name" , "TypeDebt" , " amount " , "DateDebt" , "Due_Date"
            }
            ));

    table.setPreferredScrollableViewportSize(new Dimension(300,400));
    table.setSelectionBackground(getBackground());

    JScrollPane scrollpane = new JScrollPane(table);

    scrollpane.setBounds(200, 60, 490, 490);
    background.add(scrollpane);





}



// Main Method  
public static void main(String args[]){

        java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new Main();

        }
    });
 }//end of main method
 }//end of class Main

1 个答案:

答案 0 :(得分:2)

除了使用null布局,这将导致无趣和快乐的结束,你正在隐藏你的table变量......

基本上,您将table声明为实例变量...

public class Main extends JFrame {

    //...
    public JTable table = new JTable();

    //query for updating the JTable
    public void UpdateJTable() {

然后,您在init方法

中将其重新声明为局部变量
public void init() {
    //...
    //Jtable attribute and location
    JTable table = new JTable();
    table.setModel(new javax.swing.table.DefaultTableModel(
                    new Object[][]{
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null},
                        {null, null, null, null, null}
                    },
                    new String[]{
                        "name", "TypeDebt", " amount ", "DateDebt", "Due_Date"
                    }
    ));

    table.setPreferredScrollableViewportSize(new Dimension(300, 400));
    table.setSelectionBackground(getBackground());

    JScrollPane scrollpane = new JScrollPane(table);

    scrollpane.setBounds(200, 60, 490, 490);
    background.add(scrollpane);

这意味着当您调用UpdateJTable方法时,实际上是在与实例变量进行交互,而不是屏幕上的对象。

看看: