个人JDBC封装工具类DButil

package ZK3;

import java.sql.*;

/**
 * 
 * @author 陶宇
 * .数据操作工具类
 */

public class DButil {
    // 加载mysql数据库驱动
	static ResultSet rs;
	static PreparedStatement ps;
	static Connection c;
	static String s1="jdbc:mysql:///";
	// 用于指定时区
	static String s3="?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT";
	static String url="";
	//用于指定对应数据库
	public static void  setDB(String s2) {
		url=s1+s2+s3;
	}
	static {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	// 获得指定数据库连接
	public static Connection joindb() {
		c=null;
		try {
			c = DriverManager.getConnection(url,"root","123456");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return c;
	}
    // 关流  带有返回结果集ResultSet的
	public static void closeAll(ResultSet rs,Statement s,Connection c) {
		try {
			rs.close();
		} catch (SQLException e) {
			System.out.println("ResultSet 关闭异常");
			e.printStackTrace();
		}
		try {
			s.close();
		} catch (SQLException e) {
			System.out.println("Statement 关闭异常");
			e.printStackTrace();
		}
		try {
			c.close();
		} catch (SQLException e) {
			System.out.println("Connection 关闭异常");
			e.printStackTrace();
		}
	}
	// 关流 不带返回结果集ResultSet的
	public static void closeAll(Statement s,Connection c) {
		try {
			s.close();
		} catch (SQLException e) {
			System.out.println("Statement 关闭异常");
			e.printStackTrace();
		}
		try {
			c.close();
		} catch (SQLException e) {
			System.out.println("Connection 关闭异常");
			e.printStackTrace();
		}
	}
	//无参全部关闭
	public static void closeAll() {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				System.out.println("ResultSet 关闭异常");
				e.printStackTrace();
			}
		}
		if(ps!=null) {
			try {
				ps.close();
			} catch (SQLException e) {
				System.out.println("Statement 关闭异常");
				e.printStackTrace();
			}
		}
		try {
			c.close();
		} catch (SQLException e) {
			System.out.println("Connection 关闭异常");
			e.printStackTrace();
		}
	}
	// 封装select 方法 使用PreparedStatement() Sql预处理技术
	public static ResultSet select(String sql,Object...param) {
		c=joindb();
		try {
			ps =c.prepareStatement(sql);
			for(int i=0;i<param.length;i++) {
				 ps.setObject((i+1),param[i]);
			}
			ResultSet R = ps.executeQuery();
			R.next();
			System.out.println(R.getString("id"));
			return R;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DButil.closeAll();
		return null;
	}
	// 封装 增、删、改 方法  Sql预处理技术
	public static int update(String sql,Object ...All) {
		c=joindb();
		try {
			ps=c.prepareStatement(sql);
			for(int i=0;i<All.length;i++) {
				ps.setObject(i+1, All[i]);
			}
			int row = ps.executeUpdate();
			if(row>0) {
				System.out.println("成功");
				return 1;
			}else {
				System.out.println("失败");
				return 0;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	// 测试用的main方法
	public static void main(String[] args) {
		
	}
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值