第四篇

MyHospital

JDBCUtil.java

package com.chinasofti.hospital.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtil {
	static {
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		

	public static Connection getConn() throws SQLException {
		Connection conn=null;
		String url=null;
		String userName=null;
		String password=null;
		Properties properties=new Properties();
		try {
			properties.load(JDBCUtil.class.getClassLoader().getResourceAsStream("com/chinasofti/hospital/util/db.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		url=properties.getProperty("url");
		userName=properties.getProperty("userName");
		password=properties.getProperty("password");
		conn=DriverManager.getConnection(url,userName,password);
		return conn;
	}
	
	public static void CloseAll(Connection conn,PreparedStatement pstmt,ResultSet rs) throws SQLException{
		if(rs!=null){
			rs.close();
		}
		if(pstmt!=null){
			pstmt.close();
		}
		if(conn!=null){
			conn.close();
		}
		
	}
}

JDBCDao.java

package com.chinasofti.hospital.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.chinasofti.hospital.util.JDBCUtil;

public class JDBCDao {
	public static <T>List<T> queryAll(Class clazz,String sql,String...args) {
		Connection conn=null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		ResultSetMetaData ra = null;
		ArrayList<T> list = new ArrayList<>();
		Method[] methods = clazz.getDeclaredMethods();
		try {
			conn = JDBCUtil.getConn();
			pst = conn.prepareStatement(sql);
			for (int i = 0; i < args.length; i++) {
				pst.setString(i+1,args[i]);
			}
			rs = pst.executeQuery();
			ra = rs.getMetaData();
			int columnNum = ra.getColumnCount(); 
			while(rs.next()){
				T t = (T) clazz.newInstance();
				
				//list.add(u);
				for (int i = 1; i <= columnNum; i++) {
					for (Method method : methods) {
						if(method.getName().equalsIgnoreCase("set"+ra.getColumnName(i))){
							method.invoke(t, rs.getString(i));
						}
					}
				}
				list.add(t);
			}	
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		finally {
			try {
				JDBCUtil.CloseAll( conn,pst,rs);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
}

PageHelper.java

package com.chinasofti.hospital.util;

public class PageHelper {
	private static final int PAGE_COUNT=6;
	private int start;
	private int end;
	private int page;
	private int count;
	private int endPage;
	public PageHelper(int page,int count) {
		this.page=page;
		this.count=count;
		end=page*PAGE_COUNT;
		start=end-(PAGE_COUNT-1);
		endPage=count/PAGE_COUNT;
		if (count%PAGE_COUNT!=0) {
			endPage++;
		}
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getEnd() {
		return end;
	}
	public void setEnd(int end) {
		this.end = end;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
}


db.properties

url=jdbc:oracle:thin:@localhost:1521:xe
userName=scott
password=tiger
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值