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

被折叠的 条评论
为什么被折叠?



