欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

原生jdbc复习

程序员文章站 2024-03-26 11:03:05
...
	public List<Order> testQuery(String openid, int page)  {
		Connection con = null;
		try {
			//获取一个数据库,连接,实现方式略,可以使用spring生成
			con = ConnectionFactory.getInstance().getConnection(Constant.DATABASE_DRAGON);
			//执行查询,拼装参数
			PreparedStatement pstmt = con.prepareStatement("select id, openid, billno, name from orders where openid=?  order by id desc limit ?,20");
			pstmt.setString(1, openid);
			pstmt.setInt(2, (page - 1) * 20);
			
			List<Order> orders = new ArrayList<Order>();
			ResultSet rs = pstmt.executeQuery();
			//获取结果集
			while (rs.next()) {
				Order order = new Order(rs.getLong("id"), rs.getString("openid"), rs.getString("billno"), rs.getString("name"));
				orders.add(order);
			}
			rs.close();
			pstmt.close();
			return orders;
		} catch (Exception e) {
			logger.error("", e);
			return null;
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (Exception e) {
					logger.error("", e);
				}
			}
		}
	}
	
	public boolean testInsert(PayInfo payInfo)  {
		Connection con = null;
		try {
			//获取一个数据库连接,比较简单,过程略
			con = ConnectionFactory.getInstance().getConnection(Constant.DATABASE_DRAGON);
			PreparedStatement pstmt = con.prepareStatement("insert into orders (openid,billno, name, amount) values(?,?,?,?)");
			pstmt.setString(1, payInfo.getOpenid());
			pstmt.setString(2, payInfo.getBillno());
			pstmt.setString(3, payInfo.getItemname());
			pstmt.setInt(4, payInfo.getAmount());
			//执行查询
			pstmt.executeUpdate();
			return true;
		} catch (Exception e) {
			logger.error(e.toString(),e);
			return false;
		} finally {
			if (con != null) {
				try {
					con.close();
				} catch (Exception e) {
					logger.error(e.toString());
				}
			}
		}
	}