JDBC 입문부터 숙련까지(둘)

5412 단어
4
import java.sql.*;
import java.util.List;
//Dao   
public class DaoFactory {
	private static String driver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static String url="jdbc:sqlserver://localhost:1433;DatabaseName=News";
	private static String sql="insert userInfo values('admin','admin',getdate())";
	private static String user="sa";
	private static String pwd="sa";
	
	//1.              
	public static Connection getConnection(){
		Connection con=null;
		try {
			Class.forName(driver);// , 
			con=DriverManager.getConnection(url,user,pwd);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;// void   return
	}
	//2.      ; 3   !,    !!!
	public static void closeAll(ResultSet rs,Statement stmt,Connection con){
		try {
			if(rs!=null){
				rs.close();
			}
			if(stmt!=null){
				stmt.close();
			}
			if(con!=null){
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	//3.setParams,            ?     ;
	public void setParams(PreparedStatement pstmt,Object[]params){
		if(params==null){return; }//return:    ,    ;
		try {
			for(int i=0;i<params.length;i++){
				pstmt.setObject(i+1,params[i]);
			}
		} catch (SQLException e) {//   ,   
			e.printStackTrace();
		}
	}
	//4.        ,         sql  ;
	public int executeUpdate(String sql,Object[]params){
		//1.    ;          ;
		Connection con=null;
		PreparedStatement pstmt=null;
		int count=0; //         ;
		
		try {
			con=this.getConnection();//       ;
			pstmt=con.prepareStatement(sql);//   :     ,?
			setParams(pstmt,params);//    ?   ,     !!!
			count=pstmt.executeUpdate();//3.  ;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			this.closeAll(null, pstmt, con);
		}
		return count;
	}
	//5.      ;
	public static List executeQuery(String sql, Object[] params) {
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		int colCount = 0;
		ArrayList tableList=new ArrayList();//   
		
		try {
			con = getConnection();
			pstmt = con.prepareStatement(sql);
			setParams(pstmt, params);
			rs = pstmt.executeQuery();//     ,   rs
			ResultSetMetaData rd = rs.getMetaData();//      
			colCount = rd.getColumnCount();
			while (rs.next()) {
				ArrayList rowList = new ArrayList();//   
				for (int i = 1; i <= colCount; i++) {
					rowList.add(rs.getString(i));
				}
				tableList.add(rowList);				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeAll(rs,pstmt,con);
		}
		return tableList;
	}
}
DAO 커넥터
import java.util.List;
//  UserInfo       ;
public interface UserInfoDAO {
	public int insertUserInfo(UserInfo user); //saveXXX
	public int updateUserInfo(UserInfo user);
	public int deleteUserInfo(UserInfo user);
	public List<UserInfo>queryUserInfo();//      
}

//구현 클래스
import java.util.List;


public class UserInfoDAOImpl extends DaoFactory implements UserInfoDAO {

	@Override
	public int deleteUserInfo(UserInfo user) {
		return 0;
	}

	/* (non-Javadoc)
	 * @see     ,  
	 */
	@Override
	public int insertUserInfo(UserInfo user) {
		int result=0;
		String sql="";
		Object[]params=;
		result=super.executeUpdate(sql, params);
		return result;
	}

	@Override
	public List<UserInfo> queryUserInfo() {
		String sql="select * from userinfo";
		List list=DaoFactory.executeQuery(sql, null);
		return list;
	}

	@Override
	public int updateUserInfo(UserInfo user) {
		return 0;
	}

}

테스트 클래스
package nan;
import java.util.List;

public class Test {
	public static void main(String[] args) {
		
		UserInfoDAO udd=new UserInfoDAOImpl();
		List tableList=udd.queryUserInfo();
		for(int i=0;i<tableList.size();i++){
			List rowList=(List)tableList.get(i);
			//cann't from Object to List,    
			for(int j=0;j<rowList.size();j++){
				System.out.print(rowList.get(j)+"\t");
			}
			System.out.println();//       
		}
	}
}

좋은 웹페이지 즐겨찾기