jdbcUtil封装的工具类

public class JdbcUtil{
//提取参信息--方便以后进行封装
	private static String driver = "";
	private static String url = "";
	private static String username = "";
	private static String userpwd = "";
	
	//加载类的时候自动执行--静态代码块
	static{
		//通过流的方式来解析配置文件的内容
		//创建properties对象
		Properties properties = new Properties();
		try {
			//用properties对象读取配置文件,这里需要用当前类的class事例对象调用类加载器加载二进制流对象
                        资源作为 
                        properties.load(JdbcUtil.class.getClassLoader().getResourceAsStream("Bjsxtjdbc.
                        properties"));
			//获取配置参数信息,通过properties对象的getProperty()方法,这里参数中的jdbcname是对应配置文
                       //件中的属性前缀
                        String jdbcname = properties.getProperty("jdbcname");
			driver = properties.getProperty(jdbcname+"driver");
			url = properties.getProperty(jdbcname+"url");
			username = properties.getProperty(jdbcname+"username");
			userpwd = properties.getProperty(jdbcname+"userpwd");	
			System.out.println("JdbcUtis.enclosing_method():"+"[driver]"+driver+"[url]"+url+"[username]"+username+"[userpwd]"+userpwd);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//创建连接
	public static Connection getConnection(){
		//声明连接
		Connection connection = null;
		try {
			//加载驱动
			Class.forName(driver);
			//创建连接
			connection = DriverManager.getConnection(url, username, userpwd);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("JdbcUtil.getConnection(请检查连接参数):"+"[url]:"+url+" [username]:"+username+" [userpwd]:"+userpwd);
			e.printStackTrace();
		}
		return connection;
	}
	
	//创建sql命令发生器preparedStatement
	public static PreparedStatement getPreparedStatement(Connection connection,String sql){
		//声明sql命令发送器对象
		PreparedStatement preparedStatement = null;
		try {
			preparedStatement = connection.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return preparedStatement;
	}
	
	//创建statement命令发生器
	public static Statement getStatement(Connection connection){
		//声明sql命令发送器对象
		Statement statement = null;
		try {
			statement = connection.createStatement();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return statement;
	}
	
	//关闭资源连接 connection,statement,resultset
	public static void closeAll(ResultSet resultSet,Statement statement,Connection connection){
		//后开的先关, 判断是否为空
		try {
			if(resultSet!=null){
				resultSet.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			if(statement!=null){
				statement.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			if(connection!=null){
				connection.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	//封装DML语句
	public static int excuteDML(String sql,Object...objs){
		//声明一个变量
		int n = 0;
		//声明连接
		Connection connection = null;
		//声明sql命令发送器
		PreparedStatement preparedStatement = null;		
		try {
			//创建连接
			connection = getConnection();
			preparedStatement = getPreparedStatement(connection, sql);
			//赋值
			for (int i = 0; i < objs.length; i++) {
				preparedStatement.setObject(i+1, objs[i]);
			}
			//发送sql
			n = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//关闭连接
			closeAll(null,preparedStatement,connection);
		}
		return n;
	}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值