当前位置: 技术问答>java相关
从数据库中查询出的Resultset如何在JTable中显示
来源: 互联网 发布时间:2015-03-24
本文导语: 老是提示无效的列索引,我不知道怎样用JTable,因为数据的行列都是动态的 package gistar.classes; import java.lang.*; import java.util.*; import java.sql.*; import java.math.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import...
老是提示无效的列索引,我不知道怎样用JTable,因为数据的行列都是动态的
package gistar.classes;
import java.lang.*;
import java.util.*;
import java.sql.*;
import java.math.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import oracle.jdbc.driver.*;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
public class frmMdi extends JFrame implements ActionListener {
JTable Btable;
JScrollPane scrollpane1;
public frmMdi(){
super("Gistar");
addWindowListener(new Wadapt());
setSize(600,500);
getContentPane().setLayout(new FlowLayout());
jinit();
//setVisible(true);
}
private void jinit(){
JMenuBar mb=new JMenuBar();
setJMenuBar(mb);
JMenu filemenu=new JMenu("File");
mb.add(filemenu);
JMenuItem miLoginOut=new JMenuItem("Login");
miLoginOut.addActionListener(this);
filemenu.add(miLoginOut);
JMenuItem miExit=new JMenuItem("Exit");
miExit.addActionListener(this);
filemenu.add(miExit);
JMenuItem miFixSearch=new JMenuItem("search");
miFixSearch.addActionListener(this);
filemenu.add(miFixSearch);
//MyTableModel myModel = new MyTableModel();
//JTable table = new JTable(myModel);
//Create the scroll pane and add the table to it.
Btable=new JTable();
Btable.setPreferredScrollableViewportSize(new Dimension(500, 400));
scrollpane1 = new JScrollPane(Btable);
//Add the scroll pane to this window.
getContentPane().add(scrollpane1, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt){
if (((JMenuItem)(evt.getSource())).getText()=="search"){
search();
}
if (((JMenuItem)evt.getSource()).getText()=="Exit"){
System.exit(0);
}
}
private void search(){
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("java:oracle:thin:@192.168.200.101:1521:orical","hh","hh");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM BUILDING" );
ResultSetMetaData rsmd = rs.getMetaData();
int i=0;
int j;
Btable=new JTable(rs.getRow(),rsmd.getColumnCount());
while( rs.next() )
{
for(j=0;j
package gistar.classes;
import java.lang.*;
import java.util.*;
import java.sql.*;
import java.math.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import oracle.jdbc.driver.*;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.JScrollPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
public class frmMdi extends JFrame implements ActionListener {
JTable Btable;
JScrollPane scrollpane1;
public frmMdi(){
super("Gistar");
addWindowListener(new Wadapt());
setSize(600,500);
getContentPane().setLayout(new FlowLayout());
jinit();
//setVisible(true);
}
private void jinit(){
JMenuBar mb=new JMenuBar();
setJMenuBar(mb);
JMenu filemenu=new JMenu("File");
mb.add(filemenu);
JMenuItem miLoginOut=new JMenuItem("Login");
miLoginOut.addActionListener(this);
filemenu.add(miLoginOut);
JMenuItem miExit=new JMenuItem("Exit");
miExit.addActionListener(this);
filemenu.add(miExit);
JMenuItem miFixSearch=new JMenuItem("search");
miFixSearch.addActionListener(this);
filemenu.add(miFixSearch);
//MyTableModel myModel = new MyTableModel();
//JTable table = new JTable(myModel);
//Create the scroll pane and add the table to it.
Btable=new JTable();
Btable.setPreferredScrollableViewportSize(new Dimension(500, 400));
scrollpane1 = new JScrollPane(Btable);
//Add the scroll pane to this window.
getContentPane().add(scrollpane1, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt){
if (((JMenuItem)(evt.getSource())).getText()=="search"){
search();
}
if (((JMenuItem)evt.getSource()).getText()=="Exit"){
System.exit(0);
}
}
private void search(){
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("java:oracle:thin:@192.168.200.101:1521:orical","hh","hh");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM BUILDING" );
ResultSetMetaData rsmd = rs.getMetaData();
int i=0;
int j;
Btable=new JTable(rs.getRow(),rsmd.getColumnCount());
while( rs.next() )
{
for(j=0;j