我正在MySQL数据库上创建类别表。我需要列出所有需要在JTree
上查看的类别,但尚未加载。到目前为止我尝试过的代码如下。
类别表
+------------+
|id | catname|
+------------+
|1 | book |
|2 | drink |
|3 | cake |
+------------+
public void LoadtTree()
{
try {
pst = con.prepareStatement("select * from category");
rs = pst.executeQuery();
DefaultMutableTreeNode categories = new DefaultMutableTreeNode("Categories");
String currentGroup = null;
while (rs.next()) {
String group = rs.getString("catname");
DefaultMutableTreeNode categorynames= new DefaultMutableTreeNode(rs.getString("catname"));
categories.add(categorynames);
}
}catch(Exception ex){ex.getMessage();}
}
加载表单时,它仅显示默认节点。如何将数据库值加载到该节点?
连接
Connection con;
PreparedStatement pst;
ResultSet rs;
public void Connect()
{
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/emk", "root","");
} catch (ClassNotFoundException ex) {
Logger.getLogger(fo.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(fo.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:0)
尝试使用DefaultMutableTreeNode。
这不是我的代码,但请尝试查看以下示例:http://www.javaknowledge.info/populate-jtree-from-mysql-database/