发布于2020-11-19 20:51 阅读(981) 评论(0) 点赞(8) 收藏(1)
package jdbcutil;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
public class JdbcUtil {
static DataSource ds = null;
static {
Properties p = new Properties();
InputStream in = null;
ds = null;
try {
in = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties");
p.load(in);
ds = DruidDataSourceFactory.createDataSource(p);
} catch (Exception e1) {
e1.printStackTrace();
}
}
public static Connection getConn() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public static void close(ResultSet rset, PreparedStatement pstmt, Connection conn) {
try {
if (rset != null)
rset.close();
if (pstmt != null)
pstmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package jdbcutil;
import com.handler.IResultSetHandler;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Execute {
public int executeUpdata(String sql, Object... params) {
Connection conn = JdbcUtil.getConn();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++)
pstmt.setObject(i + 1, params[i]);
return pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.close(null, pstmt, conn);
}
return 0;
}
public <T> T executeQuery(String sql, IResultSetHandler<T> rh, Object... params) {
Connection conn = JdbcUtil.getConn();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < params.length; i++)
pstmt.setObject(i + 1, params[i]);
rs = pstmt.executeQuery();
return (T)rh.handle(rs);
} catch (Exception e) {
e.printStackTrace();
} finally {
JdbcUtil.close(rs, pstmt, conn);
}
return null;
}
}
package com.handler;
import java.sql.ResultSet;
public class BeanCountHandler implements IResultSetHandler<Integer> {
public Integer handle(ResultSet rs) throws Exception {
if (rs.next())
return Integer.valueOf(rs.getInt(1));
return null;
}
}
package com.handler;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.sql.ResultSet;
public class BeanHandler<T> implements IResultSetHandler<T> {
private Class<T> classType;
public BeanHandler(Class<T> classType) {
this.classType = classType;
}
public T handle(ResultSet rs) throws Exception {
if (rs.next()) {
T obj = this.classType.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(this.classType, Object.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
byte b;
int i;
PropertyDescriptor[] arrayOfPropertyDescriptor1;
for (i = (arrayOfPropertyDescriptor1 = pds).length, b = 0; b < i; ) {
PropertyDescriptor pd = arrayOfPropertyDescriptor1[b];
Object val = rs.getObject(pd.getName());
pd.getWriteMethod().invoke(obj, new Object[] { val });
b++;
}
return obj;
}
return null;
}
}
package com.handler;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class BeanListHandler<T> implements IResultSetHandler<List<T>> {
private Class<T> classType;
public BeanListHandler(Class<T> classType) {
this.classType = classType;
}
public List<T> handle(ResultSet rs) throws Exception {
List<T> list = new ArrayList<T>();
while (rs.next()) {
T obj = this.classType.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(this.classType, Object.class);
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
byte b;
int i;
PropertyDescriptor[] arrayOfPropertyDescriptor1;
for (i = (arrayOfPropertyDescriptor1 = pds).length, b = 0; b < i; ) {
PropertyDescriptor pd = arrayOfPropertyDescriptor1[b];
Object val = rs.getObject(pd.getName());
pd.getWriteMethod().invoke(obj, new Object[] { val });
b++;
}
list.add(obj);
}
return list;
}
}
package com.handler;
import java.sql.ResultSet;
public interface IResultSetHandler<T> {
T handle(ResultSet paramResultSet) throws Exception;
}
至此已经完成,打个jar包 和jdbc jar包一起导入就可以用了
链接:[https://share.weiyun.com/sWGwc44z]密码:999999
导入后需要在你的项目里创建resource 文件夹以及里面的 db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc\:mysql\://localhost\:3306/test
username=root
password=root
/*
Execute类中有两个方法
1.executeQuery(); 执行查询操作
2.executeUpdata(); 执行修改操作
而里面的参数是可变的 具体根据自己的情况传参
结果集处理器有三种
public 返回结果类型 方法名(参数类型) {
String sql = "这里写要执行的sql语句";
return executeQuery(sql, 结果集处理器,
这里是sql语句里?对应的值);
}
作者:java战神
链接:http://www.javaheidong.com/blog/article/893/19bf8802d6edf056a97e/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!