jtree和mysql的应用_JTree连接数据库 并调用数据库的内容

-------------------------------------------------------------------

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);

}

}

----------------------------------------------------------------

a4c26d1e5885305701be709a3d33442f.png

a4c26d1e5885305701be709a3d33442f.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值