我目前正在尝试使用 MySQL 学习 JTable。我已将我的数据库设置为 JTable 并使用滚动窗格将其添加到我的 GUI 中。
我的问题是,当我单击按钮时,如何创建更新数据库或执行搜索操作的功能(两者都是一样的......)。
这是我的代码(我正在将数据库信息读入我的 .ini 文件)
final Vector columnNames = new Vector();
final Vector data = new Vector();
try
{
// Connect to an Access Database
driver = IniFonksiyon.iniSVNOkut(driver, "databaseBilgileri", "driver");//"com.mysql.jdbc.Driver";
url = IniFonksiyon.iniOkut(url, "databaseBilgileri", "url");//"jdbc:mysql://localhost:3306/";
userid = IniFonksiyon.iniOkut(userid, "databaseBilgileri", "kullanici");
password = IniFonksiyon.iniOkut(password, "databaseBilgileri", "sifre");
dbName = IniFonksiyon.iniOkut(dbName, "databaseBilgileri", "dbIsmi");
Class.forName( driver );
Connection connection = DriverManager.getConnection( url+dbName , userid, password );
// Read data from a table
String sql = "select * from profildb.tbl_detailed";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
connection.close();
}
catch(Exception e)
{
System.out.println( e );
}
// Create table with database data
final JTable table = new JTable(data, columnNames)
{
/**
*
*/
private static final long serialVersionUID = 1L;
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object o = getValueAt(row, column);
if (o != null)
{
return o.getClass();
}
}
return Object.class;
}
};
这是处理创建表的代码,但它与我的主要 GUI 功能直接相关,所以如何刷新/执行其他操作,如搜索功能?