项目结构
Main
package com
.jd
.tool
.test
;
import java
.util
.Scanner
;
import java
.util
.UUID
;
import com
.jd
.tool
.MD5Tool
;
import com
.jd
.tool
.db
.DBLink
;
public class Main {
public static void main(String
[] args
) {
System
.out
.println("*********************************");
System
.out
.println("*\t\t\t\t*");
System
.out
.println("*\t欢迎使用注册登录系统\t\t*");
System
.out
.println("*\t\t\t\t*");
System
.out
.println("*********************************");
while (true) {
menu();
}
}
static void menu() {
System
.out
.println("1、注册");
System
.out
.println("2、登录");
System
.out
.println("3、退出");
System
.out
.println("请输入操作,以Enter键结束:");
Scanner scanner
= new Scanner(System
.in
);
int option
= scanner
.nextInt();
switch (option
) {
case 1:{
System
.out
.println("请输入用户名:");
String userName
= scanner
.next();
System
.out
.println("请输入密码:");
String password
= scanner
.next();
System
.out
.println("请确认密码:");
String repassword
= scanner
.next();
String sql
= "select id from user_info where user_name=?";
if(new DBLink().exist(sql
, userName
)) {
System
.out
.println("用户名已存在!");
return;
}
if(!password
.equals(repassword
)) {
System
.out
.println("密码不一致!");
return;
}
String id
= UUID
.randomUUID().toString();
password
= MD5Tool
.encrypt(password
);
sql
="insert into user_info(id,user_name,password) values('"+id
+"',?,?)";
if(new DBLink().update(sql
,userName
,password
)) {
System
.out
.println("注册成功!");
return;
}
System
.out
.println("注册失败!");
break;
}
case 2:{
System
.out
.println("请输入用户名:");
String userName
= scanner
.next();
System
.out
.println("请输入密码:");
String password
= scanner
.next();
password
= MD5Tool
.encrypt(password
);
String sql
= "select id form user_info where user_name=? and password=?";
if(new DBLink().exist(sql
,userName
,password
)) {
System
.out
.println("登录成功!");
return;
}
System
.out
.println("用户名或密码错误,登录失败!");
break;
}
case 3:{
System
.out
.println("已退出系统!");
System
.exit(0);
break;
}
default:
System
.out
.println("I'm Sorry,there is not the "+option
+" option,please try again.");
}
}
}
DBLink
package com
.jd
.tool
.db
;
import java
.sql
.Connection
;
import java
.sql
.DriverManager
;
import java
.sql
.PreparedStatement
;
import java
.sql
.ResultSet
;
import java
.sql
.SQLException
;
import java
.sql
.Statement
;
import org
.apache
.log4j
.Logger
;
import com
.jd
.tool
.PropertiesTool
;
public class DBLink {
private Logger logger
= Logger
.getLogger(DBLink
.class);
private Connection
getConnection() {
try {
Class
.forName("com.mysql.jdbc.Driver");
String userName
= PropertiesTool
.getValue("db.username");
String password
= PropertiesTool
.getValue("db.password");
String url
= PropertiesTool
.getValue("db.url");
return DriverManager
.getConnection(url
, userName
, password
);
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}
return null
;
}
public boolean exist(String sql
) {
Connection connection
= null
;
Statement statement
=null
;
ResultSet resultSet
=null
;
try {
connection
= getConnection();
statement
= connection
.createStatement();
resultSet
= statement
.executeQuery(sql
);
return resultSet
.next();
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(resultSet
,statement
,connection
);
}
return false;
}
public boolean exist(String sql
, Object
...params
) {
Connection connection
= null
;
PreparedStatement preparedStatement
=null
;
ResultSet resultSet
=null
;
try {
connection
= getConnection();
preparedStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
preparedStatement
.setObject(i
+1, params
[i
]);
}
resultSet
= preparedStatement
.executeQuery();
return resultSet
.next();
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(resultSet
,preparedStatement
,connection
);
}
return false;
}
public void select(String sql
,IRowMapper rowMapper
) {
Connection connection
= null
;
Statement statement
=null
;
ResultSet resultSet
=null
;
try {
connection
= getConnection();
statement
= connection
.createStatement();
resultSet
= statement
.executeQuery(sql
);
rowMapper
.rowMapper(resultSet
);
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(resultSet
,statement
,connection
);
}
}
public void select(String sql
,IRowMapper rowMapper
,Object
...params
) {
Connection connection
= null
;
PreparedStatement preparedStatement
=null
;
ResultSet resultSet
=null
;
try {
connection
= getConnection();
preparedStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
preparedStatement
.setObject(i
+1, params
[i
]);
}
resultSet
= preparedStatement
.executeQuery();
rowMapper
.rowMapper(resultSet
);
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(resultSet
,preparedStatement
,connection
);
}
}
public boolean update(String sql
) {
Connection connection
= null
;
Statement statement
= null
;
try {
connection
= getConnection();
statement
= connection
.createStatement();
int result
= statement
.executeUpdate(sql
);
return result
>0;
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(statement
,connection
);
}
return false;
}
public boolean update(String sql
,Object
...params
) {
Connection connection
= null
;
PreparedStatement preparedStatement
= null
;
try {
connection
= getConnection();
preparedStatement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< params
.length
; i
++) {
preparedStatement
.setObject(i
+1, params
[i
]);
}
return preparedStatement
.executeUpdate()>0;
} catch (Exception e
) {
logger
.debug(e
.getMessage(), e
);
}finally {
close(preparedStatement
,connection
);
}
return false;
}
private void close(Statement statement
,Connection connection
) {
try {
if(statement
!=null
) {
statement
.close();
}
} catch (SQLException e
) {
logger
.debug(e
.getMessage(), e
);
}
try {
if(connection
!=null
) {
connection
.close();
}
} catch (SQLException e
) {
logger
.debug(e
.getMessage(), e
);
}
}
private void close(ResultSet resultSet
,Statement statement
,Connection connection
) {
try {
if(resultSet
!=null
) {
resultSet
.close();
}
} catch (SQLException e
) {
logger
.debug(e
.getMessage(), e
);
}
close(statement
,connection
);
}
}
IRowMapper
package com
.jd
.tool
.db
;
import java
.sql
.ResultSet
;
public interface IRowMapper {
void rowMapper(ResultSet rs
);
}
PropertiesTool
package com
.jd
.tool
;
import java
.io
.IOException
;
import java
.io
.InputStream
;
import java
.util
.Properties
;
public class PropertiesTool {
private static Properties properties
= new Properties();
static {
InputStream inputStream
= PropertiesTool
.class.getClassLoader().getResourceAsStream("db.properties");
try {
properties
.load(inputStream
);
} catch (IOException e
) {
e
.printStackTrace();
}
}
public static String
getValue(String key
) {
return properties
.getProperty(key
);
}
}
MD5Tool
package com
.jd
.tool
;
import java
.math
.BigInteger
;
import java
.security
.MessageDigest
;
public class MD5Tool {
public static String
encrypt(String password
) {
byte[] bytes
= null
;
try {
MessageDigest messageDigest
= MessageDigest
.getInstance("MD5");
messageDigest
.update(password
.getBytes());
bytes
= messageDigest
.digest();
} catch (Exception e
) {
e
.printStackTrace();
}
String result
= new BigInteger(1, bytes
).toString(16);
for (int i
= 0; i
< 32 - result
.length(); i
++) {
result
= "0" + result
;
}
return result
;
}
}