gpt4 book ai didi

java - 程序跳过我创建 JTable 的行

转载 作者:行者123 更新时间:2023-12-01 14:14:43 24 4
gpt4 key购买 nike

我创建了 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();
}

}

最佳答案

  1. 不要创建 JComponents里面try - catch - finally block ,第一个异常(exception)是跳过并执行的所有内容是 catch block (然后 finally block ),空 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. 搜索(在这个论坛上,在这里搜索一百万次)ResultSetTableModel , TableFromDatabase ,简单、清晰且相当安全JDBC _ to _ JTable
  7. 为了获得更好的帮助,请尽快发布 SSCCE ,短,可运行,可编译导致 JTable 的本地变量中的硬编码值而不是任何硬编码值出现问题(隐藏可以存储在那里的任何内容)JDBC代码
  8. 在阅读任何内容之前Oracles tutorials关于try-catch-finallyJTable特别是creating table model部分

关于java - 程序跳过我创建 JTable 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18215755/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com