java swing数据库_使用Java Swing界面实现数据库基本操作-Java频道-中国IT实验室

该博客介绍了一个使用Java Swing创建的GUI应用程序,用于执行数据库的基本操作,包括添加、显示、更新和删除记录。通过JFrame、JTable和JButton组件与数据库交互,实现了对bankAccount表的操作。连接数据库采用的是Microsoft SQL Server驱动,用户可以进行直观的界面操作来管理数据。
摘要由CSDN通过智能技术生成

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.*;

import java.awt.*;

import javax.swing.*;

public class data extends JFrame implements ActionListener{

JButton add,select,del,update;

JTable table;

Object body[][]=new Object[50][4];

String title[]={"编号","姓名","分数","级别"};

Connection conn;

Statement stat;

ResultSet rs;

JTabbedPane tp;

public data() {

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("删除");

add.addActionListener(this);

select.addActionListener(this);

update.addActionListener(this);

del.addActionListener(this);

ps.add(add);ps.add(select);ps.add(update);ps.add(del);

table=new JTable(body,title);

tp=new JTabbedPane();

tp.add("bankAccount表",new JScrollPane(table));

this.getContentPane().add(tp,"Center");

this.getContentPane().add(ps,"South");

this.setVisible(true);

this.connection();

}

public void connection(){

try {

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=dxaw";

conn=DriverManager.getConnection(url,"dxaw","123");

stat = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,

ResultSet.CONCUR_READ_ONLY);

} catch (Exception ex) {

}

}

public static void main(String[] args) {

data data = new data();

}

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();}

}

public void del() {

try {

int row=table.getSelectedRow();

stat.executeUpdate("delete bankAccount where accountID='"+body[row][0]+"'");

JOptionPane.showMessageDialog(null,"数据已成功删除");

this.select();

} catch (SQLException ex) {

}

}

public void update() {

try {

int row=table.getSelectedRow();

JTextField t[]=new JTextField[6];

t[0]=new JTextField("输入姓名:");

t[0].setEditable(false);

t[1]=new JTextField();

t[2]=new JTextField("输入分数:");

t[2].setEditable(false);

t[3]=new JTextField();

t[4]=new JTextField("输入级别:");

t[4].setEditable(false);

t[5]=new JTextField();

String but[]={"确定","取消"};

int go=JOptionPane.showOptionDialog(

null,t,"插入信息",JOptionPane.YES_OPTION,

JOptionPane.INFORMATION_MESSAGE,null,but,but[0]);

if(go==0){

String ownerName=new String(t[1].getText().getBytes("ISO-8859-1"),

"GBK");

String accountValue=t[3].getText();

int accountLevel=Integer.parseInt(t[5].getText());

stat.executeUpdate("update bankAccount set ownerName='"+ownerName+"',

accountValue='"+accountValue+"',accountLevel='"+accountLevel+"'

where accountID='"+body[row][0]+"'");

JOptionPane.showMessageDialog(null,"修改数据成功");

this.select();

}

} catch (Exception ex) {

}

}

public void select() {

try {

for(int x=0;x

--------------------next---------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值