我正在与一些人一起开展学校项目,需要将MySQL数据库表(spiele = games)导出到高分列表(基于GUI的grpahics)。 尝试了多种变化,但不知何故代码似乎不正确。
我需要在哪里放置表格代码? 如何正确修改/粘贴rowData和tablecolums? 我可以通过" ValueAt"来填充表格行数据吗?或者这是不同的东西?
我还可以为用户名/ ID / studentID添加另一个字符串吗? 我还可以将这样的数据导出为excel吗?或者这是另一回事吗?
谢谢!
public void initHighScore() throws IOException {
Connection conn = null;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
}
try {
// The newInstance() call is a work around for some
// broken Java implementations
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sys?useSSL=false","admin","admin");
//conn =
// DriverManager.getConnection("jdbc:mysql://localhost/sys?useSSL=false" +
// "user=admin&password=admin");
if (conn!= null) {
System.out.println("Connection established!");
}
String query = "SELECT * FROM spiele";
// create the java statement
Statement st = conn.createStatement();
// execute the query, and get a java resultset
ResultSet rs = st.executeQuery(query);
// iterate through the java resultset
String rowData [][] = new String[][]{
int li_row = 0;
while(rs.next()){
table.setValueAt(rs.getDate("tag"),li_row,0);
table.setValueAt(rs.getString("gewinnername"),li_row,0);
table.setValueAt(rs.getInt("schuesse"),li_row,0);
Date userid = rs.getDate("tag");
int schuesse = rs.getInt("schuesse");
String name1 = rs.getString("gewinnername");
//System.out.println(name1);
li_row++;
}
}
frame2 = new JFrame("High Score");
String columnNames[] = { "Tag", "Gewinner", "Anzahl Schüsse" };
//table = new JTable( DBHighscore() );
table = new JTable(rowData, columnNames);
table.setForeground(Color.white);
Color background = new Color(0,0,0,0);
table.setOpaque(false);
table.setBackground(background);
}
st.close();
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
/*Object rowData [][] = {{ " Rang ", " Punkte ", " Matrikelnummer " },
{ "1", "", "" },{ "", "", "" },{ "", "", "" },{ "", "", "" },{ "", "", "" },{ "", "", "" } };
*/