java連接MySql數據庫 實現用戶登錄功能

一: 準備工作

        1.首先將MySql的jar包導入項目文件中,在進行連接。(Build path--> Add)

        2.寫好配置文件(dbinfo.properties)

        3.

      

        4.創建相應的類進行。

二:具體實現

    1:登錄界面

        

public class DengLu {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		System.out.println("請輸入帳號:");
		String name = sc.nextLine();
		
		System.out.println("請輸入密碼:");
		String pwd  = sc.nextLine();
	
		DoDengLu dl = new DoDengLu();
		User   user   = dl.findUser(name, pwd);
		if(user!=null)
		{
			System.out.print("welcome to  "+user.getName());
		}
		else
			System.out.print("輸入錯誤");
	
	}

}

    2.與數據庫實現交互

public class DoDengLu {

	public User findUser(String name,String pwd)
	{
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;		
		User u  =null;
		try{
			conn  = DBUtils.getConnection();
		
			String Sql = "select * from stu28 where NAme=? and password=?"; // 預編譯
			
			stmt = conn.prepareStatement(Sql);
			// 給問號賦值
			stmt.setString(1,name);
			stmt.setString(2,pwd);
//			String sql ="select * from stu28 where Name = '"+name+"' AND PASsword = '"+pwd+"'";
			// 這樣寫會導致SQL注入問題    就是後面   or   '1'='1
			
			rs  = stmt.executeQuery();
			if(rs.next())
			{
				u  =  new User();
				u.setId(rs.getInt(1));
				u.setName(rs.getString(2));
				u.setPassword(rs.getString(3));
				u.setEmail(rs.getString(4));
				u.setBirthday(rs.getDate(5));
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			DBUtils.closeAll(rs, stmt, conn);
		}
		return u; 
	}
}

    3.幫助工具

    

public class DBUtils {

		private static String DriverClass;
		private static String url;
		private static String user;
		private static String password;
		// 靜態代碼塊   類只要加載,直接執行     
		 // 就是將配置文件加載;
		static{
			ResourceBundle rb  = ResourceBundle.getBundle("dbinfo");
			// 進行賦值操作;
			DriverClass   = rb.getString("DriverClass");
			url  = rb.getString("url");
			user = rb.getString("user");
			password = rb.getString("password");
			try {
				Class.forName(DriverClass);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		// 創建兩個方法:    // yi :得到連接的方法
		public static Connection getConnection() throws Exception{
			
			return  DriverManager.getConnection(url,user,password);
				
			
		}
		// 二 : 關閉資源
		public static void closeAll(ResultSet rs,Statement stmt,Connection conn)
		{
			if(rs!=null)
			{
				try{
					rs.close();
				}
				catch(Exception e){
					e.printStackTrace();
				}
			}
			

			if(stmt!=null)
			{
				try{
					stmt.close();
				}
				catch(Exception e){
					e.printStackTrace();
				}
			}
			

			if(conn!=null)
			{
				try{
					conn.close();
				}
				catch(Exception e){
					e.printStackTrace();
				}
			}
			
		}
		
		
		
	}
    4.用戶
public class User {
	private int id;
	private String name;
	private String password;
	private String email;
	private Date birthday;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

}

5.配置文件

 // 根據自己修改
DriverClass = com.mysql.jdbc.Driver   
url = jdbc:mysql:///JDBC28
user = root
password =root


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