DButils 도구 사용

6911 단어 Java
STEP 1: 우선 jar 패키지 4 개 가 져 오기
1. mysql-connector-java-5.1.13-bin.jar
2. commons-beanutils-1.9.3.jar
3. commons-logging-1.2.jar
4. commons-dbutils-1.7.jar
두 번 째 단계: 자바 빈 작성
package com.ydwj.DBUtils;

public class Girl {
	private Integer girlID;
	private String girlName;
	private Integer girlAge;

	public Girl() {}

	public Girl(Integer girlID, String girlName, Integer girlAge) {
		super();
		this.girlID = girlID;
		this.girlName = girlName;
		this.girlAge = girlAge;
	}

	public String getGirlName() {
		return girlName;
	}

	public void setGirlName(String girlName) {
		this.girlName = girlName;
	}

	public Integer getGirlAge() {
		return girlAge;
	}

	public void setGirlAge(Integer girlAge) {
		this.girlAge = girlAge;
	}

	public Integer getGirlID() {
		return girlID;
	}
	public void setGirlID(Integer girlID) {
		this.girlID = girlID;
	}

	@Override
	public String toString() {
		return "Girl [girlID=" + girlID + ", girlName=" + girlName + ", girlAge=" + girlAge + "]";
	}

}

세 번 째 단계: DBUtils 방법 테스트 클래스 작성
query 관련 방법
package com.ydwj.DBUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.ydwj.utils.JDBCUtils;

public class Demo1 {
	public static void main(String[] args) {
//		query1();
//		query2();
//		query3();
//		query4();
//		query5();
//		query6();
		query7();
	}
	
	/**
	 *      girl  
	 *   ResultSetHandler
	 */
	public static void query1() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl where girlID=2";
		QueryRunner qr = new QueryRunner();
		try {
			Girl girl = qr.query(conn, sql, new ResultSetHandler() {

				@Override
				public Girl handle(ResultSet arg0) throws SQLException {
					Girl g = null;
					if (arg0.next()) {
						g = new Girl(arg0.getInt("girlID"), arg0.getString("girlName"), 
								arg0.getInt("girlAge"));
					}
					return g;
				}

			});
			System.out.println(girl);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
	}

	/**
	 *     girl  
	 *  BeanHandler
	 */
	public static void query2() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl where girlID = 3";
		QueryRunner qr = new QueryRunner();
		try {
			Girl girl = qr.query(conn, sql, new BeanHandler<>(Girl.class));
			System.out.println(girl);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
	}
	
	
	/**
	 *     girl  ,       Girl   List  
	 *   BeanListHandler
	 */
	public static void query3() {
		Connection conn = JDBCUtils.getConnection();
		QueryRunner qr = new QueryRunner();
		String sql = "select * from girl";
		
		try {
			List list = qr.query(conn, sql, new BeanListHandler<>(Girl.class));
			
			for (Girl girl : list) {
				System.out.println(girl);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.close(conn, null);
		}
		
	}

	/**
	 *     Girl  
	 *      ,QueryRunner query(Connection conn, String sql, ResultSetHandler rs, Object... params)
	 */
	public static void query4() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl where girlID=?";
		QueryRunner qr = new QueryRunner();
		try {
			Girl girl = qr.query(conn, sql, new BeanHandler<>(Girl.class), 2);
			System.out.println(girl);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
		
	}

	
	/**
	 *                Object      
	 */
	public static void query5() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl where girlID=2";
		QueryRunner qr = new QueryRunner();
		try {
			Object[] arr = qr.query(conn, sql, new ArrayHandler());
			System.out.println(Arrays.toString(arr));
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
		
	}
	
	
	/**
	 *             ,            ,           List   
	 */
	public static void query6() {
		Connection conn = JDBCUtils.getConnection();
		String sql = "select * from girl";
		QueryRunner qr = new QueryRunner();
		try {
			List list = qr.query(conn, sql, new ArrayListHandler());
			for (Object[] objects : list) {
				System.out.println(Arrays.toString(objects));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
	}
	
	public static void query7() {
		Connection conn = JDBCUtils.getConnection();
		QueryRunner qr = new QueryRunner();
		String sql = "select * from girl";
		
		try {
			List> list = qr.query(conn, sql, new MapListHandler());
			for (Map map : list) {
				Set> entrySet = map.entrySet();
				for (Entry entry : entrySet) {
					System.out.println(entry);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtils.close(conn, null);
		}
	}
}

업데이트 방법
package com.ydwj.DBUtils;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;

import com.ydwj.utils.JDBCUtils;

public class Demo2 {
	/**
	 *           、 、   
	 */
	@Test
	public void testUpdate() {
		String sql = "delete from girl where girlID=1";
		Connection conn = JDBCUtils.getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			qr.update(conn, sql);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.close(conn, null);
			//     DBUtils     
			//DbUtils.close(conn);
		}
	}
	
	/**
	 *    ,          ,        
	 */
	@Test
	public void testBatch() {
		String sql = "insert into girl(girlName,girlAge) values(?,?)";
		Connection conn = JDBCUtils.getConnection();
		QueryRunner qr = new QueryRunner();
		try {
			qr.batch(conn, sql, new Object[][] {{"  ",22},{"   ",21}});
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtils.close(conn, null);
		}
	}
}

좋은 웹페이지 즐겨찾기