Java-JDBC-工具类

5 篇文章 0 订阅
1 篇文章 0 订阅

Java-JDBC-工具类

首先导入需要加载的数据库驱动包
这里我使用mysql测试
我这里是根据: 一起喜羊羊的工具类所改

/*
*创建类jdbcUtil作为工具类
*/
public class jdbcUtil  {
	/*
	 * 静态块驱动加载 mysql驱动包
	 * */
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 * 创建连接对象
	 */
	public static Connection getConnection() throws SQLException {
	    //stuDB数据库名  
	    //useUnicode=true&characterEncoding=utf8设置编码格式  
	    String url="jdbc:mysql://localhost:3306/stuDB?useUnicode=true&characterEncoding=utf8";
	    String user="root";
	    String password="123456";
	    //url 连接路径  user 连接名 password 连接密码
		return DriverManager.getConnection(url, user, password);
	}
	
	/*
	 * 关闭增删改操作的连接对象
	 */
	public static void Close(Connection coon,PreparedStatement stmt) {
		if (stmt!=null) {
			try {
				stmt.close();
				coon.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/*
	 * 关闭读取连接对象
	 */
	public static void Close(Connection coon,PreparedStatement stmt,ResultSet rs) {
		if (rs!=null) {
			try {
				rs.close();
				stmt.close();
				coon.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/*
	 * 增删改操作的方法
	 * String sql 执行增删改的SQL语句
	 * Object[] parems 操作参数
	 */
	public static int NonQuery(String sql,Object[] parems) {
		Connection coon = null;
		PreparedStatement stmt = null;
		int rowCount = 0;
		try {
			coon = getConnection();
			stmt = coon.prepareStatement(sql);
			if (parems!=null&&parems.length>0) {
				for (int i = 0; i < parems.length; i++) {
					stmt.setObject(i+1, parems[i]);
				}
			}
			rowCount = stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			Close(coon,stmt);
		}
		return rowCount;
	}
	
	/*
	 * 泛型查询
	 * String sql 执行查询的SQL语句
	 * Object[] parems 操作参数
	 * RowsMapper<T> rm 自定义泛型类 字段读取接口 --获取实体类信息
	 */
	public static<T> ArrayList<T> GenericQeruy(String sql,Object[] parems,RowsMapper<T> rm){
		Connection coon = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		ArrayList<T> list = new ArrayList<T>();
		try {
			coon = getConnection();
			stmt = coon.prepareStatement(sql);
			if (parems!=null&&parems.length>0) {
				for (int i = 0; i < parems.length; i++) {
					stmt.setObject(i+1, parems[i]);
				}
			}
			rs = stmt.executeQuery();
			while(rs.next()) {
				T t = rm.getEntity(rs);
				list.add(t);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			Close(coon, stmt, rs);
		}
		return list;
	}
	/*
	 定义泛型接口
	 public interface RowsMapper<T> {
	     public T getEntity(ResultSet rs) throws SQLException;
     }
	*/
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿࿆杰࿆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值