简易学生管理系统的工具类——DButil,PropertiesUtil

本博客之中所写工具类的作用会在下一篇关于学生管理系统(https://blog.csdn.net/qq_39676433/article/details/99829543)中阐述。

 首先我创建了一个接口——IRowMapper

package newjdbc;

import java.sql.ResultSet;

public interface IRowMapper {
	void rowMapper(ResultSet rs);
}

其次是工具类PropertiesUtil—— 

package newjdbc;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {

		static Properties properties=new Properties();
		
		static{
			InputStream inputStream = PropertiesUtil.class.getClassLoader().getResourceAsStream("db.properties");
			try {
				( properties).load(inputStream);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		public static String getValue(String key) {
			return properties.getProperty(key);
		}
}

 然后是工具类DButil——

package newjdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 * 学生信息管理系统的工具类
 *
 * @author Sommerfeld-zlk
 */
public class DButil{

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 建立数据库连接
	 *
	 * @author Sommerfeld-zlk
	 */
	public static Connection getconnection() {
		try {
			String url=PropertiesUtil.getValue("jdbc.url");
			String name=PropertiesUtil.getValue("jdbc.name");
			String password=PropertiesUtil.getValue("jdbc.password");
			Connection connection = DriverManager.getConnection(url, name, password);
			return connection;
		}catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 判断数据是否存在
	 *
	 * @author Sommerfeld-zlk
	 */
	public static boolean exist(String sql) {
		class RowMapper implements IRowMapper {
			boolean state;

			@Override
			public void rowMapper(ResultSet rs) {
				try {
					state = rs.next();

				} catch (SQLException e) {

					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		Select(sql, rowMapper);
		return rowMapper.state;
	}
	
	/**
	 * 判断数据是否存在
	 *
	 * @author Sommerfeld-zlk
	 */
	public static boolean exist(String sql, String... list) {
		class RowMapper implements IRowMapper {
			boolean state;
			@Override
			public void rowMapper(ResultSet rs) {
				try {
					state = rs.next();

				} catch (SQLException e) {

					e.printStackTrace();
				}
			}
		}
		RowMapper rowMapper = new RowMapper();
		Select(sql, rowMapper, list);
		return rowMapper.state;
	}

	/**
	 * 查找数据
	 *
	 * @author Sommerfeld-zlk
	 */
	public static void select(String sql, IRowMapper rowMapper) {
		Connection connection = null;
		Statement statement = null;
		ResultSet rs = null;
		try {
			connection = getconnection();
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			rowMapper.rowMapper(rs);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			finalclose(connection, statement, rs);
		}
	}
	
	/**
	 * 查找数据
	 *
	 * @author Sommerfeld-zlk
	 */
	public static void Select(String sql, IRowMapper RowMapper, Object... list) {

		Connection connection = null;
		PreparedStatement preparedStatement= null;
		ResultSet resultset = null;
		try {
			connection = getconnection();
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= list.length; i++) {
				preparedStatement.setObject(i, list[i-1]);
			}
			resultset = preparedStatement.executeQuery();
			RowMapper.rowMapper(resultset);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			finalclose(connection, preparedStatement, resultset);
		}
	}

	/**
	 *更新数据
	 *
	 * @author Sommerfeld-zlk
	 */
	public static boolean update(String sql) {
		Connection connection = null;
		Statement statement = null;
		try {
			connection = getconnection();
			statement = connection.createStatement();
			int result = statement.executeUpdate(sql);
			return result > 0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			finalclose(connection, statement);
		}
		return false;
	}

	/**
	 *更新数据
	 *
	 * @author Sommerfeld-zlk
	 */
	public static boolean update(String sql, Object... list) {// 这个方法没写完,接着写
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		
		try {
			connection = getconnection();
			preparedStatement = connection.prepareStatement(sql);
			for (int i = 1; i <= list.length; i++) {
				preparedStatement.setObject(i , list[i-1]);
			}
			int result = preparedStatement.executeUpdate();
			return result>0;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			finalclose(connection, preparedStatement);
		}
		return false;
	}
	
	/**
	 * 
	 *批量处理
	 * @author Sommerfeld-zlk
	 */
	public static boolean Batch(String...sqls) {
		Connection connection=null;
		Statement statement=null;
		try {
			connection=getconnection();
			connection.setAutoCommit(false);
			statement=connection.createStatement();
			for (String string : sqls) {
				statement.addBatch(string);
			}
			statement.executeBatch();
			connection.commit();
			return true;
		 }catch (Exception e) {
				e.printStackTrace();
				if (connection!=null) {
					try {
						connection.rollback();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
		}finally {
			finalclose(connection, statement);
		}
		return false;
	}
	/**
	 * 批量处理,只能输入固定的sql语句模板
	 *
	 * @author Sommerfeld-zlk
	 */
	public static boolean Batch(String sql,Object[]...objects) {
		Connection connection=null;
		PreparedStatement statement=null;
		try {
			connection=getconnection();
			connection.setAutoCommit(false);
			statement= connection.prepareStatement(sql);
			for (int i = 0; i < objects.length; i++) {
				for (int j = 1; j <=objects[i].length; j++) {
					statement.setObject(j, objects[i][j-1]);
				}
				statement.addBatch();
			}
			statement.executeBatch();
			connection.commit();
			return true;
		 }catch (Exception e) {
				e.printStackTrace();
				if (connection!=null) {
					try {
						connection.rollback();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
		}finally {
			finalclose(connection,statement);
		}
		return false;
	}
	
	/**
	 *释放资源
	 *
	 * @author Sommerfeld-zlk
	 */
	public static void finalclose(Connection connection, Statement statement) {
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 *释放资源
	 *
	 * @author Sommerfeld-zlk
	 */
	public static void finalclose(Connection connection, Statement statement, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (statement != null) {
			try {
				statement.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值