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~


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章