十、JDBC--自定义JdbcUtils工具类(二)

一、JDBC原始版本


public class Demo {
	
	static String  url="jdbc:mysql://localhost:3306/day11";
	static String user="root";
	static String password="root";
	
	
	/**
	 * 获取连接对象
	 * @return
	 */
	private static Connection getConn() {
		
		Connection conn = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(url, "root", "root");
		} catch (Exception e) {
			throw new RuntimeException();
		}
		
		return conn;
	}
	
	
	/**
	 *  创建表
	 * @throws SQLException
	 */
	@Test
	public void createTable() throws SQLException {
		
		Connection 	conn = Demo.getConn();
		Statement st=null;
		
		try {
			st = conn.createStatement();
			
			String sql="CREATE TABLE stu(id INT ,NAME VARCHAR(20),age int ,birth date);";
			int row = st.executeUpdate(sql);
			System.out.println("影响了"+row+"行");
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
		}
	}
	
	/**
	 * 插入数据
	 * @throws SQLException
	 */
	@Test
	public void insert() throws SQLException {
		
		Connection 	conn = Demo.getConn();
		Statement st=null;
		
		try {
			st = conn.createStatement();
			
			String sql="insert into stu(id,name,age) values(1,'小白',12)";
			int row = st.executeUpdate(sql);
			
			
			System.out.println("影响了"+row+"行");
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
		}
	}
	
	/**
	 * 修改数据
	 * @throws SQLException
	 */
	@Test
	public void update() throws SQLException {
		
		Connection 	conn = Demo.getConn();
		Statement st=null;
		
		try {
			st = conn.createStatement();
			
			String sql="update stu set name='小何' where id=1 ";
			int row = st.executeUpdate(sql);
			System.out.println("影响了"+row+"行");
			
		} catch (Exception e) {
			throw new RuntimeException(e);
			
		}finally {
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
		}
		
	}
	
	/**
	 * 查询数据
	 * @throws SQLException
	 */
	@Test
	public void select() throws SQLException {
		
		Connection 	conn = Demo.getConn();
		Statement st=null;
		
		try {
			st = conn.createStatement();
			
			String sql="select * from stu";
			ResultSet rs = st.executeQuery(sql);
		
			while(rs.next()) {
				
				//根据索引值获取行数据
				int id = rs.getInt(1);
				String name = rs.getString(2);
				int age = rs.getInt(3);
				System.out.println(id+" - "+name+" - "+age);
				
				//根据字段名称获取行数据
				int id1 = rs.getInt("id");
				String name1 = rs.getString("name");
				int age1 = rs.getInt("age");
				System.out.println(id1+" - "+name1+" - "+age1);
			}
			
		} catch (Exception e) {
			throw new RuntimeException(e);
			
		}finally {
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
		}
		
	}
	
	
	/**
	 * 删除数据
	 * @throws SQLException
	 */
	@Test
	public void delete() throws SQLException {
		
		Connection 	conn = Demo.getConn();
		Statement st=null;
		
		try {
			st = conn.createStatement();
			
			String sql="delete from h where id =1 ";
			int row = st.executeUpdate(sql);
			System.out.println("影响了"+row+"行");
			
		} catch (Exception e) {
			throw new RuntimeException(e);
			
		}finally {
			if(st!=null) {
				st.close();
			}
			if(conn!=null) {
				conn.close();
			}
		}
		
	}
	
	
}

二、JdbcUtil工具1.0 - 原始版本的升级版本

JdbcUtil.java


public class JdbcUtil {
	
	private static  String url="jdbc:mysql://localhost:3306/day11";
	private static  String user="root";
	private static  String password="root";
	
