Java SWING如何从数据库中读取多组数据通过JTable显示?

Java SWING如何从数据库中读取多组数据通过JTable显示?,第1张

public void inithavesold(){//建立查询已销售商品表格、模型

try {

Vector<String>columnName = new Vector<String>()//字段名

Vector<Vector<Object>>dataVector = new

Vector<Vector<Object>>()//存储所有数据,里面每个小的Vector是存单行的

columnName.add("编号")

columnName.add("商品名")

columnName.add("数量")

columnName.add("价格")

columnName.add("总盈利")

conn = DataBase.getconnection()

stmt = conn.createStatement()

rs = stmt.executeQuery("select * from havesold")

while(rs.next()){

Vector<Object>vec = new Vector<Object>()//就是这个存单行的,最后放到上面的大的Vector里面

for(int i=1i<=5i++){

vec.add(rs.getObject(i))

}

dataVector.add(vec)

}

tmhavesold = new DefaultTableModel()//建立默认的JTable模型

tmhavesold.setDataVector(dataVector, columnName)//设定模型数据和字段

DataBase.free(conn, stmt, rs)

} catch (SQLException e) {

e.printStackTrace()

}

jtable.setModel(tmhavesold)

}

打成JAR后,你的驱动jar 用eclipse是打包不进去的你可以把你的驱动包解压缩,然后双击你自己打的jar,点添加,把解压缩出来的一个文件添加到你的jar包中去 另外一个方法,把你的jar包和驱动包放在同一个目录下应该可以跑

程序写好了,而且实现了延时加载。以免启动速度过慢。

import java.io.File

import javax.swing.JFrame

import javax.swing.JScrollPane

import javax.swing.JTree

import javax.swing.event.TreeExpansionEvent

import javax.swing.event.TreeExpansionListener

import javax.swing.tree.DefaultMutableTreeNode

import javax.swing.tree.DefaultTreeModel

import javax.swing.tree.MutableTreeNode

public class TreeFrame extends JFrame {

private JTree tree

public static void main(String[] args) {

new TreeFrame()

}

public TreeFrame() {

this.setSize(800, 600)

this.setDefaultCloseOperation(EXIT_ON_CLOSE)

tree = new JTree()

tree.addTreeExpansionListener(new TreeExpansionAction())

setRootDir(new File("c:/windows"))

this.add(new JScrollPane(tree))

this.setVisible(true)

}

private void setRootDir(File dir) {

tree.setModel(new DefaultTreeModel(createNode(dir)))

MutableTreeNode rootNode = (MutableTreeNode) tree.getModel().getRoot()

rootNode.setUserObject(dir)

updateNode(rootNode)

tree.updateUI()

}

private void updateNode(Object object) {

DefaultMutableTreeNode node = (DefaultMutableTreeNode) object

for (int i = 0i <node.getChildCount()i++) {

DefaultMutableTreeNode node2 = (DefaultMutableTreeNode) node.getChildAt(i)

FileObject fileObject = (FileObject) node2.getUserObject()

if (!fileObject.isUpdated()) {

fileObject.setUpdated(true)

File file = fileObject.getFile()

if (file.isDirectory()) {

addSubFile(node2, file)

}

}

}

}

public MutableTreeNode createNode(File dir) {

DefaultMutableTreeNode node = new DefaultMutableTreeNode(new FileObject(dir))

if (dir.isDirectory()) {

addSubFile(node, dir)

}

return node

}

private void addSubFile(DefaultMutableTreeNode node, File dir) {

File[] files = dir.listFiles()

if (files != null) {

for (File file : files) {

node.add(new DefaultMutableTreeNode(new FileObject(file)))

}

}

}

public class TreeExpansionAction implements TreeExpansionListener {

public void treeExpanded(TreeExpansionEvent event) {

updateNode(event.getPath().getLastPathComponent())

}

public void treeCollapsed(TreeExpansionEvent event) {

}

}

public class FileObject {

private File file

private boolean updated

public FileObject(File file) {

this.file = file

}

public File getFile() {

return file

}

public boolean isUpdated() {

return updated

}

public void setUpdated(boolean updated) {

this.updated = updated

}

public String toString() {

return file.getName()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10029939.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存