Java 动态向 JTable 中添加数据
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
public class JTableAppClass {
boolean packFrame = false; public JTableAppClass() {
JTableAppFrame frame = new JTableAppFrame();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / ,
(screenSize.height - frameSize.height) / );
frame.setVisible(true);
} public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
new JTableAppClass();
}
});
}
}
操作类:JTableAppFrame.java
package jtabletest;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import java.awt.Rectangle;
import javax.swing.JScrollPane;
import java.util.Vector;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
public class JTableAppFrame extends JFrame {
JPanel contentPane;
Vector CellsVector = new Vector();
Vector TitleVector = new Vector();
JScrollPane scp = new JScrollPane();
JTable tab = null;
DBCon dbcon = new DBCon();
JLabel lbl_name = new JLabel();
JLabel lbl_age = new JLabel();
JLabel lbl_address = new JLabel();
JTextField txt_name = new JTextField();
JTextField txt_age = new JTextField();
JTextField txt_address = new JTextField();
JButton btn_add = new JButton();
JButton btn_del = new JButton();
JButton btn_update = new JButton();
JButton btn_reset = new JButton();
public JTableAppFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
} private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
this.setResizable(false);
setSize(new Dimension(, ));
setTitle("JTable");
scp.setBounds(new Rectangle(, , , ));
lbl_name.setFont(new java.awt.Font("宋体", Font.BOLD, ));
lbl_name.setText("姓名:");
lbl_name.setBounds(new Rectangle(, , , ));
lbl_age.setFont(new java.awt.Font("宋体", Font.BOLD, ));
lbl_age.setText("年龄:");
lbl_age.setBounds(new Rectangle(, , , ));
lbl_address.setFont(new java.awt.Font("宋体", Font.BOLD, ));
lbl_address.setText("地址:");
lbl_address.setBounds(new Rectangle(, , , ));
txt_name.setFont(new java.awt.Font("宋体", Font.BOLD, ));
txt_name.setBounds(new Rectangle(, , , ));
txt_age.setFont(new java.awt.Font("宋体", Font.BOLD, ));
txt_age.setBounds(new Rectangle(, , , ));
txt_address.setFont(new java.awt.Font("宋体", Font.BOLD, ));
txt_address.setBounds(new Rectangle(, , , ));
btn_add.setBounds(new Rectangle(, , , ));
btn_add.setFont(new java.awt.Font("宋体", Font.BOLD, ));
btn_add.setText("添 加");
btn_add.addActionListener(new JTableAppFrame_btn_add_actionAdapter(this));
btn_del.setBounds(new Rectangle(, , , ));
btn_del.setFont(new java.awt.Font("宋体", Font.BOLD, ));
btn_del.setText("删 除");
btn_del.addActionListener(new JTableAppFrame_btn_del_actionAdapter(this));
btn_update.setBounds(new Rectangle(, , , ));
btn_update.setFont(new java.awt.Font("宋体", Font.BOLD, ));
btn_update.setText("修 改");
btn_update.addActionListener(new
JTableAppFrame_btn_update_actionAdapter(this));
btn_reset.setBounds(new Rectangle(, , , ));
btn_reset.setFont(new java.awt.Font("宋体", Font.BOLD, ));
btn_reset.setText("重 置");
btn_reset.addActionListener(new JTableAppFrame_btn_reset_actionAdapter(this));
contentPane.add(scp);
contentPane.add(lbl_name);
contentPane.add(lbl_age);
contentPane.add(lbl_address);
contentPane.add(txt_name);
contentPane.add(txt_age);
contentPane.add(txt_address);
contentPane.add(btn_del);
contentPane.add(btn_update);
contentPane.add(btn_add);
contentPane.add(btn_reset);
dbcon.connectDB();
this.showTable();
tab = new JTable(CellsVector, TitleVector);
scp.getViewport().add(tab);
} public void showTable() {
this.TitleVector.add("姓名");
this.TitleVector.add("年龄");
this.TitleVector.add("地址");
dbcon.select();
ArrayList list = dbcon.select();
for (int i = ; i < list.size(); i++) {
Student stu = (Student) list.get(i);
Vector v = new Vector();
v.add(stu.getName());
v.add(stu.getAge());
v.add(stu.getAddress());
CellsVector.add(v);
}
} String name;
String age;
String address;
public int checkInformation() {
name = this.txt_name.getText();
age = this.txt_age.getText();
address = this.txt_address.getText();
if (name.equals("")) {
JOptionPane.showMessageDialog(this, "您好!请输入姓名!", "提示", );
this.txt_name.grabFocus();
return ;
}
if (age.equals("")) {
JOptionPane.showMessageDialog(this, "您好!请输入年龄!", "提示", );
this.txt_age.grabFocus();
return ;
}
char[] ans = age.toCharArray();
for (int i = ; i < ans.length; i++) {
if (!Character.isDigit(ans[i])) {
JOptionPane.showMessageDialog(this, "您好!年龄输入错误!", "提示", );
this.txt_age.setText("");
this.txt_age.grabFocus();
return ;
}
}
if (age.length() > ) {
JOptionPane.showMessageDialog(this, "您好!年龄最高只能为 100 岁!", "提示", );
this.txt_age.setText("");
this.txt_age.grabFocus();
return ;
}
if (address.equals("")) {
JOptionPane.showMessageDialog(this, "您好!请输入地址!", "提示", );
this.txt_address.grabFocus();
return ;
}
ages = Integer.valueOf(age);
return ;
} int ages;
public void btn_add_actionPerformed(ActionEvent e) {
if (this.checkInformation() == ) {
return;
}
dbcon.insert(name, ages, address);
this.CellsVector.clear();
this.showTable();
this.tab.updateUI();
this.btn_reset_actionPerformed(e);
} ResultSet rs;
public void btn_del_actionPerformed(ActionEvent e) {
String name = this.txt_name.getText();
this.txt_age.setEditable(false);
this.txt_address.setEditable(false);
if (name.equals("")) {
JOptionPane.showMessageDialog(this, "您好!请输入姓名!", "提示", );
this.txt_name.grabFocus();
return;
}
try {
String sql = "select * from Student where name=?";
PreparedStatement ps = DBCon.con.prepareStatement(sql);
ps.setString(, name);
rs = ps.executeQuery();
if (rs.next()) {
ps.close();
dbcon.delete(name);
this.CellsVector.clear();
this.showTable();
this.tab.updateUI();
this.btn_reset_actionPerformed(e);
} else {
JOptionPane.showMessageDialog(this, "您好!该学员不存在!", "提示", );
this.btn_reset_actionPerformed(e);
return;
}
} catch (SQLException ex) {
System.out.println("核对学员信息发生异常" + ex.getMessage());
}
} public void btn_update_actionPerformed(ActionEvent e) {
if (this.checkInformation() == ) {
return;
}
try {
String sql = "select * from Student where name=?";
PreparedStatement ps = DBCon.con.prepareStatement(sql);
ps.setString(, name);
rs = ps.executeQuery();
if (rs.next()) {
ps.close();
dbcon.update(name, ages, address);
this.CellsVector.clear();
this.showTable();
this.tab.updateUI();
this.btn_reset_actionPerformed(e);
} else {
JOptionPane.showMessageDialog(this, "您好!该学员不存在!", "提示", );
this.btn_reset_actionPerformed(e);
return;
}
} catch (SQLException ex) {
System.out.println("核对学员信息发生异常" + ex.getMessage());
}
} public void btn_reset_actionPerformed(ActionEvent e) {
this.txt_name.setText("");
this.txt_age.setText("");
this.txt_address.setText("");
this.txt_age.setEditable(true);
this.txt_address.setEditable(true);
}
} class JTableAppFrame_btn_del_actionAdapter implements ActionListener {
private JTableAppFrame adaptee;
JTableAppFrame_btn_del_actionAdapter(JTableAppFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn_del_actionPerformed(e);
}
} class JTableAppFrame_btn_update_actionAdapter implements ActionListener {
private JTableAppFrame adaptee;
JTableAppFrame_btn_update_actionAdapter(JTableAppFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn_update_actionPerformed(e);
}
} class JTableAppFrame_btn_reset_actionAdapter implements ActionListener {
private JTableAppFrame adaptee;
JTableAppFrame_btn_reset_actionAdapter(JTableAppFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn_reset_actionPerformed(e);
}
} class JTableAppFrame_btn_add_actionAdapter implements ActionListener {
private JTableAppFrame adaptee;
JTableAppFrame_btn_add_actionAdapter(JTableAppFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn_add_actionPerformed(e);
}
}
连接数据库类:DBCon.java
package jtabletest;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class DBCon {
static Connection con;
private final String DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";
private final String URL = "jdbc:odbc:test";
ResultSet rs = null;
Statement st = null;
public DBCon() {
} public void connectDB() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
if (con == null) {
try {
con = DriverManager.getConnection(URL);
} catch (SQLException ex) {
System.out.println("创建连接发生异常:" + ex.getMessage());
System.exit();
}
}
} public ArrayList select() {
ArrayList list = new ArrayList();
String sql = "select * from Student";
try {
st = con.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
Student stu = new Student();
stu.setName(rs.getString());
stu.setAge(rs.getInt());
stu.setAddress(rs.getString());
list.add(stu);
}
} catch (SQLException ex) {
System.out.println("查询数据发生异常:" + ex.getMessage());
} finally {
try {
rs.close();
} catch (SQLException ex1) {
System.out.println("查询数据关闭语句异常:" + ex1.getMessage());
}
}
return list;
} public void insert(String name, int age, String address) {
String sql = "insert into Student values(?,?,?)";
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
ps.setString(, name);
ps.setInt(, age);
ps.setString(, address);
if (ps.executeUpdate() != ) {
JOptionPane.showMessageDialog(null, "恭喜!插入数据成功!", "消息", );
}
} catch (SQLException ex) {
System.out.println("插入数据发生异常:" + ex.getMessage());
} finally {
try {
ps.close();
} catch (SQLException ex1) {
System.out.println("插入数据关闭语句异常:" + ex1.getMessage());
}
}
} public void update(String name, int age, String address) {
String sql = "update Student set age = ?,address = ? where name =?";
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
ps.setInt(, age);
ps.setString(, address);
ps.setString(, name);
if (ps.executeUpdate() != ) {
JOptionPane.showMessageDialog(null, "恭喜!更新数据成功!", "消息", );
}
} catch (SQLException ex) {
System.out.println("修改数据发生异常!");
} finally {
try {
ps.close();
} catch (SQLException ex1) {
System.out.println("修改数据关闭语句异常:" + ex1.getMessage());
}
}
} public void delete(String name) {
String sql = "delete from Student where name =?";
PreparedStatement ps = null;
int delNumber;
try {
ps = con.prepareStatement(sql);
ps.setString(, name);
delNumber = ps.executeUpdate();
if (delNumber != ) {
JOptionPane.showMessageDialog(null, "恭喜!删除数据成功!", "消息", );
}
} catch (SQLException ex) {
System.out.println("删除数据发生异常:" + ex.getMessage());
} finally {
try {
ps.close();
} catch (SQLException ex1) {
System.out.println("删除数据关闭语句异常:" + ex1.getMessage());
}
}
} public void destoryConnection() {
if (con != null) {
try {
con.close();
} catch (SQLException ex) {
System.out.println("释放连接异常:" + ex.getMessage());
}
}
}
}
JavaBean 类:Student.java
package jtabletest;
public class Student {
private String name;
private int age;
private String address;
public Student() {
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setAddress(String address) {
this.address = address;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getAddress() {
return address;
}
}
Java 动态向 JTable 中添加数据的更多相关文章
- ajax异步获取数据后动态向表格中添加数据(行)
因为某些原因,项目中突然需要做自己做个ajax异步获取数据后动态向表格中添加数据的页面,网上找了半天都没有 看到现成的,决定自己写个例子 1.HTML页面 <!doctype html> ...
- Eclipse中java向数据库中添加数据,更新数据,删除数据
前面详细写过如何连接数据库的具体操作,下面介绍向数据库中添加数据. 注意事项:如果参考下面代码,需要 改包名,数据库名,数据库账号,密码,和数据表(数据表里面的信息) package com.ning ...
- 【Java】在JTable中设置鼠标监听器,点击操作对应数据
最终效果 鼠标点击JTable中任一数据,修改相应的信息. 确定点击的行和列 package com.dao; import java.awt.event.MouseAdapter; import j ...
- java向文件中添加数据---手动版日志添加
核心代码为创建多级文件夹创建 //目标文件 File file=new File(filePath); //若不存在即创建文件 if(!file.exists()) { if (!file.getPa ...
- ligerui_实际项目_003:form中添加数据,表格(grid)里面显示,最后将表格(grid)里的数据提交到servlet
实现效果: "Form"中填写数据,向本页"Grid"中添加数据,转换成Json数据提交,计算总和,Grid文本框可编辑,排序 图片效果: 总结: //disp ...
- foreach循环时动态往数组里添加数据
今天在用TP做项目的时候遇到一个问题,foreach的时候需要动态往数组里添加数据,示例代码如下: $arr = array( array('id'=>'字符串1','name'=>'字符 ...
- js如何实现动态在表格中添加标题和去掉标题?
js如何实现动态在表格中添加标题和去掉标题? 一.总结 1.通过table标签的createCaption(),deleteCaption()方法实现. document.getElementById ...
- spring:如何用代码动态向容器中添加或移除Bean ?
先来看一张类图: 有一个业务接口IFoo,提供了二个实现类:FooA及FooB,默认情况下,FooA使用@Component由Spring自动装配,如果出于某种原因,在运行时需要将IFoo的实现,则F ...
- 用PHP向数据库中添加数据
显示页面(用户可见) <body><form action="chuli.php" method="post"> //将该页面接收的数 ...
随机推荐
- 洛谷P2525 Uim的情人节礼物·其之壱 [康托展开]
题目传送门 Uim的情人节礼物·其之壱 题目描述 情人节到了,Uim打算给他的后宫们准备情人节礼物.UIm一共有N(1<=N<=9)个后宫妹子(现充去死 挫骨扬灰!). 为了维护他的后宫的 ...
- MySQL 关于索引那点事
索引 其实数据库中的数据是按页存放的其实索引也是按页存放的所以本质上索引也占硬盘空间(以最小的消耗,换取最大的利益) 索引是一种有效组合数据的方式!为快速查找到指定记录做铺垫 目的就是快速或者某个记录 ...
- 基于特征码文件恢复工具magicrescue
基于特征码文件恢复工具magicrescue 常见类型的文件都包含一些特殊的字节,用来标识文件的类型.这些字节被称为特征码.在磁盘中,当记录文件存储位置的簇损坏后,就可以基于这些特征码来恢复文件. ...
- Kubernetes1.3:POD生命周期管理
转:http://blog.csdn.net/horsefoot/article/details/52324830 (一) 核心概念 Pod是kubernetes中的核心概念,kubernetes对 ...
- CF1060E Sergey and Subway 思维
分两种情况讨论 一种为奇数长为$L$的路径,在经过变化后,我们需要走$\frac{L}{2} + 1$步 一种为偶数长为$L$的路径,在变化后,我们需要走$\frac{L}{2}$步 那么,我们只需要 ...
- BZOJ.3105.[CQOI2013]新Nim游戏(线性基 贪心 博弈论)
题目链接 如果后手想要胜利,那么在后手第一次取完石子后 可以使石子数异或和为0.那所有数异或和为0的线性基长啥样呢,不知道.. 往前想,后手可以取走某些石子使得剩下石子异或和为0,那不就是存在异或和为 ...
- 洛谷.3803.[模板]多项式乘法(NTT)
题目链接:洛谷.LOJ. 为什么和那些差那么多啊.. 在这里记一下原根 Definition 阶 若\(a,p\)互质,且\(p>1\),我们称使\(a^n\equiv 1\ (mod\ p)\ ...
- [AGC016E]Poor Turkeys
[AGC016E]Poor Turkeys 题目大意: 有\(n(n\le400)\)只火鸡,编号为\(1\)到\(n\),有\(m(m\le10^5)\)个人,每人指定了两只火鸡\(x\)和\(y\ ...
- 河南省队选拔 HAOI2015 解题报告
其实省选在四天前就已经结束了,但由于题目难度略大我到今天上午才补完所有题目……(捂脸逃)考场上很幸运,打完了所有我会写的部分分,最后Round1的110分 + Round2的70分,勉强算是没有被 ...
- http协议之 COOKIE
cookie我们都很了解,这里描述下cookie的几个参数意义 key = "qq", value = "Bobser" .. os.time(), path ...