JTree 数据未从数据库加载 (MySQL)
JTree data not loading from the database (MySQL)
我正在 MySQL 数据库上创建类别 table。我需要列出所有需要在 JTree
上查看的类别,但它没有加载。到目前为止尝试过的代码我写在下面。
类别table
+------------+
|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);
}
}
尝试使用 DefaultMutableTreeNode。
这不是我的代码,但试着看看这个例子:http://www.javaknowledge.info/populate-jtree-from-mysql-database/
我正在 MySQL 数据库上创建类别 table。我需要列出所有需要在 JTree
上查看的类别,但它没有加载。到目前为止尝试过的代码我写在下面。
类别table
+------------+
|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);
}
}
尝试使用 DefaultMutableTreeNode。
这不是我的代码,但试着看看这个例子:http://www.javaknowledge.info/populate-jtree-from-mysql-database/