首先前期工作这里就不讲了,比如说数据库的建立,MySql连接驱动等内容。
界面主要内容:
(1)运行出现第一个点击按键,点击后进入JTable数据绑定实例界面
(2)接下来就可以进行相应操作。
废话不说,直接上效果图。
图一 添加数据前
图二 添加数据后
图三 修改数据后
图四 删除数据前
图五 删除数据后
下面是代码:
test11.java文件:
package test11;
import java.sql.*;
import javax.swing.JTable;
public class test11 {
/*public static void main(String[] args) {
try {
/*Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
System.out.println("加载数据库驱动成功");
String url="jdbc:mysql://localhost:3306/qwert";//声明数据库test的url
String user="root";//数据库的用户名
String password="d";//数据库的密码
//建立数据库连接,获得连接对象conn(抛出异常即可)
Connection conn=DriverManager.getConnection(url, user, password);
System.out.println("连接数据库成功");
//生成一条mysql语句
//String sql="insert into personInfo(personId,name,gender,age) values('1001','xiaoong','male',23)";
Statement stmt=conn.createStatement();//创建一个Statement对象
//stmt.executeUpdate(sql);//执行sql语句
//System.out.println("插入到数据库成功");
//conn.close();
//System.out.println("关闭数据库成功");
String sql1 = "select * from personInfo"; //要执行的SQL
ResultSet rs = stmt.executeQuery(sql1);//创建数据对象
System.out.println("编号"+"\t\t"+"姓名"+"\t\t"+"性别"+"\t\t"+"年龄");
while (rs.next()){
System.out.printf("%-16s",rs.getInt(1));
System.out.printf("%-16s",rs.getString(2));
System.out.printf("%-16s",rs.getString(3));
System.out.printf("%-16s",rs.getString(4));
System.out.println();
}
rs.close();
String sql2="update personInfo set gender = 'female' where personId = 1002";
stmt.executeUpdate(sql2);//执行sql语句
String sql3="update personInfo set name = 'xiao' where personId = 1002";
stmt.executeUpdate(sql3);//执行sql语句
String sql4="delete from personInfo where age = 21";
stmt.executeUpdate(sql4);//执行sql语句
Connection conn1 = fun1();
Statement stmt=conn1.createStatement();//创建一个Statement对象
fun2(conn1);
fun3(conn1);
stmt.close();
conn1.close();
}
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
public static Connection fun1() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动
System.out.println("加载数据库驱动成功");
String url="jdbc:mysql://localhost:3306/qwert?useUnicode=true&characterEncoding=utf-8";//声明数据库test的url
//?useUnicode=true&characterEncoding=utf-8作用是可以成功的把汉字的数据传输到文本中
String user="root";//数据库的用户名
String password="d";//数据库的密码
//建立数据库连接,获得连接对象conn(抛出异常即可)
conn=DriverManager.getConnection(url, user, password);
System.out.println("连接数据库成功");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//
catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public static int i=1;
public static void fun2(Connection conn,String sql) { //fun2作用是向数据库中插入同类型数据
try
{
Statement stmt=conn.createStatement();//创建一个Statement对象
stmt.executeUpdate(sql);//执行sql语句
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("插入到数据库成功");
}
public static Object[][] fun3(Connection conn) throws SQLException {
String sql = "select * from personInfo";
java.sql.PreparedStatement pstm =null;
try
{
pstm = conn.prepareStatement(sql);
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// 执行查询
ResultSet rs = pstm.executeQuery();
// 计算有多少条记录
int count = 0;
while(rs.next()){
count++;
}
rs = pstm.executeQuery();
// 将查询获得的记录数据,转换成适合生成JTable的数据形式
Object[][] info = new Object[count][4];
count = 0;
while(rs.next()){
info[count][0] = Integer.valueOf( rs.getInt("personId"));
info[count][1] = rs.getString("name");
info[count][2] = Integer.valueOf( rs.getInt("age") );
info[count][3] = rs.getString("gender");
count++;
}
// 定义表头
// 创建JTable
return info;
}
public static void fun4(Connection conn,String sql4) {
try
{
Statement stmt = conn.createStatement();
//String sql2="update personInfo set gender = 'female' where personId = 1012";
stmt.executeUpdate(sql4);//执行sql语句
} catch (SQLException e)
{
e.printStackTrace();
}
System.out.println("修改到数据库成功");
}
public static void fun5(Connection conn,String sql5) {
try
{
Statement stmt = conn.createStatement();
//String sql4="delete from personInfo where gender = female";
stmt.executeUpdate(sql5);//执行sql语句
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("删除到数据库成功");
}
}
jiemian11.java文件
package test11;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.JTableHeader;
public class jiemian11 extends JFrame
{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
jiemian11 frame = new jiemian11();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public jiemian11()
{
super("学生信息管理系统");
JPanel contentPane = new JPanel();
this.setBounds(100, 100, 450, 300);
//setBounds(x,y,width,height); x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 width:组件的长度 height:组件的高度
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JButton b1 = new JButton("点击");
contentPane.add(b1, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1.addActionListener(new ActionListener(){ //b1按钮的点击事件
public void actionPerformed(ActionEvent arg0) {
//这里就是侦听后触发事件处理的代码部分
//比如说你要触发的另一个class类名为NewWindow,是继承自JFrame的窗体class
//那么如下代码就可以了
//test11 t1= new test11();
//Connection conn1 = (Connection) t1.fun1();
//t1.fun3(conn1);
test frame1 = new test();
frame1.setVisible(true);
//这样就实现该按钮触发新的class程序NewWindow了
}
});
}
}
class test extends JFrame{
// 定义组件
private JScrollPane scpDemo;
private JTableHeader jth;
private JTable tabDemo;
private JButton btnShow,b2,b3,b4;
private TextField text1,text2,text3;
// 构造方法
public test(){
// 窗体的相关属性的定义
super("JTable数据绑定示例");
this.setSize(600,400);
this.setLayout(null);
this.setLocation(300,40);
// 创建组件
this.scpDemo = new JScrollPane();
this.scpDemo.setBounds(20,50,560,300);
this.btnShow = new JButton("显示数据");
text1 = new TextField();
text2 = new TextField();
text3 = new TextField();
b2 = new JButton("添加数据");
b3 = new JButton("修改数据");
b4 = new JButton("删除数据");
b2.setBounds(165, 10, 125, 30);
b3.setBounds(310, 10, 125, 30);
b4.setBounds(455, 10, 125, 30);
this.btnShow.setBounds(20,10,125,30);
// 给按钮注册监听
this.btnShow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
try
{
btnShow_ActionPerformed1(ae);
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
b2.addActionListener(new ActionListener(){ //按钮的点击事件
public void actionPerformed(ActionEvent ae){
btnShow_ActionPerformed2(ae);
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnShow_ActionPerformed3(ae);
}
});
b4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
btnShow_ActionPerformed4(ae);
}
});
// 将组件加入到窗体中
add(this.scpDemo);
add(this.btnShow);
add(b2);
add(b3);
add(b4);
// 显示窗体
this.setVisible(true);
}
test11 t1= new test11();
java.sql.Connection conn = t1.fun1();
// 点击按钮时的事件处理
public void btnShow_ActionPerformed1(ActionEvent ae) throws ClassNotFoundException{
try{
//java.sql.Connection conn = t1.fun1();
{
Object[][] info = test11.fun3(conn); //将数据库数据转换为jtable表
// 定义表头
String[] title = {"学号","姓名","年龄","性别"};
// 创建JTable
this.tabDemo = new JTable(info,title);
// 显示表头
this.jth = this.tabDemo.getTableHeader();
// 将JTable加入到带滚动条的面板中
this.scpDemo.getViewport().add(tabDemo);
}
}catch(SQLException sqle){
JOptionPane.showMessageDialog(null,"数据操作错误","错误",JOptionPane.ERROR_MESSAGE);
}
//catch(ClassNotFoundException cnfe){
//JOptionPane.showMessageDialog(null,"数据源错误","错误",JOptionPane.ERROR_MESSAGE);
//}
}
public void btnShow_ActionPerformed2(ActionEvent ae) {
JDialog jd = new JDialog();
jd.setBounds(320, 180, 600, 300);
jd.setTitle("弹出文本框");
text1.setBounds(20,50,560,300);
jd.add(text1);
jd.setModal(true);//确保弹出的窗口在其他窗口前面
jd.setVisible(true);
String gm = text1.getText();
t1.fun2(conn,gm); //向数据表中添加数据
//text1.setText("");
}
public void btnShow_ActionPerformed3(ActionEvent ae) {
JDialog jd = new JDialog();
jd.setBounds(400, 220, 600, 300);
jd.setTitle("弹出文本框");
text2.setBounds(20,50,560,300);
jd.add(text2);
jd.setModal(true);//确保弹出的窗口在其他窗口前面
jd.setVisible(true);
String gm = text2.getText();
t1.fun4(conn,gm); //将数据表中数据进行修改
//text2.setText("");
}
public void btnShow_ActionPerformed4(ActionEvent ae) {
JDialog jd = new JDialog();
jd.setBounds(4800, 260, 600, 300);
jd.setTitle("弹出文本框");
text3.setBounds(20,50,560,300);
jd.add(text3);
jd.setModal(true);//确保弹出的窗口在其他窗口前面
jd.setVisible(true);
String gm = text3.getText();
t1.fun5(conn,gm); //将数据表中数据进行删除
//text3.setText("");
}
}
总结,花了两天时间,逛了众多大佬的博客,终于写出来了。。。
恩,这就是我想说的。