gpt4 book ai didi

mysql - 在 JAVA swing 中将数据库记录显示到 JTable

转载 作者:行者123 更新时间:2023-11-29 00:35:09 25 4
gpt4 key购买 nike

我试图在 Jtable 中显示数据库记录,但我没有得到正确的代码。我使用的是 IDE netbeans,数据库是 mysql。我可以看到面板和滚动 Pane ,但未显示表格。我认为表格属性有问题或者不知道它是否不可见。我的代码如下:

try{        
panel_paylist.setVisible(true);
String dbUrl = "jdbc:mysql://localhost/hostel";
String dbClass = "com.mysql.jdbc.Driver";
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(dbUrl,"root","17121990");
System.out.println("Connected!!!!");
MainScreen obj = new MainScreen(conn);

String[] columnNames = {"First Name",
"Last Name",
"Amount Recvd.",
"Date","Cheque/cash","cheque no","Balance Amt.","Total Amt.",
"Vegetarian"};

ResultSet rs = null;
Statement sql= null;
ArrayList<Object[]> data = new ArrayList<>();
String query="SELECT firstname,lastname, amountreceivd,dte,creditcashcheque,cheque_no,balance_amt, totalamount,Remark FROM payment;";
sql = con.createStatement();
sql.executeQuery(query);
rs = sql.getResultSet();

while(rs.next()){
Object[] row = new Object[]{rs.getString(1),
rs.getString(2),
rs.getInt(3),
rs.getString(4),
rs.getString(5),
rs.getString(6),
rs.getInt(7),
rs.getInt(8),
rs.getString(9)};
data.add(row);
}
Object[][] realData = data.toArray(new Object[data.size()][]);
table_paylist= new JTable(realData, columnNames);
scroll_paylist= new JScrollPane(table_paylist);
table_paylist.setPreferredScrollableViewportSize(new Dimension(800, 200));
table_paylist.setFillsViewportHeight(true);
panel_paylist.setLayout(new BorderLayout());
panel_paylist.add(scroll_paylist, BorderLayout.CENTER);
}
catch(Exception e)
{

}

请帮忙

最佳答案

首先,您缺少本地主机的端口号。改变

"jdbc:mysql://localhost/hostel"

"jdbc:mysql://localhost:3306/hostel";

第二点不要在 sql 字符串末尾使用分号 (;)。我。 e.从

中删除分号

“SELECT firstname,lastname,amountreceivd,dte,creditcashcheque,cheque_no,balance_amt,totalamount,Remark FROM payment;”

你也可以试试这个

public DefaultTableModel PlayList() throws ClassNotFoundException,SQLException,ParseException
{

String[] columnNames = {"First Name","Last Name","Amount Recvd.","Date","Cheque/cash","cheque no","Balance Amt.","Total Amt.","Vegetarian"};

DefaultTableModel dtm = new DefaultTableModel(columnNames , 0);

dtm.setColumnCount(9);

Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:8080/hostel","root","17121990");
PreparedStatement ps1 = null;
ResultSet rs1 = null;

try
{
Class.forName("com.mysql.jdbc.Driver");
ps1=conn.prepareStatement("SELECT firstname,lastname,mountreceivd,dte,creditcashcheque,cheque_no,balance_amt,totalamount,Remark FROM payment");
rs1 = ps1.executeQuery();
while(rs1.next())
{
dtm.addRow(new Object[]{rs1.getString(1)
rs.getString(2),
rs.getInt(3),
rs.getString(4),
rs.getString(5),
rs.getString(6),
rs.getInt(7),
rs.getInt(8),
rs.getString(9)});
}
}
finally
{
rs1.close();
ps1.close();
conn.close();
}
return dtm;
}

现在使用 PlayList() 方法作为 jtable 的 setmodel 方法的参数;

我。 e. jtable.setmodel(PlayList());

关于mysql - 在 JAVA swing 中将数据库记录显示到 JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14687518/

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