import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;
import com.mysql.jdbc.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
/*
* 点击任意的一个格子可以进行修改,或者删除整个一行的数据
* 我删除和修改的操作可以直接看到结果,我觉得这样的界面更友好
* 添加操作:每次添加一行,直接在table中添加
* 点击提交可以看到当前数据库中的信息
*/
public class TSQl
extends JFrame implements ActionListener{
static JButton add,select,del,update,close;
static JTable table;
static Object body[][]=new Object[50][4];//显示数据的界面
static String title[]={"name","birthday","homeAddress","classNumbe"};
static Connection conn;
static Statement stat;
static ResultSet rs;
static JTabbedPane tp;
public TSQl() {
super("数据库操作");
this.setSize(400,300);
this.setLocation(300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel ps=new JPanel();
add=new JButton("添加");
select=new JButton("提交");
update=new JButton("更改");
del=new JButton("删除");
close=new JButton("关闭");
add.addActionListener(this);
select.addActionListener(this);
update.addActionListener(this);
del.addActionListener(this);
close.addActionListener(this);
ps.add(add);
ps.add(select);
ps.add(update);
ps.add(del);
ps.add(close);
table=new JTable(body,title);
tp=new JTabbedPane();
tp.add("student",new JScrollPane(table));
this.getContentPane().add(tp,"Center");
this.getContentPane().add(ps,"South");
this.setVisible(true);
this.connection();
this.select();
}
public void connection(){ //数据库的连接
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/student1","root","yangqiumin");
stat = conn.createStatement();
}
catch(ClassNotFoundException e){
JOptionPane.showMessageDialog(null,"数据库驱动存在问题");
}
catch(SQLException e){
JOptionPane.showMessageDialog(null,"数据库位成功打开");
}
}
public static void main(String[] args) {
TSQl sql = new TSQl();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==add){ //添加一条数据
add();
}
if(e.getSource()==select){ //提交操作并显示
select();
}
if(e.getSource()==update){ //修改数据
update();
}
if(e.getSource()==del){ //删除一条数据
del();
}
if(e.getSource()==close){ //关闭数据库
close();
}
}
public void close(){ //关闭数据库
try {
if(rs!=null){
rs.close();
rs=null;
}
if(stat!=null){
stat.close();
stat=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}
catch(SQLException e){
e.printStackTrace();
}
System.exit(0);
}
public void del() {
try {
int row=table.getSelectedRow();
stat.executeUpdate("delete from student3 where name='"+body[row][0]+"'");
JOptionPane.showMessageDialog(null,"数据已成功删除");
this.select();
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"删除数据失败!");
}
}
public void update() { //修改数据
try {
int row=table.getSelectedRow();
int column=table.getSelectedColumn();
JTextField t[]=new JTextField[2];
t[1]=new JTextField();
switch(column){ //根据用户点击的格子确定用户需要修改的是哪一栏的数据
case 0:
t[0]=new JTextField("name:");
break;
case 1:
t[0]=new JTextField("birthday:");
break;
case 2:
t[0]=new JTextField("homeaddress:");
break;
case 3:
t[0]=new JTextField("classnumber:");
break;
}
t[0].setEditable(false);
String but[]={"确定","取消"};
int go=JOptionPane.showOptionDialog(null,t,"修改信息",JOptionPane.YES_OPTION,JOptionPane.INFORMATION_MESSAGE,null,but,but[0]);
//点击“确认”键之后进行数据库的操作
if(go==0){
switch(column){ //修改不同的栏的内容
case 0:
String Name=t[1].getText();
stat.executeUpdate("update student3 set name='"+Name+"' where name='"+body[row][0]+"'");
break;
case 1:
String Birthday=t[1].getText();
stat.executeUpdate("update student3 set birthday='"+java.sql.Date.valueOf(Birthday)+"' where name='"+body[row][0]+"'");
break;
case 2:
String Homeaddress=t[1].getText();
stat.executeUpdate("update student3 set homeaddress='"+Homeaddress+"' where name='"+body[row][0]+"'");
break;
case 3:
String Classnumber=t[1].getText();
stat.executeUpdate("update student3 set classnumber='"+Classnumber+"' where name='"+body[row][0]+"'");
break;
}
JOptionPane.showMessageDialog(null,"修改数据成功");
this.select();
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null,"修改数据错误!");
}
}
public void select() { //显示
try {
for(int x=0;x<body.length;x++){
body[x][0]=null;
body[x][1]=null;
body[x][2]=null;
body[x][3]=null;
}
int i=0;
rs=stat.executeQuery("select * from student3");
while(rs.next()){
body[i][0]=rs.getString("name");
body[i][1]=rs.getDate("birthday");
body[i][2]=rs.getString("homeAddress");
body[i][3]=rs.getString("classNumber");
i=i+1;
}
this.repaint();
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null,"数据库操作失败!");
}
}
private void add() { //添加一条数据
try {
int row=table.getSelectedRow();
try{
String Name=body[row][0].toString();
String Birthday=body[row][1].toString();
String Homeaddress=body[row][2].toString();
String Classnumber=body[row][3].toString();
stat.executeUpdate("insert into student3 values ('"+Name+"','"+Birthday+"','"+Homeaddress+"','"+Classnumber+"')");
JOptionPane.showMessageDialog(null,"数据已成功插入!");
}
catch(Exception ee){
JOptionPane.showMessageDialog(null,"插入数据错误!");
}
}
catch (Exception ex) {
}
}
}
JDBC.rar_Table_jdbc
版权申诉
7 浏览量
2022-09-21
07:30:15
上传
评论
收藏 8KB RAR 举报
小贝德罗
- 粉丝: 71
- 资源: 1万+
最新资源
- 基于Java的本科探索学习项目设计源码 - 本科探索
- 基于Javascript和Python的微商城项目设计源码 - MicroMall
- 基于Java的网上订餐系统设计源码 - online ordering system
- 基于Javascript的超级美眉网络资源管理应用模块设计源码
- 基于Typescript和PHP的编程知识储备库设计源码 - study-php
- Screenshot_2024-05-28-11-40-58-177_com.tencent.mm.jpg
- 基于Dart的Flutter小提琴调音器APP设计源码 - violinhelper
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0