1 package per.sww.page; 2 3 import java.util.ArrayList; 4 5 import per.sww.dao.UserDao; 6 import per.sww.entity.User; 7 import per.sww.tools.ScannerChoice; 8 9 public class MainPage { 10 11 public static void main(String[] args) { 12 MainPage.mainpage(); 13 } 14 15 /** 16 * 主界面 17 */ 18 public static void mainpage(){ 19 System.out.println("▂▂▂▂▂▂▂▂▂▂欢迎使用自助银行系统▂▂▂▂▂▂▂▂▂▂"); 20 System.out.println("1.账号登录"); 21 System.out.println("0.退出系统"); 22 System.out.println("▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂"); 23 24 do { 25 String choice=ScannerChoice.ScannerinfoString(); 26 String regex="[0-1]"; 27 if(choice.matches(regex)){ 28 int info=Integer.parseInt(choice); 29 switch (info) { 30 case 0: 31 System.out.println("谢谢使用,再见!"); 32 System.exit(1); 33 break; 34 case 1: 35 int Loginfrequency=3; 36 while (Loginfrequency!=0) { 37 System.out.println("\t\t请输入账号:"); 38 String useName=ScannerChoice.ScannerinfoString(); 39 System.out.println("\t\t请输入密码:"); 40 String usePwd=ScannerChoice.ScannerinfoString(); 41 ArrayList<User> usersList=new UserDao().checkLogin(useName); 42 if(usersList == null || usersList.size()==0){ 43 System.err.println("\t!!用户名输入有误!!\n"); 44 System.out.println("\n剩余登陆次数:" + Loginfrequency); 45 }else { 46 User user=usersList.get(0); 47 if(usePwd.equals(user.getUserPassword())){ 48 System.out.println("\t ■ ■ ■ ■ ■ ■ ■ ■ 账户成功登陆 ■ ■ ■ ■ ■ ■ ■ ■ "); 49 LoginPage(user.getUserId()); 50 }else { 51 System.err.println("\t!!密码错误!!\n"); 52 System.out.println("\n剩余登陆次数:" + Loginfrequency); 53 } 54 } 55 } 56 System.out.println("■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"); 57 System.err.println("\t!!您已被开除!"); 58 System.exit(1); 59 break; 60 case 2: 61 System.out.println("■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"); 62 System.err.println("\t\t您已经退出系统!"); 63 System.exit(-1); 64 break; 65 default: 66 break; 67 } 68 } 69 } while (true); 70 } 71 72 73 /** 74 * 登录成功 75 * @param userId 76 */ 77 public static void LoginPage(int userId) { 78 System.out.println("▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂"); 79 System.out.println("\t\t1.查询余额"); 80 System.out.println("\t\t2.存取款"); 81 System.out.println("\t\t3.删除记录"); 82 System.out.println("\t\t0.返回上级"); 83 System.out.println("▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂"); 84 do { 85 String choice=ScannerChoice.ScannerinfoString(); 86 String regex="[0-4]"; 87 if(choice.matches(regex)){ 88 int info=Integer.parseInt(choice); 89 switch (info) { 90 case 0: 91 mainpage(); 92 break; 93 case 1: 94 95 break; 96 case 2: 97 UserPage.CreatMoney(); 98 break; 99 case 3: 100 101 break; 102 default: 103 break; 104 } 105 } 106 System.err.println("\t\t输入有误!"); 107 } while (true); 108 } 109 }
1 package per.sww.tools; 2 3 import java.util.Scanner; 4 5 public class ScannerChoice { 6 7 /** 8 * 记录键盘输入的信息 9 * @return 10 */ 11 public static String ScannerinfoString(){ 12 Scanner sc=new Scanner(System.in); 13 System.out.println("请输入:"); 14 return sc.next(); 15 } 16 17 /** 18 * 小数点后两位 19 */ 20 public static double ScannerInfo(){ 21 double num=0.00; 22 do { 23 String info=ScannerinfoString(); 24 String regex = "(([1-9][0-9]*)\\.([0-9]{2}))|[0]\\.([0-9]{2})"; 25 if(info.matches(regex)){ 26 num=Double.parseDouble(info); 27 }else{ 28 System.out.println("必须为小数点后两位!"); 29 continue; 30 } 31 break; 32 } while (true); 33 return num; 34 } 35 }
package per.sww.entity; public final class User { private int userId; private String userName; private String userPassword; private double userMoney; public User() { } public User(int userId,String userName) { this.userId=userId; this.userName=userName; } public User(int userId,double userMoney) { this.userId=userId; this.userMoney=userMoney; } public User(String userName,String userPassword) { this.userName=userName; this.userPassword=userPassword; } public User(int userId,String userPassword,double userMoney) { this.userId=userId; this.userPassword=userPassword; this.userMoney=userMoney; } public User(int userId,String userName,String userPassword,double userMoney) { this.userId=userId; this.userName=userName; this.userPassword=userPassword; this.userMoney=userMoney; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } public double getUserMoney() { return userMoney; } public void setUserMoney(double userMoney) { this.userMoney = userMoney; } }
1 package per.sww.page; 2 3 import per.sww.entity.User; 4 import per.sww.tools.ScannerChoice; 5 6 public class UserPage { 7 8 public static void CreatMoney() { 9 System.out.println("存款金额:"); 10 double money=ScannerChoice.ScannerInfo(); 11 User userMoney=new User(); 12 } 13 }
1 package per.sww.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import per.sww.db.DbClose; 9 import per.sww.db.DbConn; 10 import per.sww.entity.User; 11 import per.sww.tools.ScannerChoice; 12 13 public class UserDao { 14 Connection conn=null; 15 PreparedStatement ps=null; 16 ResultSet rs=null; 17 18 /** 19 * 验证用户登录 20 * @param useName 21 * @return 22 */ 23 24 public ArrayList<User> checkLogin(String useName) { 25 ArrayList<User> users=new ArrayList<User>(); 26 conn=DbConn.getConn(); 27 String sql="SELECT USERID,USERPASSWORD,USERMONEY FROM USERS WHERE USERNAME=?"; 28 try { 29 ps=conn.prepareStatement(sql); 30 ps.setString(1, useName); 31 rs=ps.executeQuery(); 32 while (rs.next()) { 33 int userId=rs.getInt("userId"); 34 String userPassword=rs.getString("userPassword"); 35 double userMoney=rs.getDouble("userMoney"); 36 37 User user=new User(userId, userPassword, userMoney); 38 users.add(user); 39 } 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 }finally { 43 DbClose.whole(ps, conn, rs); 44 } 45 return users; 46 } 47 48 49 /** 50 * 查询余额 51 */ 52 public double SelectMoney(User user) { 53 double semoney=0.00; 54 conn=DbConn.getConn(); 55 String sql="SELECT USERMONEY FROM USERS WHERE USERID=?"; 56 try { 57 ps=conn.prepareStatement(sql); 58 ps.setInt(1, user.getUserId()); 59 rs=ps.executeQuery(); 60 while (rs.next()) { 61 semoney=rs.getDouble("userMoney"); 62 } 63 } catch (SQLException e) { 64 e.printStackTrace(); 65 }finally { 66 DbClose.whole(ps, conn, rs); 67 } 68 return semoney; 69 } 70 71 72 73 /** 74 * 存款 75 * @param logined_user 76 */ 77 public static void CreatMoney(User logined_user) { 78 Connection conn=null; 79 PreparedStatement ps=null; 80 ResultSet rs=null; 81 82 double onemoney=0.00; 83 double allmoney=0.00; 84 85 System.out.println("请输入要存入的金额:"); 86 double money=ScannerChoice.ScannerInfo(); 87 if(money<=0){ 88 System.out.println("存入的金额必须大于0!"); 89 CreatMoney(logined_user); 90 } 91 conn=DbConn.getConn(); 92 String sql="SELECT USERID,USERPASSWORD,USERMONEY FROM USERS WHERE USERNAME=?"; 93 try { 94 ps=conn.prepareStatement(sql); 95 ps.setString(1,logined_user.getUserName()); 96 rs=ps.executeQuery(); 97 while (rs.next()) { 98 onemoney=rs.getDouble("userMoney"); 99 } 100 allmoney=onemoney+money; 101 } catch (SQLException e) { 102 e.printStackTrace(); 103 }finally { 104 DbClose.whole(ps, conn, rs); 105 } 106 System.out.println("\t\t操作完成"); 107 System.out.println("您的剩余存款为:"+allmoney); 108 } 109 110 111 /** 112 * 取款 113 * @param logined_user 114 */ 115 public static void TakeMoney(User logined_user) { 116 Connection conn=null; 117 PreparedStatement ps=null; 118 ResultSet rs=null; 119 120 double onemoney=0.00; 121 double allmoney=0.00; 122 123 System.out.println("请输入要取出的金额:"); 124 double money=ScannerChoice.ScannerInfo(); 125 if(money<=0){ 126 System.out.println("取出的金额必须大于0!"); 127 CreatMoney(logined_user); 128 } 129 130 conn=DbConn.getConn(); 131 String msql="UPDATE USERS SET USERMONEY = ? WHERE USERNAME=?"; 132 try { 133 ps=conn.prepareStatement(msql); 134 ps.setString(1, logined_user.getUserName()); 135 rs=ps.executeQuery(); 136 while (rs.next()) { 137 onemoney=rs.getDouble("userMoney"); 138 } 139 allmoney=onemoney-money; 140 } catch (SQLException e) { 141 e.printStackTrace(); 142 }finally { 143 DbClose.whole(ps, conn, rs); 144 } 145 System.out.println("\t\t操作完成"); 146 System.out.println("您的剩余存款为:"+allmoney); 147 } 148 149 150 151 }
dbconn和close就不发了,现在运行后是这样子的
java.sql.SQLException: Can not issue data manipulation statements with executeQuery(). 操作完成 您的剩余存款为:0.0 请输入: at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918) at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:501) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2150) at per.sww.dao.UserDao.TakeMoney(UserDao.java:135) at per.sww.page.MainPage.LoginPage(MainPage.java:105) at per.sww.page.MainPage.mainpage(MainPage.java:53) at per.sww.page.MainPage.main(MainPage.java:14)
(⊙﹏⊙)b,UserDao里的Sql粘错了
String msql = "UPDATE SET USERMONEY = ? FROM USERS WHERE USERNAME = ?";
是这个,不过错误是一样的= =
@眉间剪水泛千愁:
应该是分开的。是根据你的方法进行写的,1,2,3,4,应该就是你要的功能,我没有实现里面具体的逻辑代码
@wakasann: 看看我做的对不对,我塞不到数据库里面的哇
1 package per.sww.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import per.sww.db.DbClose; 9 import per.sww.db.DbConn; 10 import per.sww.entity.User; 11 import per.sww.tools.ScannerChoice; 12 13 public class UserDao { 14 Connection conn=null; 15 PreparedStatement ps=null; 16 ResultSet rs=null; 17 18 /** 19 * 验证用户登录 20 * @param useName 21 * @return 22 */ 23 24 public ArrayList<User> checkLogin(String useName) { 25 ArrayList<User> users=new ArrayList<User>(); 26 conn=DbConn.getConn(); 27 String sql="SELECT USERID,USERPASSWORD,USERMONEY FROM USERS WHERE USERNAME=?"; 28 try { 29 ps=conn.prepareStatement(sql); 30 ps.setString(1, useName); 31 rs=ps.executeQuery(); 32 while (rs.next()) { 33 int userId=rs.getInt("userId"); 34 String userPassword=rs.getString("userPassword"); 35 double userMoney=rs.getDouble("userMoney"); 36 37 User user=new User(userId, userPassword, userMoney); 38 users.add(user); 39 } 40 } catch (SQLException e) { 41 e.printStackTrace(); 42 }finally { 43 DbClose.whole(ps, conn, rs); 44 } 45 return users; 46 } 47 48 49 /** 50 * 查询余额 51 */ 52 public double SelectMoney(User user) { 53 double semoney=0.00; 54 conn=DbConn.getConn(); 55 String sql="SELECT USERMONEY FROM USERS WHERE USERID=?"; 56 try { 57 ps=conn.prepareStatement(sql); 58 ps.setInt(1, user.getUserId()); 59 rs=ps.executeQuery(); 60 while (rs.next()) { 61 semoney=rs.getDouble("userMoney"); 62 } 63 } catch (SQLException e) { 64 e.printStackTrace(); 65 }finally { 66 DbClose.whole(ps, conn, rs); 67 } 68 return semoney; 69 } 70 71 72 73 /** 74 * 存款 75 * @param logined_user 76 */ 77 public static void CreatMoney(User logined_user) { 78 Connection conn=null; 79 PreparedStatement ps=null; 80 ResultSet rs=null; 81 82 double onemoney=0.00; 83 double allmoney=0.00; 84 85 System.out.println("请输入要存入的金额:"); 86 double money=ScannerChoice.ScannerInfo(); 87 if(money<=0){ 88 System.out.println("存入的金额必须大于0!"); 89 CreatMoney(logined_user); 90 } 91 conn=DbConn.getConn(); 92 String sql="SELECT USERID,USERPASSWORD,USERMONEY FROM USERS WHERE USERNAME=?"; 93 try { 94 ps=conn.prepareStatement(sql); 95 ps.setString(1,logined_user.getUserName()); 96 rs=ps.executeQuery(); 97 while (rs.next()) { 98 onemoney=rs.getDouble("userMoney"); 99 } 100 allmoney=onemoney+money; 101 } catch (SQLException e) { 102 e.printStackTrace(); 103 }finally { 104 DbClose.whole(ps, conn, rs); 105 } 106 System.out.println("\t\t操作完成"); 107 System.out.println("您的剩余存款为:"+allmoney); 108 } 109 110 111 /** 112 * 取款 113 * @param logined_user 114 */ 115 public static void TakeMoney(User logined_user) { 116 Connection conn=null; 117 PreparedStatement ps=null; 118 ResultSet rs=null; 119 120 double onemoney=0.00; 121 double allmoney=0.00; 122 123 System.out.println("请输入要取出的金额:"); 124 double money=ScannerChoice.ScannerInfo(); 125 if(money<=0){ 126 System.out.println("取出的金额必须大于0!"); 127 CreatMoney(logined_user); 128 } 129 130 conn=DbConn.getConn(); 131 String msql="UPDATE USERS SET USERMONEY = ? WHERE USERNAME=?"; 132 try { 133 ps=conn.prepareStatement(msql); 134 ps.setString(1, logined_user.getUserName()); 135 rs=ps.executeQuery(); 136 while (rs.next()) { 137 onemoney=rs.getDouble("userMoney"); 138 } 139 allmoney=onemoney-money; 140 } catch (SQLException e) { 141 e.printStackTrace(); 142 }finally { 143 DbClose.whole(ps, conn, rs); 144 } 145 System.out.println("\t\t操作完成"); 146 System.out.println("您的剩余存款为:"+allmoney); 147 } 148 149 150 151 }
@wakasann: 取款的时候回报这个错,呐给你看
1 java.sql.SQLException: Can not issue data manipulation statements with executeQuery(). 2 操作完成 3 您的剩余存款为:0.0 4 请输入: 5 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1084) 6 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987) 7 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:973) 8 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:918) 9 at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:501) 10 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2150) 11 at per.sww.dao.UserDao.TakeMoney(UserDao.java:135) 12 at per.sww.page.MainPage.LoginPage(MainPage.java:105) 13 at per.sww.page.MainPage.mainpage(MainPage.java:53) 14 at per.sww.page.MainPage.main(MainPage.java:14)
@wakasann: 这一块它没进去!
while (rs.next()) {
onemoney=rs.getDouble("userMoney");
}
@眉间剪水泛千愁: jconsole
背景
红色是删除的
绿色是添加的
主要看UserDao.java
------
还有你操作为何要用username,.为何不用userId;建议你用userId进行操作用户资料,userId是主键,有索引的,数据库能更快定位到你要操作的数据。
@wakasann: Type mismatch: cannot convert from void to double
报这个错
@眉间剪水泛千愁: 什么错误,我运行没有Exception....
@眉间剪水泛千愁: 哪个方法?
@wakasann: copy过去好了- -,谢谢哥哥
@wakasann: 哦对了,取款这里,运行完输入金额报错,是这个
Can not issue data manipulation statements with executeQuery().
@眉间剪水泛千愁: 取款应该是先查询
Can not issue data manipulation statements with executeQuery().
大概是说:不能使用executeQuery() 声明进行数据操作(UPDATE,DELETE,INSERT)
@wakasann: 是的我,也反应过来啦
又是你~~~~
1.do{}while(true)你这里死循环了,break只是跳出switch
2.UserPage.CreatMoney();没有返回返回金额,无意义。
@眉间剪水泛千愁: 代码里面找找!
do{
//do something
System.err.println("\t\t输入有误!");
}
while(true)
1. println前面所有代码执行除非抛出异常,不然肯定会执行。
2. 如果想跳出while循环,可以声明变量flag=true,
do{
在需要跳出while循环的时候设置flag=false;
}
while(flag)
@waiter: Can not issue data manipulation statements with executeQuery().这个错误怎么解决
@眉间剪水泛千愁: String msql="UPDATE USERS SET USERMONEY = ? WHERE USERNAME=?"; 1。你有两个参数。。
2. 你这是更新语句,下面怎么可能取得出来onemoney=rs.getDouble("userMoney");
3. 逻辑混乱啊。。
@waiter: 因为要累加啊!我有写累加的,
好努力。。。
因为你们都不教我,好不容易遇到一个帅哥肯教我
@眉间剪水泛千愁: 谁不教你,这么坏
@眉间剪水泛千愁: @眉间剪水泛千愁:如果是一次登录失败禁用就简单了,登录失败把那一列update为1。如果是登录3次失败禁用的话,可以新建一个表,为用户登录失败日志表,登录失败后添加登录失败信息,查询该用户在登录失败日志表中有几条记录,为3条就禁用。如果登录成功,根据用户id删除登录失败用户表中的用户信息。而且你在平常登录的时候要多加一个条件,用户是否被禁用