对JDBC的简易封装

这是我们需要进行封装的代码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		//1.加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//2.获取连接对象
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","123456");
		//3.获取statement对象
		Statement st = conn.createStatement();
		//4.查询
		ResultSet rs = st.executeQuery("select * from tb");
		//5.对结果遍历
		while(rs.next()) {
			System.out.println(rs.getString(1)+"~~~"+rs.getInt(2)+"~~~"+rs.getInt(3));
		}
		//6.关闭   rs  ,   st   ,   conn
		rs.close();
		st.close();
		conn.close();
		
	}
}

1.我们先将所需的变量以及对象变成成员变量:

private String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/demo";
	private static String password = "123456";
	private static String username = "root";
	private static Connection conn = null;
	private static Statement st = null;
	private static ResultSet re = null;

 2.将加载驱动定义为静态代码块。

static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

3.获取连接对象。

public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url,username,password);
	}

4.对数据库的添加、删除、修改操作进行封装。

public void update(String sql) {
		try {
			conn = getConnection();
			
			st = conn.createStatement();
			
			int bRet = st.executeUpdate(sql);
			
			System.out.println(bRet);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			jdbcClose(conn, st);
		}
	}

5.释放资源。

public static void jdbcClose(Connection conn,Statement st,ResultSet rs) {
		try {
			rs.close();
			if(rs != null) {
				rs = null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				st.close();
				if(st!=null) {
					st = null;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					conn.close();
					if(conn!=null) {
						conn = null;
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}
	public static void jdbcClose(Connection conn,Statement st) {
		try {
			st.close();
			if(st!=null) {
				st = null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				conn.close();
				if(conn!=null) {
					conn = null;
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值