0

我创建了 JTable,其中填充了 MySQL 数据库中的数据...如果数据库中不需要数据,则 JTable 应该为空(除了一列,但这并不重要)。

问题是,我的程序只是跳过了 JTable 类方法所在的部分......这是我的整个代码,我只是不知道为什么跳过这些行......我希望它不会太长......我想要你大家看看我在课堂上做了什么,我会标记问题出在哪里......我很抱歉Java命名约定,我不得不使用我的语言中的一些词......

先感谢您...

public static void popuniTerminiTable() {

    terminDatum = Listener.yearChoose.getSelectedItem() + "-" + (Listener.monthChoose.getSelectedIndex() + 1) + "-" +  TerminiPanel.izabraniDan;
    //System.out.println("terminDatum = " + terminDatum);
    try {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/ordinacijaDB";
        String name = "root";
        String password = "admin";

        Class.forName(driver);
        Connection connection = DriverManager.getConnection( url, name, password );


        String checkQuery = ("SELECT COUNT(*) FROM ordinacijadb.zakazivanje WHERE termin = '" + terminDatum + " 7:00:00'");
        Statement checkStmt = connection.createStatement();
        ResultSet rsCheck = checkStmt.executeQuery(checkQuery);

        int number = 0;

        while(rsCheck.next()) {
            number = rsCheck.getInt("COUNT(*)");
        }

        System.out.println("number = " + number);

        if (number == 0) {
            String queryTimeStamp = ("SELECT TIMESTAMP '" + terminDatum + " 7:00:00'");
            Statement stmtTimeStamp = connection.createStatement();
            ResultSet rsTimeStamp = stmtTimeStamp.executeQuery(queryTimeStamp);


            while (rsTimeStamp.next()) {
                Timestamp ts = rsTimeStamp.getTimestamp(1);  

                for (int i = 0; i <= 26; i++) {

                    long vreme = ts.getTime(); 

                    vreme1 = new Time(vreme); 
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
                    String termin = sdf.format(vreme1); 
                    TerminiPanel.zakazivanjeTableModel.setValueAt(termin, i, 0);

                    String queryPolaSata = ("SELECT DATE_ADD('" + terminDatum + " " + vreme1 +"', INTERVAL 30 MINUTE)");
                    Statement stmtPolaSata = connection.createStatement();
                    ResultSet rsPolaSata = stmtPolaSata.executeQuery(queryPolaSata);

                    while(rsPolaSata.next()){
                        ts = rsPolaSata.getTimestamp(1);

                    }
                }
            }
            rsTimeStamp.close();

        } else if(broj == 1)  try {

        Date convertedDate = Date.valueOf(terminDatum);
        String queryUcitajZakazane = ("SELECT * FROM ordinacijaDB.zakazivanje WHERE termin >= '" + convertedDate + " 7:00:00' AND termin <= '" + convertedDate + " 20:00:00'");

        Statement stmtUcitajZakazane = connection.createStatement();
        ResultSet rsUcitajZakazane = stmtUcitajZakazane.executeQuery(queryUcitajZakazane);
        ResultSetMetaData rsmd = rsUcitajZakazane.getMetaData();

        int columns = rsmd.getColumnCount();
        data1.removeAllElements();


        while (rsUcitajZakazane.next()) {

            Vector podatak = new Vector(columns);

            podatak.removeAllElements();
            for (int i = 1; i <= columns; i++) {
                    podatak.add( rsUcitajZakazane.getObject(i) );                   
            }
            data1.addElement(podatak);
        }

        connection.close();
        stmtUcitajZakazane.close();
        rsUcitajZakazane.close();


    } catch (Exception e) {
        e.printStackTrace();
    }

    Vector<String> columnNames1 = new Vector<String>();
    columnNames1.addElement("Termin");
    columnNames1.addElement("Ime i prezime");
    columnNames1.addElement("Napomena");

    DefaultTableModel dtm = new DefaultTableModel(data1, columnNames1); /// LINE ADDED
    JTable zakazivanjeTable1 = new JTable(dtm);   /// LINE ADDED

    zakazivanjeTable1.setRowHeight(27);


    DefaultTableCellRenderer renderer = new DefaultTableCellRenderer(); 
    renderer.setHorizontalAlignment(JTextField.CENTER);

    zakazivanjeTable1.getColumn("Termin").setCellRenderer(renderer);
    zakazivanjeTable1.getColumn("Ime i prezime").setCellRenderer(renderer);
    zakazivanjeTable1.getColumn("Napomena").setCellRenderer(renderer);

    TableColumn column;
    for (int i = 0; i < 3; i++) {
        column = zakazivanjeTable1.getColumnModel().getColumn(i);
        if (i == 0) {
            column.setPreferredWidth(5);
        } else {
            column.setPreferredWidth(170);
       }
    }
    System.out.println("pred dodavanje");
    JScrollPane terminiScrollPane = new JScrollPane(zakazivanjeTable1);
    TerminiPanel.terminPanel.removeAll();
    TerminiPanel.terminPanel.add(TerminiPanel.buttonPanel, BorderLayout.SOUTH);
    TerminiPanel.terminPanel.add(terminiScrollPane, BorderLayout.CENTER);
    TerminiPanel.terminPanel.revalidate();

    } catch (Exception e) {
        e.printStackTrace();
    }

}
4

1 回答 1

1
  1. 不要创建JComponents内部try - catch - finally块,第一个例外是所有跳过和执行的内容都是catch块(然后是finally块),空的 GUI 可能是简单的原因,
  2. 全部创建JComponents为局部变量,那么这个对象是否可以从当前 JVM 的各个角落访问
  3. 不要重新创建JComponents,所有人Swing JComponents都有自己的模型,重用这个模型,对所有人Swing JComponents都有效被指定为可重用(例如TerminiPanel.terminPanel.removeAll(); ---> TerminiPanel.terminPanel.revalidate();)
  4. 放在Statements一起,因为看起来它们是从链接而来的JCalendar value(???),所以有一堆 MySQL 教程,
  5. close()全部JDBC Objectfinally块中,否则保留并增加 JVM 使用的内存
  6. 搜索(在此论坛上,此处为百万次)ResultSetTableModelTableFromDatabase以方便、清晰且非常安全JDBC _ to _ JTable
  7. 为了获得更好的帮助,请尽快发布SSCCE,简短,可运行,可编译导致 JTable 的局部变量中的硬编码值出现问题,而不是任何(隐藏可以存储在那里的任何内容)JDBC代码
  8. 在阅读任何Oracles tutorials内容之前try-catch-finallyJTable尤其是部分creating table model
于 2013-08-13T19:46:12.850 回答