	/**
	 * 使用静态代码块来注册驱动,原因:注册驱动只需注册一次即可。
	 */
	static {
		try {
						
			//加载驱动类,自动注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}

	
	/**
	 * 获取连接对象
	 * @return
	 */
	public static Connection  getConn() {
		
		try {
			Connection conn = DriverManager.getConnection(url, user, password);
			return conn;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * 关闭连接
	 * @param conn
	 * @param st
	 */
	public static void close(Connection conn,Statement st) {
		try {
			if(st!=null) {
				st.close();
			}
			
			if(conn!=null) {
				conn.close();
			}
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 关闭连接
	 * @param conn
	 * @param st
	 */
	public static void close(Connection conn,Statement st,ResultSet rs) {
		try {
			if(rs!=null) {
				rs.close();
			}
			if(st!=null) {
				st.close();
			}
			
			if(conn!=null) {
				conn.close();
			}
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
			
}

测试工具类 Demo.java


public class Demo {


	
	/**
	 * 插入数据
	 * @throws SQLException
	 */
	@Test
	public void insert() throws SQLException {
		
		Connection 	conn = JdbcUtil.getConn();
		Statement st=null;
		
		try {
			st = conn.createStatement();
			
			String sql="insert into stu(id,name,age) values(1,'小白',12)";
			int row = st.executeUpdate(sql);
			System.out.println("影响了"+row+"行");
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.close(conn, st);
		}
	}
	

	
	/**
	 * 查询数据
	 * @throws SQLException
	 */
	@Test
	public void select() throws SQLException {
		
		Connection 	conn = JdbcUtil.getConn();
		Statement st=null;
		ResultSet rs=null;
		try {
			
			String sql="select * from stu";

			st = conn.createStatement();
			
			rs = st.executeQuery(sql);
		
			while(rs.next()) {
				
				//根据索引值获取行数据
				int id = rs.getInt(1);
				String name = rs.getString(2);
				int age = rs.getInt(3);
				System.out.println(id+" - "+name+" - "+age);
				
				//根据字段名称获取行数据
				int id1 = rs.getInt("id");
				String name1 = rs.getString("name");
				int age1 = rs.getInt("age");
				System.out.println(id1+" - "+name1+" - "+age1);
			}
			
		} catch (Exception e) {
			throw new RuntimeException(e);
			
		}finally {
			JdbcUtil.close(conn, st,rs);
		}
		
	}
	

}

三、JdbcUtil工具2.0 - 读取配置文件

db.properties 配置文件。
该配置文件放在 项目/WEB-INF/bin 根目录下。(即 classpath 目录下)

	url=jdbc:mysql://localhost:3306/day11
	user=root
	password=root

JdbcUtil.java


public class JdbcUtil {
	
	public static Properties p;
	
	/**
	 * 使用静态代码块来注册驱动,原因:注册驱动只需注册一次即可。
	 */
	static {
		try {
			
			//加载配置文件
			p=new Properties();
			p.load(JdbcUtil.class.getResourceAsStream("/db.properties"));
			
			//加载驱动类,自动注册驱动
			Class.forName("com.mysql.jdbc.Driver");
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	
	public static void main(String[] args) {
		System.out.println(JdbcUtil.p);
	}
	
	
	/**
	 * 获取连接对象
	 * @return
	 */
	public static Connection  getConn() {
		
		try {
			Connection conn = DriverManager.getConnection(p.getProperty("url"), p.getProperty("user"), p.getProperty("password"));
			return conn;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * 关闭连接
	 * @param conn
	 * @param st
	 */
	public static void close(Connection conn,Statement st) {
		try {
			if(st!=null) {
				st.close();
			}
			
			if(conn!=null) {
				conn.close();
			}
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 关闭连接
	 * @param conn
	 * @param st
	 */
	public static void close(Connection conn,Statement st,ResultSet rs) {
		try {
			if(rs!=null) {
				rs.close();
			}
			if(st!=null) {
				st.close();
			}
			
			if(conn!=null) {
				conn.close();
			}
			
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
			
}

发布了94 篇原创文章 · 获赞 0 · 访问量 632

猜你喜欢

转载自blog.csdn.net/weixin_45602227/article/details/104217121
今日推荐