如何获得jtable中的数据

xtbzqw 2007-11-25 09:46:44
请教各位大哥
我在做一个毕业论文的时候,我已经新建了一个表格(jtable),可是我不知道怎么去得到用户输入的数据
请各位帮帮忙,帮我指点指点,我在此多谢了啊
...全文
389 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xtbzqw 2007-12-01
  • 打赏
  • 举报
回复
谢谢各位了啊
问题我已经解决了啊
现在正在过去 2007-11-28
  • 打赏
  • 举报
回复
谢谢 看了代码解了我的疑惑啊
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();
}
}
}
marryhong 2007-11-26
  • 打赏
  • 举报
回复
SORRY 写错了!~
marryhong 2007-11-26
  • 打赏
  • 举报
回复
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;


public class Lb extends JFrame implements ActionListener{
JButton b1 = new JButton("open");
JButton b2 = new JButton("save");
JPanel p = new JPanel();
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);

JFileChooser jfc = new JFileChooser();

public Lb() {
this.add(jsp);
this.add(p,BorderLayout.NORTH);
p.add(b1);
p.add(b2);

b1.addActionListener(this);
b2.addActionListener(this);

this.setSize(320,240);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}

public void actionPerformed(ActionEvent e){
String s = e.getActionCommand();
if(s.equals("save")){
save();
}else if(s.equals("open")){
open();
}
}

public void open(){
int n = jfc.showOpenDialog(this);
String s,text="";

if(n==0){
File f = jfc.getSelectedFile();

try{
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);

while((s=br.readLine())!=null){
text+=s+"\n";
}

jta.setText(text);

br.close();
fr.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}else {
JOptionPane.showMessageDialog(this,"choose a file please.","系统提示",JOptionPane.INFORMATION_MESSAGE);
}
}

public void save(){
if(jfc.showSaveDialog(this)==JFileChooser.APPROVE_OPTION){
File f = jfc.getSelectedFile();

try{
FileWriter fr = new FileWriter(f);

fr.write(jta.getText().replaceAll("\n","\r\n"));

fr.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}else{
JOptionPane.showMessageDialog(this,"choose a file please.");
}
}

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


希望对你有帮助!~~

62,623

社区成员

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

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