初出茅庐,写几行代码给自己看,不足之处还望见谅。此系统是为饭卡充值系统,可实现充值,注销,转账,查余额等操作,代码如下:
main类
package System;
public class main {
public int ID;
public String Name;
public int Money;
public String class1;
public main(){
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getMoney() {
return Money;
}
public void setMoney(int money) {
Money = money;
}
public String getClass1() {
return class1;
}
public void setClass1(String class1) {
this.class1 = class1;
}
}
UI类(主界面)
package System;
import java.util.Scanner;
import Operation.Delete;
import Operation.Insert;
import Operation.Transfer;
import Operation.Update;
public class UI {
public static void main(String[] args) {
main m = new main();
Insert r = new Insert();
Update u = new Update();
Delete D = new Delete();
Transfer T = new Transfer();
Scanner input = new Scanner(System.in);
System.out.print("请输入您要执行的命令(1.添加信息 2.删除信息 3.修改信息 4.转账服务 ):");
int w = input.nextInt();
System.out.println();
if(w==1){//添加信息
//输入学号
System.out.print("请输入您的学号:");
int a = input.nextInt();
m.setID(a);
//输入姓名
System.out.print("请输入您的姓名:");
String b = input.next();
m.setName(b);
//输入充值金额
System.out.print("请输入您充值的金额:");
int c = input.nextInt();
m.setMoney(c);
//输入班级
System.out.print("请输入您的班级:");
String d = input.next();
m.setClass1(d);
//调用system函数进行添加
r.system(a,b,c,d);
}
else if(w==2){//注销账户
System.out.print("请输入您要注销的账户ID:");
int a = input.nextInt();
String b = null;
int c = 0;
String d = null;
D.system(a, b, c, d);
}
else if(w==3){//修改账户
System.out.print("请输入您要修改的账户用户名:");
String b = input.next();
System.out.print("请输入您要修改的内容(1.金额 2.班级):");
int p = input.nextInt();
if(p==1){
System.out.print("请输入您要修改的金额:");
int q = input.nextInt();
String s="Money";
String o="Name";
u.system(b, q, s,o);
}
else{
System.out.print("请输入您要修改的班级:");
String q = input.next();
String s="class";
String o="Name";
u.system2(b, q, s,o);
}
}
else if(w==4){//转账服务
int n1 = 0;
int n2 = 0;
System.out.print("请输入您要转账的ID和收款人的ID:");
n1 = input.nextInt();//转账者的ID
n2 = input.nextInt();//收款者的ID
System.out.print("请输入转账的金额:");
int s1 = input.nextInt();
T.system1(s1,n1);
T.system2(s1,n2);
}
else{
System.out.println("此服务还在开发,还望见谅!");
}
}
}
增加数据到数据库
package Operation;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Insert {//饭卡充值系统-添加用户系统
public void system(int a,String b,int c,String d){
Connection o = null;//定义两个变量
Statement s = null;
try{//****
Class.forName("com.mysql.cj.jdbc.Driver");
//初始化驱动
//Mysql 8.0之后要加.cj
o = DriverManager.getConnection("jdbc:mysql://localhost:3306/meal?characterEncoding=UTF-8&serverTimezone=GMT","root","13");
//与Mysql数据库建立连接
s = o.createStatement();
//创建Statement s 用于执行sql语句
String sql = String.format("insert into system values(%d,'%s',%d,'%s')", a,b,c,d);
//执行insert语句插入数据
s.execute(sql);
//调用.execute()执行sql语句
System.out.println("充值成功,用户名为:"+b);
}catch(ClassNotFoundException e){//抛异常
e.printStackTrace();
}catch(SQLException e){//抛异常
e.printStackTrace();
}finally{//关闭数据库连接
//先关闭Statement
if(s!=null)
try{
s.close();
}catch(SQLException e){
e.printStackTrace();
}
//再关闭Connection
if(o!=null)
try{
o.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
修改数据
package Operation;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Update {//饭卡充值系统-更新修改系统
public void system(String b,int c,String x,String y){
Connection o = null;
Statement s = null;
try{//****
Class.forName("com.mysql.cj.jdbc.Driver");
//初始化驱动
//Mysql 8.0之后要加.cj
o = DriverManager.getConnection("jdbc:mysql://localhost:3306/meal?characterEncoding=UTF-8&serverTimezone=GMT","root","13");
//与Mysql数据库建立连接
s = o.createStatement();
//创建Statement s 用于执行sql语句
String sql = String.format("update system set %s=%d where %s = '%s'",x,c,y,b);
//执行insert语句插入数据
s.execute(sql);
//调用.execute()执行sql语句
System.out.println("更新成功,用户名为:"+b);
}catch(ClassNotFoundException e){//抛异常
e.printStackTrace();
}catch(SQLException e){//抛异常
e.printStackTrace();
}finally{//关闭数据库连接
//先关闭Statement
if(s!=null)
try{
s.close();
}catch(SQLException e){
e.printStackTrace();
}
//再关闭Connection
if(o!=null)
try{
o.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
public void system2(String b,String c,String x,String y){
Connection o = null;
Statement s = null;
try{//****
Class.forName("com.mysql.cj.jdbc.Driver");
//初始化驱动
//Mysql 8.0之后要加.cj
o = DriverManager.getConnection("jdbc:mysql://localhost:3306/meal?characterEncoding=UTF-8&serverTimezone=GMT","root","13");
//与Mysql数据库建立连接
s = o.createStatement();
//创建Statement s 用于执行sql语句
String sql = String.format("update system set %s='%s' where %s = '%s'",x,c,y,b);
//执行insert语句插入数据
s.execute(sql);
//调用.execute()执行sql语句
System.out.println("更新成功,用户名为:"+b);
}catch(ClassNotFoundException e){//抛异常
e.printStackTrace();
}catch(SQLException e){//抛异常
e.printStackTrace();
}finally{//关闭数据库连接
//先关闭Statement
if(s!=null)
try{
s.close();
}catch(SQLException e){
e.printStackTrace();
}
//再关闭Connection
if(o!=null)
try{
o.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
删除数据
package Operation;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Delete {//饭卡充值系统-删除注销系统
public void system(int a,String b,int c,String d){
Connection o = null;//定义两个变量
Statement s = null;
try{//****
Class.forName("com.mysql.cj.jdbc.Driver");
//初始化驱动
//Mysql 8.0之后要加.cj
o = DriverManager.getConnection("jdbc:mysql://localhost:3306/meal?characterEncoding=UTF-8&serverTimezone=GMT","root","13");
//与Mysql数据库建立连接
s = o.createStatement();
//创建Statement s 用于执行sql语句
String sql = String.format("delete from system where ID = %d ",a);
//执行insert语句插入数据
s.execute(sql);
//调用.execute()执行sql语句
System.out.println("成功删除用户:"+b);
}catch(ClassNotFoundException e){//抛异常
e.printStackTrace();
}catch(SQLException e){//抛异常
e.printStackTrace();
}finally{//关闭数据库连接
//先关闭Statement
if(s!=null)
try{
s.close();
}catch(SQLException e){
e.printStackTrace();
}
//再关闭Connection
if(o!=null)
try{
o.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
转账服务
package Operation;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Transfer {//饭卡充值系统-转账系统
Connection o = null;
Statement s = null;
ResultSet rs = null;
PreparedStatement pr = null;
public void system1(int c,int b){
try{//****
Class.forName("com.mysql.cj.jdbc.Driver");
//初始化驱动
//Mysql 8.0之后要加.cj
o = DriverManager.getConnection("jdbc:mysql://localhost:3306/meal?characterEncoding=UTF-8&serverTimezone=GMT","root","13");
//与Mysql数据库建立连接
s = o.createStatement();
//创建Statement s 用于执行sql语句
String sql = String.format("update system set Money = Money-%d where ID = %d ",c,b);//y是ID,b是ID号,x是Money,c是金额
//执行insert语句插入数据
s.execute(sql);
System.out.println("更新成功,用户名为:"+b);
}catch(ClassNotFoundException e){//抛异常
e.printStackTrace();
}catch(SQLException e){//抛异常
e.printStackTrace();
}finally{//关闭数据库连接
if(rs!=null)
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
if(pr!=null)
try{
pr.close();
}catch(SQLException e){
e.printStackTrace();
}
//先关闭Statement
if(s!=null)
try{
s.close();
}catch(SQLException e){
e.printStackTrace();
}
//再关闭Connection
if(o!=null)
try{
o.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
public void system2(int c,int b){
Connection o = null;
Statement s = null;
try{//****
Class.forName("com.mysql.cj.jdbc.Driver");
//初始化驱动
//Mysql 8.0之后要加.cj
o = DriverManager.getConnection("jdbc:mysql://localhost:3306/meal?characterEncoding=UTF-8&serverTimezone=GMT","root","13");
//与Mysql数据库建立连接
s = o.createStatement();
//创建Statement s 用于执行sql语句
String sql = String.format("update system set Money = Money+%d where ID = %d ",c,b);//y是ID,b是ID号,x是Money,c是金额
//执行insert语句插入数据
s.execute(sql);
System.out.println("更新成功,用户名为:"+b);
}catch(ClassNotFoundException e){//抛异常
e.printStackTrace();
}catch(SQLException e){//抛异常
e.printStackTrace();
}finally{//关闭数据库连接
if(rs!=null)
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
if(pr!=null)
try{
pr.close();
}catch(SQLException e){
e.printStackTrace();
}
//先关闭Statement
if(s!=null)
try{
s.close();
}catch(SQLException e){
e.printStackTrace();
}
//再关闭Connection
if(o!=null)
try{
o.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}