📄 userop.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;
public class UserOP extends JFrame implements ActionListener
{
private JButton addMoney = new JButton( "存款" );
private JButton removeMoney = new JButton( "提款" );
private JButton lookupMoney = new JButton( "查询" );
private JButton exit = new JButton( "退出" );
private JTextField howMuch = new JTextField( 15 );
private double money;
static String user;
static String count;
public UserOP(String user)
{
super( "用户操作" );
this.user=user;
Container c = getContentPane();
c.setLayout( new BorderLayout() );
JPanel[] panel = { new JPanel(), new JPanel() };
panel[0].setLayout( new FlowLayout(FlowLayout.CENTER) );
panel[1].setLayout( new GridLayout( 2,2,5,5 ) );
c.add( panel[0], BorderLayout.NORTH );
c.add( panel[1], BorderLayout.CENTER );
panel[0].add( howMuch );
panel[1].add( addMoney );
panel[1].add( removeMoney );
panel[1].add( lookupMoney );
panel[1].add( exit );
addMoney.addActionListener( this );
removeMoney.addActionListener( this );
lookupMoney.addActionListener( this );
exit.addActionListener( this );
setSize( 250, 120 );
setResizable( false );
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void setMoney()
{
try
{
money = Double.parseDouble( howMuch.getText() );
if( money<=0 )
throw new InvalidDepositException();
}
catch( InvalidDepositException ide )
{
money = 0;
JOptionPane.showMessageDialog( this, ide );
}
catch( NumberFormatException nfe )
{
money = 0;
JOptionPane.showMessageDialog( this, "请输入正确的数字" );
}
}
public void actionPerformed( ActionEvent e )
{
if( e.getSource()==addMoney )
{
setMoney();
/*数据库判断*/
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//声明驱动程序
String url="jdbc:odbc:info";//指定数据源名
Connection con=DriverManager.getConnection(url);//连接数据库
PreparedStatement stm=con.prepareStatement
("select 余额 from 客户信息 where 帐户=?");
stm.setString(1,user);
//设置查询中的一个参数
ResultSet result=stm.executeQuery();
double temp=0;
//声明存储查询结果中密码属性的变量
if(result.next())
temp=result.getDouble(1);
//得到查询结果中密码属性值
money+=temp;
Statement stm1=con.createStatement();
stm1.execute("update 客户信息 set 余额='money' where 帐户='user'");
// result.updateDouble("余额",money);
// result.close();
// stm.setDouble(1,money);
//stm.executeQuery();
//执行插入操作
con.close();
}
catch(Exception edfh)
{System.out.println(edfh);}
}
else if( e.getSource()==removeMoney )
{
setMoney();
/*数据库判断*/
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//声明驱动程序
String url="jdbc:odbc:info";//指定数据源名
Connection con=DriverManager.getConnection(url);//连接数据库
PreparedStatement stm=con.prepareStatement
("select 余额 from 客户信息 where 帐户=?");
stm.setString(1,user);
//设置查询中的一个参数
ResultSet result=stm.executeQuery();
double temp=0;
//声明存储查询结果中密码属性的变量
if(result.next())
temp=result.getDouble(1);
//得到查询结果中密码属性值
money=temp-money;
Statement stm1=con.createStatement();
stm1.execute("update 客户信息 set 余额='money' where 帐户='user'");
con.close();
}
catch(Exception edfh)
{System.out.println(edfh);}
}
else if( e.getSource()==lookupMoney )
{
/*数据库判断*/
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//声明驱动程序
String url="jdbc:odbc:info";//指定数据源名
Connection con=DriverManager.getConnection(url);//连接数据库
PreparedStatement stm=con.prepareStatement
("select 余额 from 客户信息 where 帐户=?");
stm.setString(1,user);
//设置查询中的一个参数
ResultSet result=stm.executeQuery();
double temp=0;
//声明存储查询结果中密码属性的变量
if(result.next())
temp=result.getDouble(1);
JOptionPane.showMessageDialog(null,"用户余额为"+temp);
con.close();
}catch(Exception ev){}
}
else if( e.getSource()==exit )
{
int n = JOptionPane.showConfirmDialog(
this, "是否真的要退出", "退出窗口",
JOptionPane.YES_NO_OPTION );
if( n==JOptionPane.YES_OPTION )
{
dispose();
new Login();
}
}
}
public static void main( String[] args )
{
new UserOP(""+1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -