JavaEE--JDBC完成操作

接着上一篇文章我们回顾了JDBC的基本操作以及sql攻击。现在我们使用JDBC完成增删改查操作。

工具类的抽取

首先看看我们以前写的version 1.0版的JDBC代码吧!!

public class JDBCUtils_V1 {

	/**
	 * 获取连接方法
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/web08", "root", "root");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}

通过注册驱动连接数据库得到connection对象,额外封装了释放对象的方法。是不是觉得不够高大上??对,如果我们现在要改变连接的数据库是不是我们要重新写数据库的路径了?不慌我们开始我们的version 2.0版的代码。

public class JDBCUtils_V2 {
	private static String driver;
	private static String url;
	private static String username;
	private static String password;
	
	/**
	 * 静态代码块加载配置文件信息
	 */
	static{
		ResourceBundle bundle = ResourceBundle.getBundle("db");
		driver = bundle.getString("driver");
		url = bundle.getString("url");
		username = bundle.getString("username");
		password = bundle.getString("password");
	}

	/**
	 * 获取连接方法
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}
}

没有多少变化是吧??我们把数据库的参数写到了一个文件里了,以后直接改动数据库的配置就好了!!工程文件的结构是这样的。

配置文件的内容: 

 driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8
username=root
password=123456

 最后我们来一波测试吧!!

public class TestUtils {
	/**
	 * 根据id更新用户信息方法
	 */
	@Test
	public void testUpdateById() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			// 1.获取连接
			conn = JDBCUtils_V3.getConnection();
			// 2.编写sql语句
			String sql = "update tbl_user set upassword=? where uid=?";
			// 3.获取执行sql语句对象
			pstmt = conn.prepareStatement(sql);
			// 4.设置参数
			pstmt.setString(1, "999");
			pstmt.setInt(2, 3);
			// 5.执行更新操作
			int row = pstmt.executeUpdate();
			if (row > 0) {
				System.out.println("更新成功!");
			} else {
				System.out.println("更新失败!");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			// 6.释放资源
			JDBCUtils_V3.release(conn, pstmt, null);
		}
	}

	/**
	 * 根据id删除信息方法
	 */
	@Test
	public void testDeleteById() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			// 1.获取连接
			conn = JDBCUtils_V3.getConnection();
			// 2.编写sql语句
			String sql = "delete from tbl_user where uid=?";
			// 3.获取执行sql语句对象
			pstmt = conn.prepareStatement(sql);
			// 4.设置参数
			pstmt.setInt(1, 4);
			// 5.执行删除操作
			int row = pstmt.executeUpdate();
			if (row > 0) {
				System.out.println("删除成功!");
			} else {
				System.out.println("删除失败!");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			// 6.释放资源
			JDBCUtils_V3.release(conn, pstmt, null);
		}
	}

	/**
	 * 添加用户信息方法
	 */
	@Test
	public void testAdd() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			// 1.获取连接
			conn = JDBCUtils_V2.getConnection();
			// 2.编写sql语句
			String sql = "insert into tbl_user values(null,?,?)";
			// 3.获取执行sql语句对象
			pstmt = conn.prepareStatement(sql);
			// 4.设置参数
			pstmt.setString(1, "lisi");
			pstmt.setString(2, "hehe");
			// 5.执行插入操作
			int row = pstmt.executeUpdate();
			if (row > 0) {
				System.out.println("添加成功!");
			} else {
				System.out.println("添加失败!");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			// 6.释放资源
			JDBCUtils_V2.release(conn, pstmt, null);
		}
	}

	/**
	 * 根据id查询用户信息
	 */
	@Test
	public void testFindUserById() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			// 1.获取连接
			conn = JDBCUtils_V1.getConnection();
			// 2.编写sql语句
			String sql = "select * from tbl_user where uid=?";
			// 3.获取执行sql语句对象
			pstmt = conn.prepareStatement(sql);
			// 4.设置参数
			pstmt.setInt(1, 2);
			// 5.执行查询操作
			rs = pstmt.executeQuery();
			// 6.处理结果集
			while (rs.next()) {
				System.out.println(rs.getString(2) + "----" + rs.getString("upassword"));
			}
			// 释放资源放在此处行么?【不行滴!】
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 7.释放资源
			JDBCUtils_V1.release(conn, pstmt, rs);
		}
	}
}

这次我们做的还是比较简单!!下次我们做点复杂的数据库连接池。

猜你喜欢

转载自blog.csdn.net/weixin_40657079/article/details/81876067