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