一、实体层(po层)
package com. hugeyurt. po ; import java. sql. ResultSet ;
import java. sql. SQLException ; public class UserInfo { private String userID; private String name; private int count; private Long errorTime; private String pwd; public UserInfo ( ) { } public String getUserID ( ) { return userID; } public void setUserID ( String userID) { this . userID = userID; } public String getName ( ) { return name; } public void setName ( String name) { this . name = name; } public int getCount ( ) { return count; } public void setCount ( int count) { this . count = count; } public Long getErrorTime ( ) { return errorTime; } public void setErrorTime ( Long errorTime) { this . errorTime = errorTime; } public String getPwd ( ) { return pwd; } public void setPwd ( String pwd) { this . pwd = pwd; } }
二、database持久层
package com. hugeyurt. database ; import java. lang. reflect. Field ;
import java. lang. reflect. InvocationTargetException ;
import java. lang. reflect. Method ;
import java. sql. ResultSet ;
import java. sql. SQLException ;
import java. util. ArrayList ; public class ORMUtil { private ORMUtil ( ) { }
public static Object [ ] toInsert ( StringBuffer sql, Object obj) { Class clz = obj. getClass ( ) ; String tableName = clz. getSimpleName ( ) ; sql. append ( "insert into " ) . append ( tableName) . append ( " (" ) ; Field [ ] fields = clz. getDeclaredFields ( ) ; Object [ ] params = new Object [ fields. length] ; int k = 0 ; for ( int i = 0 ; i < fields. length; i++ ) { Object value = null ; String fieldName = fields[ i] . getName ( ) ; try { fields[ i] . setAccessible ( true ) ; value = fields[ i] . get ( obj) ; } catch ( IllegalArgumentException e) { e. printStackTrace ( ) ; } catch ( IllegalAccessException e) { e. printStackTrace ( ) ; } if ( value == null ) continue ; sql. append ( fieldName) . append ( "," ) ; params[ k] = value; k++ ; } int index = sql. lastIndexOf ( "," ) ; sql. replace ( index, index + 1 , ")" ) ; sql. append ( "values(" ) ; for ( int i = 0 ; i < k; i++ ) sql. append ( "?," ) ; index = sql. lastIndexOf ( "," ) ; sql. replace ( index, index + 1 , ")" ) ; if ( k == fields. length) return params; Object [ ] temp = new Object [ k] ; for ( int i = 0 ; i < k; i++ ) { temp[ i] = params[ i] ; } return temp; } public static Object toObject2 ( ResultSet rs, Class clz) { Object obj = new Object ( ) ; try { obj = clz. newInstance ( ) ; } catch ( InstantiationException e) { e. printStackTrace ( ) ; } catch ( IllegalAccessException e) { e. printStackTrace ( ) ; } Field [ ] fields = clz. getDeclaredFields ( ) ;
for ( int i = 0 ; i < fields. length; i++ ) { Object value = null ; String columnLabel = fields[ i] . getName ( ) ; try { value = rs. getObject ( columnLabel) ; } catch ( SQLException e1) { e1. printStackTrace ( ) ; } if ( value == null ) continue ; Field f = fields[ i] ; try { f. setAccessible ( true ) ; if ( f. getType ( ) == Integer . TYPE || f. getType ( ) == Integer . class ) f. set ( obj, Integer . parseInt ( value. toString ( ) ) ) ; else if ( f. getType ( ) == Byte . TYPE || f. getType ( ) == Byte . class ) f. set ( obj, Byte . parseByte ( value. toString ( ) ) ) ; else if ( f. getType ( ) == Long . TYPE || f. getType ( ) == Long . class ) f. set ( obj, Long . parseLong ( value. toString ( ) ) ) ; else if ( f. getType ( ) == Double . TYPE || f. getType ( ) == Double . class ) f. set ( obj, Double . parseDouble ( value. toString ( ) ) ) ; else if ( f. getType ( ) == Float . TYPE || f. getType ( ) == Float . class ) f. set ( obj, Float . parseFloat ( value. toString ( ) ) ) ; else if ( f. getType ( ) == Boolean . TYPE || f. getType ( ) == Boolean . class ) f. set ( obj, Boolean . parseBoolean ( value. toString ( ) ) ) ; else if ( f. getType ( ) == Short . TYPE || f. getType ( ) == Short . class ) f. set ( obj, Short . parseShort ( value. toString ( ) ) ) ; else f. set ( obj, value) ; } catch ( IllegalArgumentException e) { e. printStackTrace ( ) ; } catch ( IllegalAccessException e) { e. printStackTrace ( ) ; } } return obj; }
public static String getMethod ( String columnLabel) { char [ ] str = columnLabel. toCharArray ( ) ; if ( str[ 0 ] > 'a' && str[ 0 ] < 'z' ) str[ 0 ] = ( char ) ( str[ 0 ] - 32 ) ; return "set" + new String ( str) ; } public static Object [ ] toQuery ( Object obj, StringBuffer sql) { Class clz = obj. getClass ( ) ; Field [ ] fields = clz. getDeclaredFields ( ) ; String columnName = fields[ 0 ] . getName ( ) ; Object value = null ; try { fields[ 0 ] . setAccessible ( true ) ; value = fields[ 0 ] . get ( columnName) ; } catch ( IllegalArgumentException e) { e. printStackTrace ( ) ; } catch ( IllegalAccessException e) { e. printStackTrace ( ) ; } sql. append ( "select userID from userinfo where userID=?" ) ; if ( value == null ) return null ; Object [ ] params = new Object [ ] { value } ; return params; } }
package com. hugeyurt. database ; import java. sql. Connection ;
import java. sql. DriverManager ;
import java. sql. PreparedStatement ;
import java. sql. ResultSet ;
import java. sql. SQLException ;
import java. util. ArrayList ; public class DBConnection { String url= "jdbc:mysql://127.0.0.1:3306/db20250715?characterEncoding=UTF-8" ; String user= "root" ; String pwd= "" ; Connection conn= null ; static { try { Class . forName ( "com.mysql.jdbc.Driver" ) ; } catch ( ClassNotFoundException e) { e. printStackTrace ( ) ; } } public DBConnection ( ) { try { this . conn= DriverManager . getConnection ( url, user, pwd) ; } catch ( SQLException e) { e. printStackTrace ( ) ; } } public int executeUpdate ( String sql, Object [ ] params) { PreparedStatement ps= null ; try { ps= this . conn. prepareStatement ( sql) ; if ( params!= null ) for ( int i= 0 ; i< params. length; i++ ) ps. setObject ( i+ 1 , params[ i] ) ; return ps. executeUpdate ( ) ; } catch ( SQLException e) { e. printStackTrace ( ) ; } finally { try { if ( ps!= null ) ps. close ( ) ; } catch ( SQLException e) { e. printStackTrace ( ) ; } } return 0 ; } public ArrayList < Object > executeQuery ( Class clz, String sql, Object [ ] params) { PreparedStatement ps= null ; ResultSet rs= null ; ArrayList < Object > list= new ArrayList < Object > ( ) ; try { ps= this . conn. prepareStatement ( sql) ; if ( params!= null ) for ( int i= 0 ; i< params. length; i++ ) ps. setObject ( i+ 1 , params[ i] ) ; rs= ps. executeQuery ( ) ; while ( rs. next ( ) ) { Object obj= ORMUtil . toObject2 ( rs, clz) ; list. add ( obj) ; } } catch ( SQLException e) { e. printStackTrace ( ) ; } finally { try { if ( rs!= null ) rs. close ( ) ; if ( ps!= null ) ps. close ( ) ; } catch ( SQLException e) { e. printStackTrace ( ) ; } } return list; } public boolean save ( Object obj) { StringBuffer sql= new StringBuffer ( ) ; Object [ ] params= ORMUtil . toInsert ( sql, obj) ; return executeUpdate ( sql. toString ( ) , params) > 0 ; } }
三、服务层
package com. hugeyurt. service ; import java. util. ArrayList ; import com. hugeyurt. database. DBConnection ;
import com. hugeyurt. po. UserInfo ;
import com. hugeyurt. vo. ResultVO ; public class LogService { private DBConnection conn= new DBConnection ( ) ; private static final int MAX_COUNT= 3 ; public ResultVO login ( String userID, String pwd) { UserInfo user= new UserInfo ( ) ; String sql= "select * from userinfo where userID=?" ; Object [ ] params= new Object [ ] { userID} ; Class clz= user. getClass ( ) ; ArrayList < Object > list= new ArrayList < Object > ( ) ; list= conn. executeQuery ( clz, sql, params) ; System . out. println ( ) ; ResultVO vo= new ResultVO ( ) ; if ( list. size ( ) == 0 ) { vo. setCode ( 100 ) ; vo. setMessage ( "no such userID" ) ; return vo; } user= ( UserInfo ) list. get ( 0 ) ; Long currentTime= System . currentTimeMillis ( ) ; if ( ( currentTime- user. getErrorTime ( ) ) > 30 * 1000 ) { this . update ( userID, currentTime, 0 ) ; } if ( user. getCount ( ) == 3 ) { vo. setCode ( 200 ) ; vo. setMessage ( "you have to wait " + ( 30 - ( currentTime- user. getErrorTime ( ) ) / 1000 ) + " hours" ) ; return vo; } if ( pwd. equals ( user. getPwd ( ) ) ) { vo. setCode ( 300 ) ; vo. setMessage ( "success!" ) ; vo. setUser ( user) ; this . update ( userID, 0L , 0 ) ; return vo; } user. setCount ( user. getCount ( ) + 1 ) ; if ( user. getCount ( ) == 3 ) { vo. setCode ( 400 ) ; vo. setMessage ( "you have been locked for 24 hours!" ) ; this . update ( userID, currentTime, user. getCount ( ) ) ; return vo; } vo. setCode ( 500 ) ; vo. setMessage ( "password is error!" ) ; this . update ( userID, currentTime, user. getCount ( ) ) ; return vo; } public void update ( String userID, Long currentTime, Integer count) { String sql= "update userinfo set errorTime=?,count=? where userID=?" ; Object [ ] params= new Object [ ] { currentTime, count, userID} ; conn. executeUpdate ( sql, params) ; } }
四、数据传输对象
package com. hugeyurt. vo ; import com. hugeyurt. po. UserInfo ; public class ResultVO { private Integer code; private String message; private UserInfo user; public Integer getCode ( ) { return code; } public void setCode ( Integer code) { this . code = code; } public String getMessage ( ) { return message; } public void setMessage ( String message) { this . message = message; } public UserInfo getUser ( ) { return user; } public void setUser ( UserInfo user) { this . user = user; } }
五、表示层
package com. hugeyurt. view ; import java. util. Scanner ; import com. hugeyurt. service. LogService ;
import com. hugeyurt. vo. ResultVO ;
public class View { public static void menu ( ) { System . out. println ( "1.login" ) ; System . out. println ( "0.exit" ) ; } public static void main ( String [ ] args) { LogService log= new LogService ( ) ; ResultVO vo= new ResultVO ( ) ; while ( true ) { menu ( ) ; Scanner sc= new Scanner ( System . in) ; int op= sc. nextInt ( ) ; if ( op== 1 ) { System . out. println ( "input your userID:" ) ; String userID= sc. next ( ) ; System . out. println ( "input your password:" ) ; String pwd= sc. next ( ) ; vo= log. login ( userID, pwd) ; if ( vo. getCode ( ) == 300 ) { System . out. println ( "welcome!" ) ; System . out. println ( "userID:" + vo. getUser ( ) . getUserID ( ) + " name:" + vo. getUser ( ) . getName ( ) ) ; } else { System . out. println ( vo. getMessage ( ) ) ; } } else if ( op== 0 ) break ; } }
}