簡單的純java語言登入和註冊功能
阿新 • • 發佈:2018-12-25
**Main.java**
package com.su.main;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
new Menu();
}
}
**Menu.java**
package com.su.main;
import java.util.Scanner;
public class Menu {
Operate operate = new Operate();
public Menu(){
while (true){
this.show();
}
}
public void show(){
System.out.println("歡迎進入阿輝資訊管理系統");
System.out.println("***1-登入***");
System.out.println("***2-註冊***");
System.out.println("***0-系統退出***");
Scanner input = new Scanner(System.in );
System.out.println("請選擇:");
int i = input.nextInt();
switch(i){
case 1 : {
operate.login();
break;
}
case 2 : {
operate.register();
break;
}
case 0 : {
System.exit(1 );
break;
}
default: {
System.out.println("請選擇正確的操作!");
}
}
}
}
**Operate.java**
package com.su.main;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class Operate {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
public void login(){
Scanner scan = new Scanner(System.in);
int i = 3;
String username = null;
String password = null;
while(i != -1){ //迴圈進行
System.out.print("請輸入總系統使用者名稱:");
username = scan.next(); //輸入使用者名稱
while(username == null){ //判斷使用者名稱是否為空
System.out.print("請輸入總系統使用者名稱:");
username = scan.next();
}
System.out.println("請輸入總系統密碼:");
password = scan.next(); //輸入密碼
while(password == null){ //判斷密碼是否為空
System.out.println("請輸入總系統密碼:");
password = scan.next();
}
// String str1 = "su"; //定義初始使用者名稱
// String str2 = "su";
// //定義初始密碼
if(checkUserLogin(username,password)){ //比較輸入的使用者名稱、密碼是否和初始值相同
System.out.println("歡迎"+username+",您已成功登入");
new Menu();
}
else{
if(i == 0){ //三次登陸都錯,退出系統
System.out.println("對不起,您三次都輸入錯誤,系統將退出!!!");
System.exit(1);
}
System.out.println("對不起您輸入的使用者名稱或密碼有誤,請重新輸入("+ i-- +"次機會)!!!");
}
}
scan.close();
}
public void register(){
User user = new User();
Scanner input = new Scanner(System.in);
System.out.println("請輸入您的姓名:");
String username = input.next();
user.setUsername(username);
System.out.println("請輸入您的密碼:");
String userpassword = input.next();
user.setUserpassword(userpassword);
try {
if(doAdd(user)){
System.out.println("註冊成功!");
}else{
System.out.println("註冊失敗!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean checkUserLogin(String userName,String userPwd){
boolean flag = false;
User user = null;
try{
conn = DBConn.createDBConn();
String sql = "select * from user where username=? and userpassword=?";
ps = conn.prepareStatement(sql);
ps.setString(1, userName);
ps.setString(2, userPwd);
rs = ps.executeQuery();
while(rs.next()){
user = new User();
user.setUsername(rs.getString("username"));
user.setUserpassword(rs.getString("userpassword"));
}
flag = true;
}catch(Exception ex){
ex.printStackTrace();
}finally{
DBConn.closeConn(conn);
}
return flag;
}
public boolean doAdd(User user) throws Exception{
boolean flag = false;
PreparedStatement pstmt = null;
String sql = "INSERT INTO user(username,userpassword)" + "VALUES(?,?)";
try{
conn = DBConn.createDBConn();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getUserpassword());
if (pstmt.executeUpdate() > 0) {
flag = true;
}
} catch (Exception e) {
throw e;
} finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e1) {
}
}
}
return flag;
}
}
**DBConn,java**
package com.su.main;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class DBConn {
// public static void main(String[] args)throws Exception {
// createDBConn();
//
// System.out.println("成功");
// Statement state = createDBConn().createStatement();
// System.out.println(state);
// }
public static Connection createDBConn(){
//得到資料連線
try{
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb","root","su1573");
return conn;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
public static void closeConn(Connection conn){
try{
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
執行結果如圖:
資料庫資料如圖: