为什么不显示结果??

daddygirlqiuwf 2002-02-01 04:44:50
执行下查询数据库,在表格中显示查询结果的程序。为什么只出现一个空的Frame,加4个按钮,而没有查询结果呢?


package database;


import java.awt.*;
import java.sql.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;


public class MainFrame extends JFrame {
JPanel contentPane;

//JTable jTable1 = new JTable();

JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
Vector data_vect=new Vector();
PreparedStatement statement;
Connection connection;


final String title[]={"usercode","name","position","department"};


//implement Class AbstractTableModel;
AbstractTableModel tm=new AbstractTableModel(){
public int getColumnCount(){
return title.length;
}
public int getRowCount(){
return data_vect.size();
}
public Object getValueAt(int row,int column){
if(!data_vect.isEmpty())
return ((Vector)data_vect.elementAt(row)).elementAt(column);
else
return null;
}
public String getColumnName(int column){
return title[column];
}
public void setValueAt(Object value,int row,int column){

}
public Class getColumnClass(int c){
return getValueAt(0,c).getClass();
}
public boolean isCellEditable(int row,int column){
return false;//set uneditable is the default mode;
}
public boolean setReadOnly(){
return false;
}
};
JTable jTable1=new JTable(tm);

/**Construct the frame*/
public MainFrame() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
Class.forName("oracle.jdbc.driver.OracleDriver");
connection=DriverManager.getConnection("jdbc:oracle:thin:@wg039:1521:grade98","exercise","exercise");
System.out.println("I can connnect the database!");
setJTable();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {

//setIconImage(Toolkit.getDefaultToolkit().createImage(MainFrame.class.getResource("[Your Icon]")));

contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(400, 300));
this.setTitle("database example");


jTable1.setBounds(new Rectangle(36, 45, 282, 128));
jTable1.addFocusListener(new MainFrame_jTable1_focusAdapter(this));

jButton1.setText("add");
jButton1.setBounds(new Rectangle(36, 173, 70, 34));
jButton1.addActionListener(new MaimFrame_button1_actionAdapter(this));

jButton2.setText("modify");
jButton2.setBounds(new Rectangle(105, 174, 73, 33));
jButton2.addActionListener(new MaimFrame_button2_actionAdapter(this));

jButton3.setText("delete");
jButton3.setBounds(new Rectangle(176, 174, 75, 33));
jButton3.addActionListener(new MaimFrame_button3_actionAdapter(this));

jButton4.setText("exit ");
jButton4.setBounds(new Rectangle(250, 174, 69, 33));
jButton4.addActionListener(new MaimFrame_button4_actionAdapter(this));

contentPane.add(jTable1, null);
contentPane.add(jButton1, null);
contentPane.add(jButton2, null);
contentPane.add(jButton3, null);
contentPane.add(jButton4, null);

}

public static void main(String[] args)
{
MainFrame frame=new MainFrame();
frame.setLocation(150,150);
System.out.println("hello________________");
frame.setSize(550,400);
frame.show();
System.out.println("hello________________");
}

void setJTable(){
try{

String sql="select * from QWF_CLERK_YGXXB";
statement=connection.prepareStatement(sql);
ResultSet result=statement.executeQuery();
data_vect.removeAllElements();
tm.fireTableStructureChanged();
while(result.next())
{
Vector data=new Vector();
String userid=result.getString("userid");
String username=result.getString("username");
String zhiwu=result.getString("zhiwu");
String department=result.getString("department");
//System.out.println(department);
data.addElement(userid);
data.addElement(username);
data.addElement(zhiwu);
data.addElement(department);
data_vect.addElement(data);
}
tm.fireTableStructureChanged();
result.close();
statement.close();
/* int len=data.size()/4;
String[][] items=new String[len][4];
for(int i=0;i<len;i++)
{
items[i][0]=(String)data.elementAt(i*4);
items[i][1]=(String)data.elementAt(i*4+1);
items[i][2]=(String)data.elementAt(i*4+2);
items[i][3]=(String)data.elementAt(i*4+3);
}
*/

jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable1.setCellSelectionEnabled(false);
jTable1.setShowVerticalLines(true);
jTable1.setShowHorizontalLines(true);
JScrollPane jsp=new JScrollPane(jTable1);




}
catch(Exception e){
System.out.println(e.toString());
}
}


