java中JTable的遍历

wangjiafei 2007-11-26 08:30:57
java中JTable的遍历
...全文
560 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
marryhong 2007-11-26
  • 打赏
  • 举报
回复
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;

public class Phone{

public static void main(String[] args) {
new Dis();
}
}

class Dis extends JFrame implements ActionListener{
JTable t;
JScrollPane js;
JButton b = new JButton("添加");
JPanel p = new JPanel();
String[] dp = {"编号","姓名","性别","生日","电话","住址"};
String[][] dc;

public Dis(){
this.setTitle("通讯录");
this.setLocation(200,200);
this.add(p,BorderLayout.SOUTH);
p.add(b);
b.addActionListener(this);
open();
}

public void actionPerformed(ActionEvent e){
new Add(this);
}

public void open(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=Ph",
"sa",
"");
Statement sta = con.createStatement(1005,1008);
String sql = "select * from Tph";
ResultSet res = sta.executeQuery(sql);
res.last();
int n = res.getRow();
dc = new String[n][6];
res.first();
for(int i=0;i<n;i++){
for(int j=0;j<6;j++){
dc[i][j]=res.getString(j+1);
}
res.next();
}
res.close();
sta.close();
t = new JTable(dc,dp);
js = new JScrollPane(t);
this.add(js);
this.setSize(480,360);
this.setDefaultCloseOperation(3);
this.setVisible(true);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
}
}

class Add extends JFrame implements ActionListener{
JPanel[] p = new JPanel[6];
JLabel[] l = new JLabel[5];
JTextField[] f = new JTextField[5];
String[] s ={"姓名:","性别:","生日:","电话:","住址:"};
JPanel p1 = new JPanel(new GridLayout(6,1));
JButton b1 = new JButton("确定");
JButton b2 = new JButton("取消");
Dis z;

public Add(Dis z){
this.z=z;
this.setTitle("添加朋友");
this.setLocation(200,100);
this.add(p1);
b1.addActionListener(this);
b2.addActionListener(this);
for(int i=0;i<6;i++){
p[i] = new JPanel();
p1.add(p[i]);
}
for(int i=0;i<5;i++){
l[i]=new JLabel(s[i]);
f[i]=new JTextField(10);
}
for(int i=0;i<5;i++){
p[i].add(l[i]);
p[i].add(f[i]);
}
p[5].add(b1);
p[5].add(b2);
this.setSize(400,600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e){
Object obj = e.getSource();
if(obj==b1){
tj();
this.dispose();
}else if(obj==b2){
this.dispose();
}
}

public void tj(){
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
Connection con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=Ph",
"sa",
"");
Statement sta = con.createStatement(1005,1008);
String s1 = f[0].getText();
String s2 = f[1].getText();
String s3 = f[2].getText();
String s4 = f[3].getText();
String s5 = f[4].getText();
String sql = "insert into Tph(sname,ssex,sbr,sph,sds) values('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"')";
sta.executeUpdate(sql);
sta.close();
z.remove(z.js);
z.open();
}catch(ClassNotFoundException ex){
ex.printStackTrace();
}catch(SQLException ex){
ex.printStackTrace();
}
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