我在学完java基础与MySQL,感觉学完这些东西之后又很快把哪些东西忘掉,于是就想用这两个东西做一个小小的项目,数据库版本使用MySQL5.1。MySQL的jar包为mysql-connector-java-5.1.37-bin.jar。
项目功能需求:
1)通过JDBC来展示数据库中用户的信息(id(序号),username(用户名),password(密码),banlance(存储金额))。
2)查询用户存储余额。
3)用户取款。
4)用户进行转载。
5)退出ATM机操作界面
package Atm; import java.io.IOException; import java.sql.*; import java.util.Scanner; import static java.lang.Thread.sleep; public class Main { public static void main(String[] args) throws SQLException, ClassNotFoundException { while(true) { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root"); PreparedStatement pstmt = null; System.out.println("欢迎使用ATM机,请插入银行卡:"); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("请你输入密码:"); Scanner sc = new Scanner(System.in); String code = sc.nextLine(); int count = 3; int Mark = 0; ResultSet rs = null; Mark = enter(code, pstmt, rs, conn); util.close(rs, pstmt, conn); while (Mark > 0) { // Class.forName("com.mysql.jdbc.Driver"); Connection conn1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root"); PreparedStatement pstmt1 = null; System.out.println("1 查询账户余额"); System.out.println("2 存款"); System.out.println("3 取款"); System.out.println("4 转账"); System.out.println("5 退出"); System.out.println("请选择所需要的功能:"); int select = sc.nextInt(); if (select == 1) { select1(pstmt1, rs, conn1, code); } else if (select == 2) { select2(pstmt1, rs, conn1, sc, code); } else if (select == 3) { select3(pstmt1, rs, conn1, sc, code); } else if (select == 4) { select4(pstmt1, rs, conn1, sc, code); } else { util.close(rs, pstmt1, conn1); System.out.println("银行卡退出,请取回你的卡"); // Runtime r = Runtime.getRuntime(); // try { // r.exec("CLS"); // } catch (IOException e) { // e.printStackTrace(); // } break; } } } } public static int enter(String code, PreparedStatement stmt, ResultSet rs, Connection conn) throws ClassNotFoundException, SQLException { int count = 3; int co = 0; while(count>0) { String sql = "select * from user"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { String passwold = rs.getString("password"); if (code.equals(passwold)) { co = 1; System.out.println("进入系统"); return co; } } if (co == 0) { System.out.println("密码错误,请重试"); }else{ break; } count -=1; } return 0; } public static void select1(PreparedStatement pstmt1, ResultSet rs, Connection conn1, String code) throws SQLException { String sql = "select * from user "; pstmt1 = conn1.prepareStatement(sql); // pstmt1.setString(1,code); rs = pstmt1.executeQuery(); while(rs.next()){ // String password = rs.getString(); String password = rs.getString("password"); if(code.equals(password)){ System.out.println("你的余额为"+ String.valueOf(rs.getDouble("balance"))); } } util.close(rs, pstmt1, conn1); } public static void select2(PreparedStatement pstmt1, ResultSet rs, Connection conn1, Scanner sc, String code) throws SQLException { String sql1 = "select * from user"; pstmt1 = conn1.prepareStatement(sql1); rs = pstmt1.executeQuery(); // int id = 0; while(rs.next()){ // String password = rs.getString(); String password = rs.getString("password"); // id = rs.getInt("id"); if(code.equals(password)){ break; // System.out.println("你的余额为"+ String.valueOf(rs.getDouble("balance"))); } } System.out.println("请输入存款金额:"); double cunRu = sc.nextDouble(); // rs.next(); double Zi = rs.getDouble("balance"); Zi = Zi + cunRu; String sql = "update user set balance = ? where password = ?"; pstmt1 = conn1.prepareStatement(sql); pstmt1.setDouble(1, Zi); pstmt1.setString(2, code); int cc = pstmt1.executeUpdate(); util.close(rs, pstmt1, conn1); } public static void select3(PreparedStatement pstmt1, ResultSet rs, Connection conn1, Scanner sc, String code) throws SQLException { String sql1 = "select * from user"; pstmt1 = conn1.prepareStatement(sql1); rs = pstmt1.executeQuery(); System.out.println("请输入取款金额:"); double quChu = sc.nextDouble(); // rs.next(); while(rs.next()){ // String password = rs.getString(); String password = rs.getString("password"); if(code.equals(password)){ break; // System.out.println("你的余额为"+ String.valueOf(rs.getDouble("balance"))); } } double Zi = rs.getDouble("balance"); Zi = Zi - quChu; String sql = "update user set balance = ? where password = ?"; pstmt1 = conn1.prepareStatement(sql); pstmt1.setDouble(1, Zi); pstmt1.setString(2, code); int cc = pstmt1.executeUpdate(); } public static void select4(PreparedStatement pstmt1, ResultSet rs, Connection conn1, Scanner sc, String code) throws SQLException { Scanner sc1 = new Scanner(System.in); System.out.println("输入需要转给的人:"); String Trans = sc1.nextLine(); String sql = "select * from user"; pstmt1 = conn1.prepareStatement(sql); rs = pstmt1.executeQuery(); double b1 = 0; double b3 = 0; while(rs.next()){ String username = rs.getString("username"); String password = rs.getString("password"); if(Trans.equals(username)){ b1 = rs.getDouble("balance"); } if(code.equals(password)){ b3 = rs.getDouble("balance"); } } System.out.println("输入转账的金额:"); Scanner sc2 = new Scanner(System.in); Double money = sc2.nextDouble(); Double b2; b2 = money + b1; b3 = b3 - money; String sql1 = "update user set balance = ? where username = ?"; pstmt1 = conn1.prepareStatement(sql1); pstmt1.setDouble(1, b2); pstmt1.setString(2, Trans); int cc = pstmt1.executeUpdate(); String sql2 = "update user set balance = ? where password = ?"; pstmt1 = conn1.prepareStatement(sql2); pstmt1.setDouble(1, b3); pstmt1.setString(2, code); int ccc = pstmt1.executeUpdate(); System.out.println(Trans + String.valueOf(money)); util.close(rs, pstmt1, conn1); } }
下面是另外一个类文件
package Atm; import java.io.IOException; import java.sql.*; import java.util.Scanner; import static java.lang.Thread.sleep; public class Main { public static void main(String[] args) throws SQLException, ClassNotFoundException { while(true) { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root"); PreparedStatement pstmt = null; System.out.println("欢迎使用ATM机,请插入银行卡:"); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("请你输入密码:"); Scanner sc = new Scanner(System.in); String code = sc.nextLine(); int count = 3; int Mark = 0; ResultSet rs = null; Mark = enter(code, pstmt, rs, conn); util.close(rs, pstmt, conn); while (Mark > 0) { // Class.forName("com.mysql.jdbc.Driver"); Connection conn1 = DriverManager.getConnection("jdbc:mysql://localhost:3306/atm", "root", "root"); PreparedStatement pstmt1 = null; System.out.println("1 查询账户余额"); System.out.println("2 存款"); System.out.println("3 取款"); System.out.println("4 转账"); System.out.println("5 退出"); System.out.println("请选择所需要的功能:"); int select = sc.nextInt(); if (select == 1) { select1(pstmt1, rs, conn1, code); } else if (select == 2) { select2(pstmt1, rs, conn1, sc, code); } else if (select == 3) { select3(pstmt1, rs, conn1, sc, code); } else if (select == 4) { select4(pstmt1, rs, conn1, sc, code); } else { util.close(rs, pstmt1, conn1); System.out.println("银行卡退出,请取回你的卡"); // Runtime r = Runtime.getRuntime(); // try { // r.exec("CLS"); // } catch (IOException e) { // e.printStackTrace(); // } break; } } } } public static int enter(String code, PreparedStatement stmt, ResultSet rs, Connection conn) throws ClassNotFoundException, SQLException { int count = 3; int co = 0; while(count>0) { String sql = "select * from user"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { String passwold = rs.getString("password"); if (code.equals(passwold)) { co = 1; System.out.println("进入系统"); return co; } } if (co == 0) { System.out.println("密码错误,请重试"); }else{ break; } count -=1; } return 0; } public static void select1(PreparedStatement pstmt1, ResultSet rs, Connection conn1, String code) throws SQLException { String sql = "select * from user "; pstmt1 = conn1.prepareStatement(sql); // pstmt1.setString(1,code); rs = pstmt1.executeQuery(); while(rs.next()){ // String password = rs.getString(); String password = rs.getString("password"); if(code.equals(password)){ System.out.println("你的余额为"+ String.valueOf(rs.getDouble("balance"))); } } util.close(rs, pstmt1, conn1); } public static void select2(PreparedStatement pstmt1, ResultSet rs, Connection conn1, Scanner sc, String code) throws SQLException { String sql1 = "select * from user"; pstmt1 = conn1.prepareStatement(sql1); rs = pstmt1.executeQuery(); // int id = 0; while(rs.next()){ // String password = rs.getString(); String password = rs.getString("password"); // id = rs.getInt("id"); if(code.equals(password)){ break; // System.out.println("你的余额为"+ String.valueOf(rs.getDouble("balance"))); } } System.out.println("请输入存款金额:"); double cunRu = sc.nextDouble(); // rs.next(); double Zi = rs.getDouble("balance"); Zi = Zi + cunRu; String sql = "update user set balance = ? where password = ?"; pstmt1 = conn1.prepareStatement(sql); pstmt1.setDouble(1, Zi); pstmt1.setString(2, code); int cc = pstmt1.executeUpdate(); util.close(rs, pstmt1, conn1); } public static void select3(PreparedStatement pstmt1, ResultSet rs, Connection conn1, Scanner sc, String code) throws SQLException { String sql1 = "select * from user"; pstmt1 = conn1.prepareStatement(sql1); rs = pstmt1.executeQuery(); System.out.println("请输入取款金额:"); double quChu = sc.nextDouble(); // rs.next(); while(rs.next()){ // String password = rs.getString(); String password = rs.getString("password"); if(code.equals(password)){ break; // System.out.println("你的余额为"+ String.valueOf(rs.getDouble("balance"))); } } double Zi = rs.getDouble("balance"); Zi = Zi - quChu; String sql = "update user set balance = ? where password = ?"; pstmt1 = conn1.prepareStatement(sql); pstmt1.setDouble(1, Zi); pstmt1.setString(2, code); int cc = pstmt1.executeUpdate(); } public static void select4(PreparedStatement pstmt1, ResultSet rs, Connection conn1, Scanner sc, String code) throws SQLException { Scanner sc1 = new Scanner(System.in); System.out.println("输入需要转给的人:"); String Trans = sc1.nextLine(); String sql = "select * from user"; pstmt1 = conn1.prepareStatement(sql); rs = pstmt1.executeQuery(); double b1 = 0; double b3 = 0; while(rs.next()){ String username = rs.getString("username"); String password = rs.getString("password"); if(Trans.equals(username)){ b1 = rs.getDouble("balance"); } if(code.equals(password)){ b3 = rs.getDouble("balance"); } } System.out.println("输入转账的金额:"); Scanner sc2 = new Scanner(System.in); Double money = sc2.nextDouble(); Double b2; b2 = money + b1; b3 = b3 - money; String sql1 = "update user set balance = ? where username = ?"; pstmt1 = conn1.prepareStatement(sql1); pstmt1.setDouble(1, b2); pstmt1.setString(2, Trans); int cc = pstmt1.executeUpdate(); String sql2 = "update user set balance = ? where password = ?"; pstmt1 = conn1.prepareStatement(sql2); pstmt1.setDouble(1, b3); pstmt1.setString(2, code); int ccc = pstmt1.executeUpdate(); System.out.println(Trans + String.valueOf(money)); util.close(rs, pstmt1, conn1); } }