这个类与我的数据库进行通信,检索数据并将它们投射到JTable.I使用了DefaultTableModel,但是列名不会出现。我在另一个类中实例化这个类,在那里我通过{{1来检索带有数据的表}}。
getTable( )
主要课程
package jtable;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
public class ControlDatabase
{
private JTable table;
private Statement stmt;
private DefaultTableModel model;
private String all = "SELECT * FROM Players ORDER BY id ASC";
public ControlDatabase( )
{
String dbUsername = "root";
String dbPassword = "*****";
String dbHost = "localhost";
String dbPort = "3306";
String dbName = "oop_project_database";
String url = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dbName;
try {
Connection con = DriverManager.getConnection(url, dbUsername, dbPassword);
model = new DefaultTableModel();
model.addColumn("Id");
model.addColumn("Onoma");
model.addColumn("Epwnimo");
model.addColumn("Age");
model.addColumn("Nickname");
table = new JTable(model);
stmt = con.createStatement();
updateStatement( all );
} catch (SQLException ex) {
ex.printStackTrace(System.out);
}
}
public void addStatement(String name,String surname,Integer age,String nickname )
{
String query = "INSERT INTO Players (name,surname,age,nickname) VALUES ('"
+ name +
"','"
+ surname +
"',"
+ age +
",'"
+ nickname +
"')";
try {
int x = stmt.executeUpdate( query );
if (x > 0) {
lastStatement(all);
// updateStatement(all , true );
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public Boolean deleteStatement(String id , int row )
{
String query = "DELETE FROM Players WHERE id = " + id ;
try {
int x = stmt.executeUpdate( query );
if (x > 0) {
model.removeRow(row);
return true;
}
return false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public void updateStatement(String query , Boolean flag )
{
try {
ResultSet rs = stmt.executeQuery( query );
while (rs.next()) {
int currentId = rs.getInt("id");
String currentName = rs.getString("name");
String currentSurname = rs.getString("surname");
int currentAge = rs.getInt("age");
String currentNick = rs.getString("nickname");
Object[] currentRow = { currentId ,currentName, currentSurname, currentAge, currentNick };
model.addRow(currentRow);
}
} catch (SQLException ex) {
ex.printStackTrace(System.out);
}
}
public void updateStatement(String query) {
updateStatement(query , false );
}
public void lastStatement(String query )
{
try {
ResultSet rs = stmt.executeQuery( query );
rs.last();
int currentId = rs.getInt("id");
String currentName = rs.getString("name");
String currentSurname = rs.getString("surname");
int currentAge = rs.getInt("age");
String currentNick = rs.getString("nickname");
Object[] currentRow = { currentId ,currentName, currentSurname, currentAge, currentNick };
model.addRow(currentRow);
} catch (SQLException ex) {
ex.printStackTrace(System.out);
}
}
public JTable getTable( ) {
return table;
}
}
答案 0 :(得分:6)
您可能没有将表格放在JScrollPane
中,这就是为什么它不会绘制标题。或致电getTableHeader()
并单独添加。引自JTable
javadoc:
请注意,如果您希望在独立视图中使用JTable(在...之外) 一个JScrollPane)并希望显示标题,你可以使用它 getTableHeader()并单独显示。
编辑:要向[{1}}添加组件,请将组件提供给JScrollPane
的构造函数,或者调用JScrollPane
答案 1 :(得分:5)
您需要将组件添加到JScrollPane的视口,而不是滚动窗格本身。
所以改变:
pa.add(table);
为:
pa.getViewport().add(table);
看看会发生什么。