当前位置:Gxlcms > mysql > java(2014版)连接数据库的工具类

java(2014版)连接数据库的工具类

时间:2021-07-01 10:21:17 帮助过:14人阅读

package util;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Properties;public class JdbcUtil {pr

package util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtil {
	private static Connection conn;
	private static PreparedStatement pstmt;
	private static ResultSet rs;

	private JdbcUtil() {
		super();
		// TODO Auto-generated constructor stub
	}

	// 连接数据库的操作
	public static Connection getConn() {
		if (conn == null) {
			// 加载属性配置文件
			// 创建属性对象
			Properties prop = new Properties();
			try {
				// 加载指定名称的属性文件
				prop.load(JdbcUtil.class.getClassLoader().getResourceAsStream(
						"jdbc.properties"));
				try {
					// 根据文件的名称查找类文件
					Class.forName(prop.getProperty("driver"));

					// 创建连接对象
					try {
						conn = DriverManager.getConnection(
								prop.getProperty("url"),
								prop.getProperty("user"),
								prop.getProperty("pass"));
						System.out.println("连接数据库连接成功");

					} catch (SQLException e) {
						// TODO Auto-generated catch block
						System.out.println("连接数据库失败");
						e.printStackTrace();
					}
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					System.out
							.println("没有" + prop.getProperty("driver") + "文件");
					e.printStackTrace();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("属性文件加载出错");
				e.printStackTrace();
			}
		}
		return conn;
	}

	public static void release(ResultSet rs, PreparedStatement pstmt) {
		// 释放结果集
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// 释放准备语句
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	// 更新数据的操作增,删,改要用到的封装方法
	public static boolean upDate(String sql, Object[] obj) {
		boolean flag = false;

		try {
			// 准备语句的创建,带有sql命令的对象
			pstmt = getConn().prepareStatement(sql);

			for (int i = 1; i <= obj.length; i++) {
				pstmt.setObject(i, obj[i - 1]);
			}
			int i = pstmt.executeUpdate();
			if (i > 0) {
				flag = true;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			release(rs, pstmt);
		}
		return flag;
	}

	// 进行批量删除处理
	public static boolean updateBatchDel(String sql, Object[] ids) {
		boolean flag = false;
		Connection conn = getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			conn.setAutoCommit(false);
			pstmt = conn.prepareStatement(sql);
			for (int i = 0; i < ids.length; i++) {
				pstmt.setObject(1, ids[i]);
				System.out.println(sql + "---------------" + ids[i]);
				pstmt.addBatch();
			}
			int[] num = pstmt.executeBatch(); // 批量执行
			for (int i = 0; i < num.length; i++) {
				if (num[i] == 0) {
					try {
						conn.rollback(); // 进行事务回滚
						return flag;
					} catch (SQLException ex) {
						ex.printStackTrace();
					}
				}
			}
			conn.commit();// 提交事务
			flag = true;
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			release(rs, pstmt);
		}
		return flag;
	}

	// 根据传入的表的名称,和每页数据得到传入表的所有的页数
		// tableName:::::操作的数据表名称
		// pagesize::::::每页显示的信息条数
	
		public static Integer getCountPage(String tableName, Integer pagesize) {
			Integer countPage = 0;
			String sql = "select count(*) as c from " + tableName;
			Connection conn = JdbcUtil.getConn();
			PreparedStatement pstmt = null;
			ResultSet rs = null;
			conn = JdbcUtil.getConn();
			try {
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if (rs.next()) {
					int countRecord = rs.getInt("c");
					countPage = countRecord % pagesize == 0 ? countRecord
							/ pagesize : countRecord / pagesize + 1;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				JdbcUtil.release(rs, pstmt);
			}
			return countPage;
		}
	
	
}

人气教程排行