导入jar包,buildPath
在项目的根目录下创建lib文件夹,把.jar文件复制粘贴到lib中,右击buildpath.
配置文件
DBPro
public class DBPro {
public static final String DRIVER = "driver";
public static final String URL = "url";
public static final String UNAME = "uname";
public static final String PWD = "pwd";
}
DBUtil
import java.io.FileInputStream;
import java.io.IOException;
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 DBUtil {
private static Properties pro = null;
private static String driver = DBPro.DRIVER;
private static String url = DBPro.URL;
private static String uname = DBPro.UNAME;
private static String pwd = DBPro.PWD;
static {
try {
//通过pro加载
pro = new Properties();
InputStream is = new FileInputStream("src/DBConfig.properties");
pro.load(is);
driver = getValue(driver);
url = getValue(url);
uname = getValue(uname);
pwd = getValue(pwd);
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConn() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, uname, pwd);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement getStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
private static void CloseConn(Connection conn) {
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void CloseStmt(Statement stmt) {
if(stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private static void CloseRs(ResultSet rs) {
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void CloseAll(Connection conn,Statement stmt,ResultSet rs) {
CloseConn(conn);
CloseStmt(stmt);
CloseRs(rs);
}
public static String getValue(String key) {
if("".equals(key)||key==null) {
throw new RuntimeException("传入的key有误"+key+"===");
}
return pro.getProperty(key);
}
}
BaseDap
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BaseDao {
public static <T> T queryOne(String sql,Class<T> clz,Object...objs) {
return queryAll(sql,clz,objs).get(0);
}
public static <T> List<T> queryAll(String sql,Class<T> clz,Object...objs){
List<T> ls = new ArrayList<>();
//连接声明
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
conn = DBUtil.getConn();
try {
pstmt = conn.prepareStatement(sql);
if(null==objs) {
throw new RuntimeException("参数有误");
}
//设置绑定变量
for(int i =0;i<objs.length;i++) {
pstmt.setObject(i+1,objs[i]);
}
//发送sql
rs = pstmt.executeQuery();
while(rs.next()) {
T t = getObject(rs,clz);
ls.add(t);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.CloseAll(conn, pstmt, rs);
}
return ls;
}
private static <T> T getObject(ResultSet rs,Class<T> clz) {
T t = null;
//获取对象
try {
t = clz.newInstance();
Field[] fs = clz.getDeclaredFields();
for(Field f:fs) {
f.setAccessible(true);
try {
f.set(t, rs.getObject(f.getName()));
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
f.setAccessible(false);
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return t;
}
}