2019-01-24
本程序由java编写,包含了数据库,GUI等知识。
压缩文件中有程序源码,数据库文件,程序使用说明。
程序主要代码如下:
/*
*接口
*/
public ResultSet Select(String x); //查询
public void withDrawal(float x); //取款
public void Remove(String x,float y); //转账
public void Insert(float x); //存款
public boolean Land(String x,String y);
/*
*实现
*/
package Link_ATM;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import Windows_ATM.Windows_land;
public class Operate implements Link_Method{
@Override
public ResultSet Select(String x){ //查询
Link link=new Link();
Statement stmt=null;
ResultSet rs=null;
ResultSet vs = null;
Connection conn=null;
try {
conn=link.Link();
stmt=(Statement) conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
String sql="select * from user where Id= "+x;
try {
rs=stmt.executeQuery(sql);
vs=rs;
} catch (SQLException e) {
e.printStackTrace();
}
return vs;
}
@Override
public void withDrawal(float x) { //取款
Link link=new Link();
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
float g=0f;
try {
conn=link.Link();
stmt=(Statement) conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
String sql="select Money from user where Id= "+Windows_land.x;
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
while(rs.next()) {
float money =rs.getFloat("Money");
if(money>=x) {
g=money-x;
// 取款成功!
}
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
String in="UPDATE user SET Money = '"+g+"'WHERE Id = '"+Windows_land.x+"'" ;
try {
stmt.executeUpdate(in);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
@Override
public void Remove(String x,float y) { //转账
/*
* 有bug 数据库中没有目标用户时 也会进行转账操作 扣除源账户资金
*/
Link link=new Link();
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
float z=0f; //当前账户金额
float out=0f; //对方账户金额
try {
conn=link.Link();
stmt=(Statement) conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
/*
* 查询转账用户是否存在
*/
String pd="select Name from user where Id="+x;
try {
ResultSet result=stmt.executeQuery(pd);
result.next();
if(!result.getString("Name").equals(""))
{
String sql1="select * from user where Id= "+Windows_land.x;
try {
rs=stmt.executeQuery(sql1);
} catch (SQLException e) {
e.printStackTrace();
}
try {
while(rs.next()) {
float money =rs.getFloat("Money");
if(money>=y) {
z=money-y;
// 余额足够转账
}
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/*
* 当前用户金额减少
*/
String in="UPDATE user SET Money = '"+z+"'WHERE Id = '"+Windows_land.x+"'" ;
try {
stmt.executeUpdate(in);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
/*
* 增加对方账户金额
*/
String out2="select * from user where Id= "+x;
try {
rs=stmt.executeQuery(out2);
} catch (SQLException e) {
e.printStackTrace();
}
try {
while(rs.next()) {
float money =rs.getFloat("Money");
z=money+y;
// 增加余额
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
String out1="UPDATE user SET Money = '"+z+"'WHERE Id = '"+x+"'" ;
try {
stmt.executeUpdate(out1);
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
else {
JOptionPane.showMessageDialog(new JFrame().getContentPane(), "无此账户!");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void Insert(float x) { //存款
Link link=new Link();
Statement stmt=null;
ResultSet rs=null;
Connection conn=null;
float y = 0;
try {
conn=link.Link();
stmt=(Statement) conn.createStatement();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
String sql="select Money from user where Id= "+Windows_land.x;
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
try {
while(rs.next()) {
y=rs.getFloat("Money");
y+=x; //存款
}
} catch (SQLException e) {
e.printStackTrace();
}
String in="UPDATE user SET Money = '"+y+"'WHERE Id = '"+Windows_land.x+"'" ;
try {
stmt.executeUpdate(in);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public boolean Land(String x, String y) { //登陆
Link link=new Link();
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
boolean back=false;
String sql="select * from user where Id="+x;
try {
conn=link.Link();
stmt=(Statement)conn.createStatement();
rs=stmt.executeQuery(sql);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
try {
while(rs.next()) {
if(rs.getString("Password").equals(y)) {
back=true;
}
else {
back=false;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return back;
}
}
程序运行截图
下载地址:链接:https://pan.baidu.com/s/1qOuYBUX7D2o_2O3xwwi1-Q
提取码:7uht