public class Text extends JFrame. {
..............
String[] column = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k"};
DefaultTableModel mode = new DefaultTableModel(column, 0) {
};
JTable myjtable = new JTable(mode);
.............
private void jbInit() throws Exception {
LinkSQL L = new LinkSQL(); //连接数据库类
String sql = "select * from BBSUsers where UName='tank'";
L.Query(sql); //Query()在连接类中写好的方法
try {
while (f.rs.next()) {
String a = f.rs.getString(1);
String b = f.rs.getString(2);
String c = f.rs.getString(3);
String d = f.rs.getString(4);
String w = f.rs.getString(5);
String h = f.rs.getString(6);
String l = f.rs.getString(7);
String j = f.rs.getString(8);
String k = f.rs.getString(9);
String m = f.rs.getString(10);
String g= f.rs.getString(11);
Object[] row={a,b,c,d,w,h,l,j,k,m,g};
mode.addRow(row);
}
} catch (SQLException ex) {
}
........................
}
//****************************************************************
通过按钮查找SQL中数据并放入表中显示出来
package text;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.*;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
/**
*
Title:
*
*
Description:
*
*
Copyright: Copyright (c) 2008
*
*
Company:
*
* @author not attributable
* @version 1.0
*/
public class Text extends JFrame. {
JPanel contentPane;
JScrollPane jScrollPane1 = new JScrollPane();
JButton jButton1 = new JButton();
JTextField jTextField1 = new JTextField();
String[] column = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k"};
DefaultTableModel mode = new DefaultTableModel(column, 0) {
};
JTable myjtable = new JTable(mode);
public Text() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
Fa f = new Fa();
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame. Title");
jScrollPane1.setBounds(new Rectangle(4, 11, 391, 179));
jButton1.setBounds(new Rectangle(247, 217, 146, 69));
jButton1.setText("jButton1");
jButton1.addActionListener(new Text_jButton1_actionAdapter(this));
jTextField1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 18));
jTextField1.setBounds(new Rectangle(8, 216, 229, 67));
contentPane.add(jScrollPane1);
contentPane.add(jButton1);
contentPane.add(jTextField1);
jScrollPane1.getViewport().add(myjtable);
}
//
public void jButton1_actionPerformed(ActionEvent e) {
String name = jTextField1.getText();
String sql = "select * from BBSUsers where UName='"+name+"'";
f.Query(sql);
try {
while (f.rs.next()) {
String a = f.rs.getString(1);
String b = f.rs.getString(2);
String c = f.rs.getString(3);
String d = f.rs.getString(4);
String w = f.rs.getString(5);
String h = f.rs.getString(6);
String l = f.rs.getString(7);
String j = f.rs.getString(8);
String k = f.rs.getString(9);
String m = f.rs.getString(10);
String g= f.rs.getString(11);
Object[] row={a,b,c,d,w,h,l,j,k,m,g};
mode.addRow(row);
}
} catch (SQLException ex) {
}
}
}
class Text_jButton1_actionAdapter implements ActionListener {
private Text adaptee;
Text_jButton1_actionAdapter(Text adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/13683607/viewspace-263752/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/13683607/viewspace-263752/