/**Overridden so we can exit when window is closed*/
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}

void jButton1_actionPerformed(ActionEvent e) {

}

void jButton2_actionPerformed(ActionEvent e) {

}

void jButton3_actionPerformed(ActionEvent e) {

}

void jButton4_actionPerformed(ActionEvent e) {

}

void jTable1_focusGained(FocusEvent e) {

}

}

class MaimFrame_button1_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;

MaimFrame_button1_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}

class MaimFrame_button2_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;

MaimFrame_button2_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}

class MaimFrame_button3_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;

MaimFrame_button3_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton3_actionPerformed(e);
}
}

class MaimFrame_button4_actionAdapter implements java.awt.event.ActionListener {
MainFrame adaptee;

MaimFrame_button4_actionAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton4_actionPerformed(e);
}
}

class MainFrame_jTable1_focusAdapter extends java.awt.event.FocusAdapter {
MainFrame adaptee;

MainFrame_jTable1_focusAdapter(MainFrame adaptee) {
this.adaptee = adaptee;
}
public void focusGained(FocusEvent e) {
adaptee.jTable1_focusGained(e);
}
}
...全文
45 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
daddygirlqiuwf 2002-02-01
Thank you!
但还是不行?
回复
relive 2002-02-01
void setJTable(){
try{

String sql="select * from QWF_CLERK_YGXXB";
statement=connection.prepareStatement(sql);
ResultSet result=statement.executeQuery();
data_vect.removeAllElements();
tm.fireTableStructureChanged();
while(result.next())
{
Vector data=new Vector();
String userid=result.getString("userid");
String username=result.getString("username");
String zhiwu=result.getString("zhiwu");
String department=result.getString("department");
//System.out.println(department);
data.addElement(userid);
data.addElement(username);
data.addElement(zhiwu);
data.addElement(department);
data_vect.addElement(data);
}
tm.fireTableStructureChanged();
result.close();
statement.close();
/* int len=data.size()/4;
String[][] items=new String[len][4];
for(int i=0;i<len;i++)
{
items[i][0]=(String)data.elementAt(i*4);
items[i][1]=(String)data.elementAt(i*4+1);
items[i][2]=(String)data.elementAt(i*4+2);
items[i][3]=(String)data.elementAt(i*4+3);
}
*/

jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTable1.setCellSelectionEnabled(false);
jTable1.setShowVerticalLines(true);
jTable1.setShowHorizontalLines(true);
JScrollPane jsp=new JScrollPane(jTable1);



/*>>>>>*/ jTable1.updateUI();




}
catch(Exception e){
System.out.println(e.toString());
}
}


回复
daddygirlqiuwf 2002-02-01
还是没有结果!
??
回复
xmvigour 2002-02-01
Vector data=new Vector();

while(result.next())
{
data.clear();
String userid=result.getString("userid");
String username=result.getString("username");
String zhiwu=result.getString("zhiwu");
String department=result.getString("department");
//System.out.println(department);
data.addElement(userid);
data.addElement(username);
data.addElement(zhiwu);
data.addElement(department);
data_vect.addElement((Vector)data.clone());
}
回复
daddygirlqiuwf 2002-02-01
还是没有显示结果呀!在若干此运行中,有一次显示了一条记录,而事实上数据库该表中有5条记录???
回复
xmvigour 2002-02-01
public static void main(String[] args)
{
MainFrame frame=new MainFrame();
frame.setLocation(150,150);
System.out.println("hello________________");
frame.setSize(550,400);
frame.setVisible(true);
frame.show();
回复
相关推荐
发帖
非技术区
创建于2007-09-28

2.3w+

社区成员

Java 非技术区
申请成为版主
帖子事件
创建了帖子
2002-02-01 04:44
社区公告
暂无公告