Created
July 7, 2017 03:52
-
-
Save jahentao/783fc5e069d484a52f522728d570ddbc to your computer and use it in GitHub Desktop.
Java-DB-操作使用代码段
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 尝试抽象更为一般的 | |
* TODO | |
* select中用反射实现属性的自动装配 | |
*/ | |
import java.beans.BeanInfo; | |
import java.beans.Introspector; | |
import java.beans.PropertyDescriptor; | |
import java.lang.reflect.Constructor; | |
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.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Properties; | |
import com.mchange.v2.c3p0.ComboPooledDataSource; | |
/** | |
* JDBC工具类 | |
* @author jahen | |
* @date 2016年9月22日 | |
* @time 下午4:33:21 | |
* @category JDBC Utils | |
*/ | |
public class JDBCUtils { | |
private static String url; // jdbc URL | |
private static String user; // 用户名 | |
private static String passwd; // 密码 | |
static ComboPooledDataSource ds; // 数据库连接池 | |
//类加载时,注册驱动 | |
static{ | |
try { | |
Properties properties = new Properties(); | |
properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("c3p0.properties")); | |
String driverClass = properties.getProperty("c3p0.driverClass"); | |
url = properties.getProperty("c3p0.url"); | |
user = properties.getProperty("c3p0.user"); | |
passwd = properties.getProperty("c3p0.password"); | |
ds = new ComboPooledDataSource(); | |
ds.setDriverClass(driverClass); | |
ds.setJdbcUrl(url); | |
ds.setUser(user); | |
ds.setPassword(passwd); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* 获取连接 | |
* TODO 设计一个简单的数据库连接池,提高效率. 池化模式 | |
*/ | |
public static Connection getConnection() { | |
Connection conn = null; | |
try { | |
// conn = DriverManager.getConnection(url, user, passwd); | |
conn = ds.getConnection(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
return conn; | |
} | |
/** | |
* 添加记录, sql 含占位符 | |
* 与update相同 | |
*/ | |
public static boolean insert(Connection conn, String sql,Object[] params) { | |
return update(conn, sql, params); | |
} | |
/** | |
* 更新数据库中特定字段,sql 含占位符 | |
*/ | |
public static boolean update(Connection conn, String sql,Object[] params){ | |
boolean result=false; | |
PreparedStatement ps=null; | |
if(conn!=null){ | |
try { | |
ps=conn.prepareStatement(sql); | |
for(int i=0;params!=null&&i<params.length;i++){ | |
ps.setObject(i+1, params[i]); | |
} | |
int n=ps.executeUpdate(); | |
if(n>0){ | |
result=true; | |
} | |
//释放资源 | |
ps.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
return result; | |
} | |
/** | |
* 删除一条记录,根据id,sql 含占位符 | |
*/ | |
public static boolean delete(Connection conn, String sql, Integer id) { | |
boolean result=false; | |
PreparedStatement ps=null; | |
if(conn!=null){ | |
try { | |
ps=conn.prepareStatement(sql); | |
ps.setInt(1, id); | |
int n=ps.executeUpdate(); | |
if(n>0){ | |
result=true; | |
} | |
//释放资源 | |
ps.close(); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
return result; | |
} | |
/** | |
* 执行查询语句,返回查询的对象集合 | |
* TODO 要做到更加一般性,得解决对象如何装配? | |
* 假定数据库中的字段名与对象中属性名为相同的映射,且数据库中的字段集合包含属性集合,在此假设上尝试用反射实现。 | |
*/ | |
@SuppressWarnings("unchecked") | |
public static <T> List<T> select(Connection conn, String sql, T t, Object...params) { | |
List<T> list = new ArrayList<T>(); | |
try { | |
PreparedStatement pstd = conn.prepareStatement(sql); | |
for(int i=0;params!=null&&i<params.length;i++){ | |
pstd.setObject(i+1, params[i]); | |
} | |
ResultSet resultSet = pstd.executeQuery(); | |
ResultSetMetaData metaData = resultSet.getMetaData(); // 获得元数据 | |
int count = metaData.getColumnCount(); | |
Constructor<? extends Object> constructor = t.getClass().getConstructor(); // 获得构造方法 | |
// 做好属性名String,映射属性描述符PropertyDescriptor | |
BeanInfo info = Introspector.getBeanInfo(t.getClass()); | |
PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); | |
Map<String, PropertyDescriptor> map = new HashMap<String, PropertyDescriptor>(); | |
for(PropertyDescriptor descriptor : descriptors) { | |
String name = descriptor.getName(); | |
if(!"class".equals(name)) | |
map.put(name, descriptor); | |
} | |
// 装配对象 | |
while(resultSet.next()) { | |
T object = (T) constructor.newInstance(); // 获得对象实例 | |
for(int i=1; i<=count; i++) { | |
String label = metaData.getColumnLabel(i); // 获得字段名 | |
PropertyDescriptor descriptor = map.get(label); // 更加字段名获得相应的方法 | |
if(descriptor==null) | |
continue; | |
Method writeMethod = descriptor.getWriteMethod(); | |
writeMethod.invoke(object, resultSet.getObject(i)); // 设置对象属性 | |
} | |
list.add(object); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return list; | |
} | |
/** | |
* 关闭连接,释放资源 | |
*/ | |
public static void closeConnection(Connection conn) { | |
try { | |
if(conn!=null && !conn.isClosed()){ | |
conn.close(); | |
} | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment