1. 程式人生 > >JDBC------Dao模式(二)

JDBC------Dao模式(二)

jdbc.properties:

driverClass=com.mysql.jdbc.Driver
url = jdbc:mysql://localhost/jdbc
name = root
password = root

JDBCUtil:
 

package com.test.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JDBCUtil {
	
	static String driverClass = null;
	static String url = null;
	static String name = null;
	static String password = null;
	
	//讀取jdbc.properties
	static{
		try {
			//1.建立一個屬性配置物件
			Properties properties = new Properties();
			
			//1.對應檔案位於工程根目錄  
			//InputStream is = new FileInputStream("jdbc.properties");
			
			//2.使用類載入器,讀取drc下的資原始檔  對應檔案位於src目錄底下  建議使用
			InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
			//2.匯入輸入流,抓取異常
			properties.load(is);
			//3.讀取屬性
			driverClass = properties.getProperty("driverClass");
			url = properties.getProperty("url");
			name = properties.getProperty("name");
			password = properties.getProperty("password");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 註冊驅動 建立引數
	 * <p>Title: close</p>
	 * <p>Description: </p>
	 * @param connection
	 * @param resultSet
	 * @param statement
	 */
	
	public static Connection getConn(){
			
		Connection connection = null;
		//2. 建立連線 引數一: 協議 + 訪問的資料庫 , 引數二: 使用者名稱 , 引數三: 密碼。
		 try {
			 //Class.forName(driverClass);可寫可不寫
			 //Class.forName(driverClass);
			connection = DriverManager.getConnection(url, name, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return connection;
		
	}
	
	/**
	 * 釋放資源
	 * <p>Title: close</p>
	 * <p>Description: </p>
	 * @param connection
	 * @param resultSet
	 * @param statement
	 */
	public static void close(Connection connection,ResultSet resultSet,Statement statement){
		closeRS(resultSet);
		closeSt(statement);
		closeConn(connection);
		
	}
	public static void close(Connection connection,Statement statement){
		closeSt(statement);
		closeConn(connection);
		
	}
	
	private static void closeRS(ResultSet resultSet){
		try {
			if(resultSet !=null){
				resultSet.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			resultSet = null;
		}
	}
	
	private static void closeSt(Statement statement){
		try {
			if(statement !=null){
				statement.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			statement = null;
		}
	}
	
	private static void closeConn(Connection connection){
		try {
			if(connection !=null){
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			connection = null;
		}
	}

	


}

UserDao:

package com.test.dao;


/**
 * 定義操作資料庫的方法
 * <p>Title: UserDao</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
public interface UserDao {
	
	//查詢
	//void findAll();
	
	//登入方法
	void login(String username , String password);
}

UserDaoImpl:

package com.test.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.test.dao.UserDao;
import com.test.util.JDBCUtil;

public class UserDaoImpl implements UserDao {



	/**
	 * 查詢
	 * <p>Title: findAll</p>
	 * <p>Description: </p>
	 * @see com.test.dao.UserDao#findAll()
	 */
	
	Connection connection = null;
	Statement statement = null;
	ResultSet resultSet = null;
	
//	public void findAll() {
//		try {
//			connection = JDBCUtil.getConn();
//			statement = connection.createStatement();
//			String sql ="select * from user";
//			resultSet = statement.executeQuery(sql);
//			while(resultSet.next()){
//				String username = resultSet.getString("username");
//				String password = resultSet.getString("password");
//				
//				System.out.println("username="+username+",password="+password);
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//		}finally{
//			JDBCUtil.close(connection, resultSet, statement);
//		}
//
//	}
//	
	
    /**
     * 登入
     * <p>Title: login</p>
     * <p>Description: </p>
     * @param username
     * @param password
     * @see com.test.dao.UserDao#login(java.lang.String, java.lang.String)
     */
	@Override
	public void login(String username, String password) {
		
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		try {
			//1. 獲取連線物件
			conn = JDBCUtil.getConn();
			//2. 建立statement物件
			st = conn.createStatement();
//			SELECT * FROM t_user WHERE username='admin' AND PASSWORD='123456'
			String sql = "select * from user where username='"+ username  +"' and password='"+ password +"'";
			rs = st.executeQuery(sql);
			
			if(rs.next()){
				System.out.println("登入成功");
			}else{
				System.out.println("登入失敗");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, resultSet, statement);
		}
	}
}


JunitTest:

package com.test.example;

import org.junit.Test;

import com.test.dao.UserDao;
import com.test.dao.impl.UserDaoImpl;  

public class JunitTest {
	
	//查詢
//	@Test
//	public void testFindAll(){
//		UserDao dao = new UserDaoImpl();
//		dao.findAll();
//	}
	
	
		
//	 	//登入
	   @Test
		public void testLogin(){
			UserDao dao = new UserDaoImpl();
			//dao.login("admin", "123456");//可行
			dao.login("admin", "123456 'or'1=1");//可行
		}



}

 資料庫:

執行結果: