⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fooditemdao.java

📁 用jdbc插件开发开发liferay-portal
💻 JAVA
字号:
/**
 * Copyright (c) 2000-2006 Liferay, LLC. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.sample.dao.model;

import com.sample.dao.util.ConnectionPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;

/**
 * <a href="FoodItemDAO.java.html"><b><i>View Source</i></b></a>
 *
 * @author  Brian Wing Shun Chan
 *
 */
public class FoodItemDAO {

	public static void addFoodItem(FoodItem foodItem) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;

		try {
			con = ConnectionPool.getConnection();

			ps = con.prepareStatement(_ADD_FOOD_ITEM);

			ps.setString(1, foodItem.getName());
			ps.setInt(2, foodItem.getPoints());

			ps.executeUpdate();
		}
		finally {
			ConnectionPool.cleanUp(con, ps);
		}
	}

	public static void deleteFoodItem(int id) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;

		try {
			con = ConnectionPool.getConnection();

			ps = con.prepareStatement(_DELETE_FOOD_ITEM);

			ps.setInt(1, id);

			ps.executeUpdate();
		}
		finally {
			ConnectionPool.cleanUp(con, ps);
		}
	}

	public static FoodItem getFoodItem(int id) throws SQLException {
		FoodItem foodItem = null;

		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			con = ConnectionPool.getConnection();

			ps = con.prepareStatement(_GET_FOOD_ITEM);

			ps.setInt(1, id);

			rs = ps.executeQuery();

			if (rs.next()) {
				foodItem = new FoodItem();

				foodItem.setId(id);
				foodItem.setName(rs.getString(2));
				foodItem.setPoints(rs.getInt(3));
			}
		}
		finally {
			ConnectionPool.cleanUp(con, ps, rs);
		}

		return foodItem;
	}

	public static List getFoodItems() throws SQLException {
		List list = new ArrayList();

		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			con = ConnectionPool.getConnection();

			ps = con.prepareStatement(_GET_FOOD_ITEMS);

			rs = ps.executeQuery();

			while (rs.next()) {
				FoodItem foodItem = new FoodItem();

				foodItem.setId(rs.getInt(1));
				foodItem.setName(rs.getString(2));
				foodItem.setPoints(rs.getInt(3));

				list.add(foodItem);
			}
		}
		finally {
			ConnectionPool.cleanUp(con, ps, rs);
		}

		return list;
	}

	public static void updateFoodItem(FoodItem foodItem) throws SQLException {
		Connection con = null;
		PreparedStatement ps = null;

		try {
			con = ConnectionPool.getConnection();

			ps = con.prepareStatement(_UPDATE_FOOD_ITEM);

			ps.setString(1, foodItem.getName());
			ps.setInt(2, foodItem.getPoints());
			ps.setInt(3, foodItem.getId());

			ps.executeUpdate();
		}
		finally {
			ConnectionPool.cleanUp(con, ps);
		}
	}

	private static final String _ADD_FOOD_ITEM =
		"INSERT INTO FoodItem (name, points) VALUES (?, ?)";

	private static final String _DELETE_FOOD_ITEM =
		"DELETE FROM FoodItem WHERE id = ?";

	private static final String _GET_FOOD_ITEM =
		"SELECT id, name, points FROM FoodItem WHERE id = ?";

	private static final String _GET_FOOD_ITEMS =
		"SELECT id, name, points FROM FoodItem";

	private static final String _UPDATE_FOOD_ITEM =
		"UPDATE FoodItem SET name = ?, points = ? WHERE id = ?";

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -