时间:2021-07-01 10:21:17 帮助过:35人阅读
查询功能:
//查询方法 public void chaxun(){ while(true){ try{ Scanner sc=new Scanner(System.in); System.out.println("请输入你要查询的卡号:"); String s=sc.nextLine(); // 访问数据库 //1、加载驱动:加载数据库提供的驱动类 Class.forName("oracle.jdbc.driver.OracleDriver"); //2、获取数据库连接,通过java的驱动管理器 //url-数据库地址,不同的数据库写法不一样;user-用户名;password-密码 Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl ","test01", "123456"); String sql="select * from bank where ba_card=? "; PreparedStatement ps=conn.prepareStatement(sql); ps.setString(1, s); ResultSet rs=ps.executeQuery(); if(rs.next()) { String pw= rs.getString("ba_password"); Scanner sc1=new Scanner(System.in); System.out.println("请输入卡号密码:"); String s1=sc.nextLine(); if(pw.equals(s1)) { System.out.println("你查询的账号信息为:"); System.out.println(); System.out.println("******************************"); System.out.println(); System.out.println("姓名 身份证号 卡号 金额"); System.out.println(); System.out.println(rs.getString("ba_name")+" "+rs.getString("ba_shenfen")+" "+rs.getString("ba_card")+" "+rs.getString("ba_balance")); break; } else { int i=0; i++; if(i==3) { System.out.println("密码输入错误超过3次"); break; } else { System.out.println("密码输入有误"); } } rs.close(); ps.close(); conn.close(); break; } else { System.out.println("卡号输入有误请确认"); } }catch (Exception e) { System.out.println("输入有误"); } break; } }
3.存款功能
public void cun() { while(true) { Scanner sc=new Scanner(System.in); System.out.println("请输入你要查询的卡号:"); String s=sc.nextLine(); // 访问数据库 //1、加载驱动:加载数据库提供的驱动类 try { Class.forName("oracle.jdbc.driver.OracleDriver"); //2、获取数据库连接,通过java的驱动管理器 //url-数据库地址,不同的数据库写法不一样;user-用户名;password-密码 Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl ","test01", "123456"); String sql="select * from bank where ba_card=? "; PreparedStatement ps=conn.prepareStatement(sql); ps.setString(1, s); ResultSet rs=ps.executeQuery(); if(rs.next()) { String pw= rs.getString("ba_password"); Scanner sc1=new Scanner(System.in); System.out.println("请输入卡号密码:"); String s1=sc.nextLine(); if(pw.equals(s1)) { System.out.println("你查询的账号信息为:"); System.out.println(); System.out.println("******************************"); System.out.println(); System.out.println("姓名 身份证号 卡号 金额"); System.out.println(); System.out.println(rs.getString("ba_name")+" "+rs.getString("ba_shenfen")+" "+rs.getString("ba_card")+" "+rs.getString("ba_balance")); } else { int i=0; i++; if(i==3) { System.out.println("密码输入错误超过3次"); break; } else { System.out.println("密码输入有误"); } } } else { System.out.println("卡号输入有误请确认"); } while(true) { Scanner sc2=new Scanner(System.in); System.out.println("请输入你要存入的金额:"); double cun=sc2.nextDouble(); // 实现存款方法 if(cun<0) { System.out.println("你输入的数据无效,系统将自动退出!谢谢使用"); } else { try { Class.forName("oracle.jdbc.driver.OracleDriver"); //2、获取数据库连接,通过java的驱动管理器 //url-数据库地址,不同的数据库写法不一样;user-用户名;password-密码 Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl ","test01", "123456"); Statement sm=con.createStatement(); balance=rs.getDouble("ba_balance"); balance+=cun; String sql1="update bank set ba_balance=‘"+balance+"‘where ba_card=‘"+s+"‘"; sm.executeQuery(sql1); Statement rl=con.createStatement(); String sql2="select ba_balance from bank where ba_card=‘"+s+"‘"; ResultSet rt=rl.executeQuery(sql2); System.out.println("存款成功,剩余金额"+rs.getDouble("ba_balance")); sm.close(); con.close(); conn.close(); rs.close(); ps.close(); break; } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); continue; } } } } catch (Exception e1) { // TODO 自动生成的 catch 块 e1.printStackTrace(); continue; } } }
4.取款功能
public void qu() { try{ while(true) { Scanner sc=new Scanner(System.in); System.out.println("请输入你要查询的卡号:"); String s=sc.nextLine(); // 访问数据库 //1、加载驱动:加载数据库提供的驱动类 Class.forName("oracle.jdbc.driver.OracleDriver"); //2、获取数据库连接,通过java的驱动管理器 //url-数据库地址,不同的数据库写法不一样;user-用户名;password-密码 Connection conn = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl ","test01", "123456"); String sql="select * from bank where ba_card=? "; PreparedStatement ps=conn.prepareStatement(sql); ps.setString(1, s); ResultSet rs=ps.executeQuery(); if(rs.next()) { String pw= rs.getString("ba_password"); Scanner sc1=new Scanner(System.in); System.out.println("请输入卡号密码:"); String s1=sc.nextLine(); if(pw.equals(s1)) { System.out.println("你查询的账号信息为:"); System.out.println(); System.out.println("******************************"); System.out.println(); System.out.println("姓名 身份证号 卡号 金额"); System.out.println(); System.out.println(rs.getString("ba_name")+" "+rs.getString("ba_shenfen")+" "+rs.getString("ba_card")+" "+rs.getString("ba_balance")); } else { int i=0; i++; if(i==3) { System.out.println("密码输入错误超过3次"); break; } else { System.out.println("密码输入有误"); } } } else { System.out.println("卡号输入有误请确认"); } Scanner sc1=new Scanner(System.in); System.out.println("请输入你要取款的金额:"); double qu=sc1.nextDouble(); Class.forName("oracle.jdbc.driver.OracleDriver"); //2、获取数据库连接,通过java的驱动管理器 //url-数据库地址,不同的数据库写法不一样;user-用户名;password-密码 Connection con = DriverManager.getConnection( "jdbc:oracle:thin:@localhost:1521:orcl ","test01", "123456"); Statement sm=con.createStatement(); balance=rs.getDouble("ba_balance"); // 实现取款方法 if(qu<balance) { balance-=qu; String sql1="update bank set ba_balance=‘"+balance+"‘where ba_card=‘"+s+"‘"; sm.executeQuery(sql1); Statement rl=con.createStatement(); String sql2="select ba_balance from bank where ba_card=‘"+s+"‘"; ResultSet rt=rl.executeQuery(sql2); System.out.println("取款成功,剩余金额"+rt.getDouble("ba_balance")); sm.close(); con.close(); conn.close(); rs.close(); ps.close(); } else if(qu<0) { System.out.println("你输入的数据无效,系统将自动退出!谢谢使用");; } else { System.out.println("余额不足"); } } } catch (Exception e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } }
结果:
银行系统---数据库连接修改
标签:upd bank builder als app car 地址 src 管理