时间:2021-07-01 10:21:17 帮助过:26人阅读
JTable控件与H2数据库的使用方法: JTable使用的关键点有两个: 1. JTable的数据 抽象类AbstractTableModel的子类,完成数据的管理。 JTable类---------------使用AbstractTableModel子类创建,直接提取Abstract子类的数据。 2.JTable的滚动 将JTable加入JSc
JTable控件与H2数据库的使用方法:
JTable使用的关键点有两个:
1. JTable的数据
抽象类AbstractTableModel的子类,完成数据的管理。
JTable类---------------使用AbstractTableModel子类创建,直接提取Abstract子类的数据。
2.JTable的滚动
将JTable加入JScrollPane
将JScrollPane加入JFrame
通过JScrollPane面板类完成滚动。其实类似的面板容器还有许多
JTabbedPane------选项卡面板
JSplitPane-----------分割条面板
JScrollPane----------滚动条面板
JDesktopPane-------MDI面板
3.数据的存储问题
全部数据存储在集合类中,如Array,Vector
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package testh2; import java.awt.BorderLayout; import java.awt.EventQueue; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Vector; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; class TableModel extends AbstractTableModel { public VectorcolumnsName=new Vector (); public Vector Data=new Vector (); //public String[] columnsName={"a","b"}; //public Object[][] Data={{"a1","b1"},{"a2","b2"}}; public int getRowCount() { return Data.size(); } public int getColumnCount() { return columnsName.size(); } public Object getValueAt(int rowIndex, int columnIndex) { return Data.get(rowIndex)[columnIndex]; } } /** * * @author wind */ public class TestH2 { public static void GUItest() throws InterruptedException { final TableModel tm=new TableModel(); tm.columnsName.add("第一列"); tm.columnsName.add("第二列"); tm.columnsName.add("第三列"); tm.columnsName.add("第四列"); tm.columnsName.add("第五列"); tm.columnsName.add("第六列"); tm.columnsName.add("第七列"); tm.columnsName.add("第八列"); EventQueue.invokeLater(new Runnable() { public void run() { JFrame jf=new JFrame(); JTable jt=new JTable(tm); jt.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); JScrollPane js=new JScrollPane(jt); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.setSize(600, 400); jf.setVisible(true); jf.add(js); } }); for(int i=3;i<50;i++) { tm.Data.add(new String[]{"a"+i,"b"+i,"c"+i,"d"+i,"e"+i,"f"+i,"g"+i,"h"+i}); //Thread.sleep(1000); } } public static void H2Test() throws ClassNotFoundException, SQLException { Class.forName("org.h2.Driver"); Connection conn = DriverManager.getConnection("jdbc:h2:E:/test", "sa", "123456"); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("select * from Test"); String str; while (rs.next()) { str = rs.getString(1) + "\t"; str += rs.getString(2) + "\t"; str += rs.getString(3) + "\t"; str += rs.getString(4) + "\t"; str += "\n"; System.out.println(str); } } public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException { // TODO code application logic here H2Test(); GUItest(); } }