原生jdbc複習

	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());
				}
			}
		}
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章