-------------------------------------------------------------------
1、先写ProductClass类 Class
package com.hygj.tree;
public class ProductClass {
private int id;
private String pname;
private int eid;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
}
public ProductClass(int id, String pname, int eid)
{ ---有参的构造函数
this.id = id;
this.pname = pname;
this.eid = eid;
}
@Override
public String toString()
{ -----在JTree中获取的pname
return pname;
}
}
----------------------------------------------------
2、连接数据库前建立的Data类 Class
package com.hygj.tree;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class Data {
Vector
headV = new Vector();
Vector bodyV =new Vector();
Connection con = null;
Statement sta = null;
ResultSet rs = null;
String url =
"jdbc:sqlserver://localhost:1433;DatabaseName=jinxiaocun";
public Vector getHeadV(String
sql) {
con = getCon();
headV.removeAllElements();
try {
sta =
con.createStatement();
rs =
sta.executeQuery(sql);
int col =
rs.getMetaData().getColumnCount();
for (int
i=1;i<=col;i++)
{
headV.add(rs.getMetaData().getColumnName(i));
}
} catch (SQLException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
return headV;
}
public Vector getBodyV(String sql) {
con = getCon();
bodyV.removeAllElements();
try {
sta =
con.createStatement();
rs =
sta.executeQuery(sql);
while(rs.next())
{
int col =
rs.getMetaData().getColumnCount();
Vector rowV = new Vector();
for(int
i=1;i<=col;i++)
{
rowV.add(rs.getString(i));
}
bodyV.add(rowV);
}
} catch (SQLException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
return bodyV;
}
public Connection getCon() {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("1");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
try {
con =
DriverManager.getConnection(url,"sa","");
System.out.println("2");
} catch (SQLException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
return con;
}
public
Vector
getProductClassV()
{
Vector proV = new
Vector();
if(null == con)
{
con = getCon();
}
try {
sta =
con.createStatement();
String sql = "select * from
product";
rs =
sta.executeQuery(sql);
while (rs.next())
{
int id =
rs.getInt("id");
String name =
rs.getString("pname");
int eid=
rs.getInt("eid");
ProductClass
pro = new ProductClass(id, name, eid); -------打包成对象
proV.add(pro);
}
} catch (SQLException e) {
// TODO Auto-generated catch
block
e.printStackTrace();
}
return proV;
} -----------------此部分为重点
}
-----------------------------------------------------------
3、
package com.hygj.tree;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSplitPane;
import javax.swing.JScrollPane;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.JTree;
import javax.swing.JLabel;
import javax.swing.JTable;
public class Tree extends JFrame {
private JPanel contentPane;
JTree tree;
Data data =new Data();
private JTable table;
public static void main(String[] args) {
EventQueue.invokeLater(new
Runnable() {
public void
run() {
try
{
Tree
frame = new Tree();
frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Tree() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450,
300);
contentPane = new
JPanel();
contentPane.setBorder(new
EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new
BorderLayout(0, 0));
setContentPane(contentPane);
JSplitPane splitPane = new
JSplitPane();
contentPane.add(splitPane,
BorderLayout.CENTER);
JPanel rightP = new
JPanel();
splitPane.setRightComponent(rightP);
rightP.setLayout(new
BorderLayout(0, 0));
JLabel lblNewLabel = new
JLabel("New label");
rightP.add(lblNewLabel,
BorderLayout.NORTH);
JPanel panel = new
JPanel();
rightP.add(panel,
BorderLayout.CENTER);
panel.setLayout(new
BorderLayout(0, 0));
JScrollPane scrollPane = new
JScrollPane();
panel.add(scrollPane,
BorderLayout.CENTER);
table = new JTable();
scrollPane.setViewportView(table);
JScrollPane leftP = new
JScrollPane();
leftP.setPreferredSize(new
Dimension(100, 2));
splitPane.setLeftComponent(leftP);
Hashtable hash = new
Hashtable();
hash.put("简介",data.getProductClassV());
tree = new JTree(hash);
leftP.setViewportView(tree);
}
}
----------------------------------------------------------------