源码:
首先链接数据库 :创建包com.hp.util 在包里创建类DBHepler 在包里链接数据库
package com.hp.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBHepler {
public static Connection getConn() {
Connection conn= null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/usmydb?serverTimezone=Asia/Shanghai";
conn= DriverManager.getConnection(url,"root","123456");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return conn;
}
//输出看一下是否链接
public static void main(String[] args) {
System.out.println(getConn());
}
}
运行效果:
输出这个则数据库链接成功
然后创建包com.hp.test 在包里创建类Test 在test里面实现各种方法
package com.hp.test;
import com.hp.util.DBHepler;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Test {
public void Meu(){
System.out.println("**************************");
System.out.println("*******欢迎来到银行********");
System.out.println("***1、用户新增***");
System.out.println("***2、修改用户***");
System.out.println("***3、存钱***");
System.out.println("***4、取钱***");
System.out.println("***5、退卡***");
System.out.println("**************************");
System.out.println("请选择您要的操作内容:");
Scanner sc=new Scanner(System.in);
String str=sc.next();
char c=str.charAt(0);
switch (c){
case '1':
add();
break;
case '2':
update();
break;
case '3':
deposit();
break;
case '4':
drawMonry();
break;
case '5':
withdraw();
break;
}
}
//展示全部
public void showAll(){
Connection conn= DBHepler.getConn();
PreparedStatement ps=null;
String sql="select * from bank";
try {
ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while (rs.next()) {
System.out.println("用户id:"+rs.getInt(1)+" "+"用户姓名:"+rs.getString(2)+" "
+"用户密码:"+rs.getString(3)+" "+"用户余额"+rs.getDouble(4)+" ");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//增加用户
public void add(){
System.out.println("********用户新增********");
Scanner sc=new Scanner(System.in);
System.out.println("请输入添加用户账号");
String name=sc.next();
System.out.println("请输入添加用户密码");
String password=sc.next();
System.out.println("请输入添加用户余额");
double banlace=sc.nextDouble();
Connection conn=DBHepler.getConn();
String sql="insert into bank values(null,?,?,?)";
PreparedStatement ps=null;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ps.setDouble(3, banlace);
int i=ps.executeUpdate();
if (i>0){
System.out.println("添加成功");
Meu();
}else{
System.out.println("添加成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//修改用户
public void update(){
System.out.println("********用户修改********");
System.out.println("************修改用户信息**************");
Scanner sc= new Scanner(System.in);
System.out.println("输入修改的用户id");
int id=sc.nextInt();
System.out.println("输入修改用户账号");
String name=sc.next();
System.out.println("输入修改用户密码");
String password=sc.next();
System.out.println("输入修改用户金额");
double banlance=sc.nextDouble();
Connection conn=DBHepler.getConn();
String sql="update bank set name=?,password=?,balance=? where id=?";
PreparedStatement ps=null;
try {
ps=conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, password);
ps.setDouble(3, banlance);
ps.setInt(4, id);
int i=ps.executeUpdate();
if(i>0){
System.out.println("修改成功");
Meu();
}else {
System.out.println("修改失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
conn.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//用户存钱
public void deposit(){
System.out.println("*******用户存钱********");
Scanner sc=new Scanner(System.in);
System.out.println("请输入存钱用户的姓名:");
String name=sc.next();
System.out.println("请输入存钱用户的密码:");
String password=sc.next();
System.out.println("请输入存钱用户的金额:");
double balance=sc.nextDouble();
Connection connn=DBHepler.getConn();
String sql="update bank set balance=balance+? where name=? and password=?";
PreparedStatement ps=null;
try {
ps= connn.prepareStatement(sql);
ps.setDouble(1, balance);
ps.setString(2, name);
ps.setString(3, password);
int i=ps.executeUpdate();
if (i>0) {
System.out.println("存入成功,存入金额为:"+balance);
Meu();
}else{
System.out.println("存入失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
connn.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//用户取钱
public void drawMonry(){
System.out.println("*******用户取钱********");
Scanner sc=new Scanner(System.in);
System.out.println("请输入取钱用户的姓名:");
String name=sc.next();
System.out.println("请输入取钱用户的密码:");
String password=sc.next();
System.out.println("请输入取钱用户的金额:");
double balance=sc.nextDouble();
Connection connn=DBHepler.getConn();
String sql="update bank set balance=balance-? where name=? and password=?";
PreparedStatement ps=null;
try {
ps= connn.prepareStatement(sql);
ps.setDouble(1, balance);
ps.setString(2, name);
ps.setString(3, password);
int i=ps.executeUpdate();
if (i>0) {
System.out.println("取出成功,取出金额为:"+balance);
Meu();
}else{
System.out.println("取出失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
connn.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void withdraw(){
System.out.println("退出成功,请收好您的卡");
System.exit(0);
}
}
最后创建包 com.hp.controller 在包里面创建测试类bank
package com.hp.controller;
import com.hp.test.Test;
public class Bank {
public static void main(String[] args) {
Test t=new Test();
t.Meu();
}
}
最后运行效果: