jdbc测试 -- 登录的实现(自己小笔记)

 
  

//DBUtils.java

package com.itheima.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

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");
		//给上面4个变量赋值
		driverClass = rb.getString("driverClass");
		url = rb.getString("url");
		user = rb.getString("user");
		password = rb.getString("password");
		try {
			Class.forName(driverClass);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	//得到连接
	public static Connection getConnection() throws SQLException{
		return DriverManager.getConnection(url, user, password);
	}
	
	//关闭资源 
	public static void closeAll(ResultSet rs,Statement stmt,Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			rs = null;
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt = null;
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			conn = null;
		}
	}
}
 
  

//DoLogin.java

 
  
package com.itheima.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.itheima.entity.User;
import com.itheima.util.DBUtils;

public class DoLogin {
	
	/**
	 * 根据用户名和密码查询用户对象信息
	 * @param name
	 * @param pwd
	 * @return u
	 */
	public User findUser(String name,String pwd){
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		User u = null;
		try {
			conn = DBUtils.getConnection();//得到连接对象Connection
			String sql ="SELECT * FROM users WHERE NAME=? AND PASSWORD=?";
			stmt = conn.prepareStatement(sql);//得到执行sql语句的对象Statement
			//给?赋值
			stmt.setString(1, name);
			stmt.setString(2, pwd);
			
			rs = stmt.executeQuery();//执行sql语句
			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 (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtils.closeAll(rs, stmt, conn);
		}
		
		return u;
	}
}


 
  

//login.java

package com.itheima.client;

import java.util.Scanner;

import com.itheima.entity.User;
import com.itheima.service.DoLogin;

public class Login {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String name = input.nextLine();
		System.out.println("请输入密码:");
		String pwd = input.nextLine();
		
		DoLogin dl = new DoLogin();
		User user = dl.findUser(name, pwd);//调用查询用户的方法
		if(user!=null){
			System.out.println("欢迎你:"+user.getName());
		}else{
			System.out.println("用户名或密码错误!");
		}
		
	}
	
}




//配置文件  dbinfo.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql:///day06
username=root
password=abc


 
 

你可能感兴趣的:(jdbc测试 -- 登录的实现(自己小笔记))