1. 程式人生 > >java利用jdbc連線Mysql資料庫——實現登入註冊功能

java利用jdbc連線Mysql資料庫——實現登入註冊功能

實現功能如下:

①0選中註冊,若使用者名稱相同則註冊失敗,重新選擇

②若使用者名稱不存在則儲存到資料庫

③1選中登入,若使用者名稱和密碼符合時,登入成功。

程式碼如下:

package com.lucfzy;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;


public class SqlText {
	private static String userID;
	private static String psw1;
	
	public static void main(String args[]) throws ClassNotFoundException, SQLException{
		System.out.println("請輸入數字 :0--代表註冊,1--代表登入");
		Scanner input = new Scanner(System.in);
		while(true){
			//註冊模組
			if (input.nextInt()==0) {
				//預設輸入的是字串,所以建議測試的時候輸入字串
				while(true){
					System.out.println("使用者名稱:");
					Scanner scanner = new Scanner(System.in);
					//獲取使用者名稱
					userID = scanner.nextLine(); 
					System.out.println("密碼:");
					Scanner scanner2 = new Scanner(System.in);
					//獲取密碼
					psw1 = scanner2.nextLine();
					//先檢查使用者名稱是否存在,若不存在則繼續註冊
					String sql2 = "select * from table_name where ID ="+"'"+userID+"'";
					DBHelper DBquery = new DBHelper();
					ResultSet rsSet =  DBquery.DB(sql2);
					if (rsSet.next()) {
						System.out.println("該使用者名稱已被註冊,請更換您的使用者名稱再進行註冊");
						DBquery.downConn();
						break;
					}
					else{
						String sql = "insert into table_name(ID,PASSWORD) values ('fzynzs','fzynzs')";
						DBHelper DBupdate = new DBHelper();
						DBupdate.excuteSql(sql);
						System.out.println("恭喜您註冊成功");
						//關閉connection的連線哦,防止洩露
						DBupdate.downConn();
						scanner.close();
						scanner2.close();
						break;
					}
				}
			}
			//登入模組
			else {
				//預設輸入的是字串,所以建議測試的時候輸入字串
				System.out.println("使用者名稱:");
				Scanner scanner = new Scanner(System.in);
				userID = scanner.nextLine(); 
				System.out.println("密碼:");
				Scanner scanner2 = new Scanner(System.in);
				psw1 = scanner2.nextLine();
				//*代表的是所有列
				String sql2 = "select * from table_name where ID ="+"'"+userID+"'"+"and password="+"'"+psw1+"'";
				//每次新建一個物件,就是新建一個連線connection
				DBHelper dbHelper = new DBHelper();
				//在資料庫中查詢結果,若結果存在返回登入成功
				ResultSet rsResultSet = dbHelper.DB(sql2);
				if(rsResultSet.next()==true){
					System.out.println("登入成功");
					dbHelper.downConn();
				}
				else{
					System.out.println("登入失敗,請檢查您的使用者名稱和密碼是否正確");
					dbHelper.downConn();
				}
				scanner.close();
				scanner2.close();
				break;
			}
		}
		input.close();
	}
}

class DBHelper{
	public Connection connection;
	public PreparedStatement preparedStatement;
	
	public DBHelper() throws SQLException, ClassNotFoundException{
		Class.forName("com.mysql.jdbc.Driver");
		connection = DriverManager.getConnection("jdbc:mysql://your IP  Address:3306/DBname","username","password");
	}
	
	public ResultSet DB(String sql) throws ClassNotFoundException, SQLException{
		preparedStatement = connection.prepareStatement(sql);
	    ResultSet resultSet = preparedStatement.executeQuery();
	    return resultSet;
	}
	
	public void excuteSql(String sql) throws SQLException{
		preparedStatement = connection.prepareStatement(sql);
	    preparedStatement.executeUpdate();
	}
	
	public void downConn() throws SQLException{
		connection.close();
	}
	
}

大家可以在此基礎上進行優化,並新增圖形化介面。完善該程式,bingo~