package com.yuyi.dbutil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.yuyi.bean.User;

public class DBUtils {

	private static final String DRIVER = "com.mysql.jdbc.Driver";
	private static final String USER = "root";
	private static final String PASSWORD = "628081";
	private static final String URL = "jdbc:mysql:///user";

	static {
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private DBUtils() {
		// TODO Auto-generated constructor stub
	}

	private static DBUtils dbUtils = new DBUtils();

	public static DBUtils getInstance() {
		return dbUtils;
	}

	public Connection getConnection() throws SQLException {
		return DriverManager.getConnection(URL, USER, PASSWORD);
	}

	public List<User> executeQueryUser(String sql, Object... objects) {
		List<User> list = new ArrayList<User>();
		Connection con = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		try {
			con = getConnection();
			pstat = con.prepareStatement(sql);
			if (objects != null)
				for (int i = 0; i < objects.length; i++) {
					pstat.setObject(i + 1, objects[i]);
				}
			rs = pstat.executeQuery();
			while (rs.next()) {
				Integer id=rs.getInt("id");
				String firstName=rs.getString("firstname");
				String lastName=rs.getString("lastname");
				String phone=rs.getString("phone");
				String email=rs.getString("email");
				User user=new User(id, firstName, lastName, phone, email);
				list.add(user);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(con, pstat, rs);
		}
		return list;
	}

	public int executeUpdate(String sql, Object... objects) {
		int num = 0;
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = getConnection();
			ps = con.prepareStatement(sql);
			if (objects != null) {
				for (int i = 0; i < objects.length; i++) {
					ps.setObject(i + 1, objects[i]);
				}
				num = ps.executeUpdate();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			close(con, ps, null);
		}
		return num;
	}

	public void close(Connection con, PreparedStatement ps, ResultSet rs) {
		if (con != null) {
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
最近下载更多
billzw  LV6 2022年2月13日
于你而言  LV1 2021年5月31日
酒后少年  LV6 2021年5月21日
永远知音  LV18 2021年5月16日
htwang  LV3 2021年4月30日
ly_fxm  LV4 2021年1月24日
532612853  LV1 2020年9月5日
厉害了你昱哥  LV17 2020年7月24日
23456lll  LV5 2020年1月5日
wyll123456  LV2 2019年12月30日
最近浏览更多
t2034286  LV1 1月4日
fff2003  LV6 2023年11月20日
无名氏111  LV32 2023年11月13日
张朕朕  LV3 2023年9月8日
asadda  LV2 2023年6月27日
milou123  LV2 2023年3月1日
小逸夜  LV4 2022年12月29日
swl137985246  LV7 2022年6月16日
charleswang  LV7 2022年6月15日
billzw  LV6 2022年2月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友