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 public static void mainpage(){ 17 System.out.println("▂▂▂▂▂▂▂▂▂▂欢迎使用自助银行系统▂▂▂▂▂▂▂▂▂▂"); 18 System.out.println("1.账号登录"); 19 System.out.println("0.退出系统"); 20 System.out.println("▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂"); 21 22 do { 23 String choice=ScannerChoice.ScannerinfoString(); 24 String regex="[0-1]"; 25 if(choice.matches(regex)){ 26 int info=Integer.parseInt(choice); 27 switch (info) { 28 case 0: 29 System.out.println("谢谢使用,再见!"); 30 System.exit(1); 31 break; 32 case 1: 33 int Loginfrequency=3; 34 while (Loginfrequency!=0) { 35 System.out.println("请输入账号:"); 36 String useName=ScannerChoice.ScannerinfoString(); 37 System.out.println("请输入密码:"); 38 String usePwd=ScannerChoice.ScannerinfoString(); 39 ArrayList<User> usersList=new UserDao().checkLogin(useName); 40 if(usersList == null || usersList.size()==0){ 41 System.err.println("\t!!用户名输入有误!!\n"); 42 System.out.println("\n剩余登陆次数:" + Loginfrequency); 43 }else { 44 User user=usersList.get(0); 45 if(usePwd.equals(user.getUserPassword())){ 46 System.out.println("\t ■ ■ ■ ■ ■ ■ ■ ■ 账户成功登陆 ■ ■ ■ ■ ■ ■ ■ ■ "); 47 }else { 48 System.err.println("\t!!密码错误!!\n"); 49 System.out.println("\n剩余登陆次数:" + Loginfrequency); 50 } 51 } 52 } 53 System.out.println("■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■"); 54 System.err.println("\t!!您已被开除!"); 55 System.exit(1); 56 break; 57 default: 58 break; 59 } 60 } 61 } while (true); 62 } 63 64 65 66 }
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 }
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 9 import per.sww.db.DbClose; 10 import per.sww.db.DbConn; 11 import per.sww.entity.User; 12 13 public class UserDao { 14 Connection conn=null; 15 PreparedStatement ps=null; 16 ResultSet rs=null; 17 18 public ArrayList<User> checkLogin(String useName) { 19 ArrayList<User> users=new ArrayList<User>(); 20 conn=DbConn.getConn(); 21 String sql="SELECT USERID,USERPASSWORD,USERMONEY FROM SALESMAN WHERE SNAME=?"; 22 try { 23 ps=conn.prepareStatement(sql); 24 ps.setString(1, useName); 25 rs=ps.executeQuery(); 26 while (rs.next()) { 27 int userId=rs.getInt("userId"); 28 String userPassword=rs.getString("userPassword"); 29 double userMoney=rs.getDouble("userMoney"); 30 31 User user=new User(userId, userPassword, userMoney); 32 users.add(user); 33 } 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 }finally { 37 DbClose.whole(ps, conn, rs); 38 } 39 return users; 40 } 41 }
1 package per.sww.entity; 2 3 public final class User { 4 5 private int userId; 6 private String userName; 7 private String userPassword; 8 private double userMoney; 9 10 11 public User(int userId,String userName) { 12 this.userId=userId; 13 this.userName=userName; 14 } 15 public User(int userId,double userMoney) { 16 this.userId=userId; 17 this.userMoney=userMoney; 18 } 19 public User(String userName,String userPassword) { 20 this.userName=userName; 21 this.userPassword=userPassword; 22 } 23 public User(int userId,String userPassword,double userMoney) { 24 this.userId=userId; 25 this.userPassword=userPassword; 26 this.userMoney=userMoney; 27 } 28 29 public User(int userId,String userName,String userPassword,double userMoney) { 30 this.userId=userId; 31 this.userName=userName; 32 this.userPassword=userPassword; 33 this.userMoney=userMoney; 34 } 35 36 37 public int getUserId() { 38 return userId; 39 } 40 public void setUserId(int userId) { 41 this.userId = userId; 42 } 43 public String getUserName() { 44 return userName; 45 } 46 public void setUserName(String userName) { 47 this.userName = userName; 48 } 49 public String getUserPassword() { 50 return userPassword; 51 } 52 public void setUserPassword(String userPassword) { 53 this.userPassword = userPassword; 54 } 55 public double getUserMoney() { 56 return userMoney; 57 } 58 public void setUserMoney(double userMoney) { 59 this.userMoney = userMoney; 60 } 61 }
1 package per.sww.db; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 7 public class DbConn { 8 9 public static Connection getConn(){ 10 Connection conn=null; 11 String user="root"; 12 String passwd="root"; 13 String url="jdbc:mysql://localhost:3306/account?useUnicode=true&characterEncoding=utf-8"; 14 try { 15 Class.forName("com.mysql.jdbc.Driver"); 16 conn=DriverManager.getConnection(url, user, passwd); 17 } catch (SQLException e) { 18 e.printStackTrace(); 19 } catch (ClassNotFoundException e) { 20 e.printStackTrace(); 21 } 22 return conn; 23 } 24 }
1 package per.sww.db; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 public class DbClose { 9 10 public static Connection addClose(PreparedStatement ps,Connection conn){ 11 try { 12 if(ps!=null){ 13 ps.close(); 14 } 15 } catch (SQLException e) { 16 e.printStackTrace(); 17 } 18 19 try { 20 if(conn!=null){ 21 conn.close(); 22 } 23 } catch (SQLException e) { 24 e.printStackTrace(); 25 } 26 return conn; 27 } 28 29 public static Connection whole(PreparedStatement ps,Connection conn,ResultSet rs){ 30 try { 31 if(ps!=null){ 32 ps.close(); 33 } 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 } 37 38 try { 39 if(conn!=null){ 40 conn.close(); 41 } 42 } catch (SQLException e) { 43 e.printStackTrace(); 44 } 45 46 try { 47 if(rs!=null){ 48 rs.close(); 49 } 50 } catch (SQLException e) { 51 e.printStackTrace(); 52 } 53 return conn; 54 } 55 }
1 ▂▂▂▂▂▂▂▂▂▂欢迎使用自助银行系统▂▂▂▂▂▂▂▂▂▂ 2 1.账号登录 3 0.退出系统 4 ▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ 5 请输入: 6 1 7 请输入账号: 8 请输入: 9 admin 10 请输入密码: 11 请输入: 12 123456 13 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 14 at java.net.URLClassLoader$1.run(Unknown Source) 15 at java.net.URLClassLoader$1.run(Unknown Source) 16 at java.security.AccessController.doPrivileged(Native Method) 17 at java.net.URLClassLoader.findClass(Unknown Source) 18 at java.lang.ClassLoader.loadClass(Unknown Source) 19 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 20 at java.lang.ClassLoader.loadClass(Unknown Source) 21 at java.lang.Class.forName0(Native Method) 22 at java.lang.Class.forName(Unknown Source) 23 at per.sww.db.DbConn.getConn(DbConn.java:15) 24 at per.sww.dao.UserDao.checkLogin(UserDao.java:20) 25 at per.sww.page.MainPage.mainpage(MainPage.java:39) 26 at per.sww.page.MainPage.main(MainPage.java:12)
喔有一行是这个,粘错了= =
String sql="SELECT USERID,USERPASSWORD,USERMONEY FROM SALESMAN WHERE USERNAME=?";
导好了,还报错= =
缺这个mysql-connector-java-5.1.7-bin.jar?
吖,忘记导了
▂▂▂▂▂▂▂▂欢迎使用自助银行系统▂▂▂▂▂▂▂▂▂▂
1.账号登录
0.退出系统
▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂
请输入:
1
请输入账号:
请输入:
admin
请输入密码:
请输入:
123456
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'account.salesman' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.Util.getInstance(Util.java:384)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4232)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4164)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2838)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2212)
at per.sww.dao.UserDao.checkLogin(UserDao.java:25)
at per.sww.page.MainPage.mainpage(MainPage.java:39)
at per.sww.page.MainPage.main(MainPage.java:12)
!!用户名输入有误!!
剩余登陆次数:3
请输入账号:
请输入:
我想哭= =
@眉间剪水泛千愁: 没有SALESMAN ???
@眉间剪水泛千愁: 搞其他的吧。。 银行的东西维护难,业务复杂,技术更新慢。。
现在都是大数据互联网的时代了。
@waiter: 你的意思是让我跟你走哈?
@眉间剪水泛千愁: 额。。我搞得也不是大数据和互联网,女孩子在互联网方面毕竟优于银行方面,it也分好多行业