JDBCUtils 类的 创建 及 使用 (增删改查)

JDBCUtils的创建:

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

public class JDBCUtils {
	//这个工具类,主要为我们获取一个数据库连接
	private static String driverName = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/day04";
	private static String username = "root";
	private static String password = "123";
	
	//静态代码块,目的,让第一次使用到JDBCUtils中加载驱动,第二次以后不再加载了
	static{
		//1.加载驱动
		try {
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			//System.out.println("驱动加载失败..请检查驱动包");
			throw new RuntimeException("驱动加载失败..请检查驱动包");
		}
	}
	
	public static Connection getConnection() throws Exception{
		//2.获取和数据库的连接
		Connection conn =  DriverManager.getConnection(url, username, password);
		//3.返回连接对象
		return conn;
		
	}
	//关闭所有资源的统一代码
	public static void closeAll(Connection conn,Statement st,ResultSet rs){
		//负责关闭
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(st != null){
			try {
				st.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

JDBCUtils 的使用:

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

public class TestDemo {
	public static void main(String[] args) {
		// 1.获取到数据库连接
		Connection conn = null;
		// 2.通过连接对象, 获取SQL执行对象
		Statement st = null;
		// 3.st执行sql语句
		ResultSet rs = null;
		try {
			conn = JDBCUtils.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery("select * from category");
			// 4.处理结果集
			while (rs.next()) {
				Object cid = rs.getObject("cid");
				Object cname = rs.getObject("cname");
				System.out.println(cid + "	" + cname);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			// 5.释放资源
			JDBCUtils.closeAll(conn, st, rs);
		}
		

	}
}

JDBCUtils 的 增删改查 操作: 

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

public class TestDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//insert();
//		delete();
//		update();
//		query();
	}
	//插入
	public static void insert(){
		//1.
		Connection conn = null;
		//2.
		Statement st = null;
		//4.
		try{
			conn = JDBCUtils.getConnection();
			st = conn.createStatement();
			int rows = st.executeUpdate("insert into category (cname) values ('饮料'),('灵饰'),('羽毛球')");
			System.out.println("成功插入"+rows+"行!!!");
		}catch(Exception e){
			System.out.println(e);
		}finally {
			JDBCUtils.closeAll(conn, st,null);
		}
	}
	//删除
	public static void delete(){
		//1.
		Connection conn = null;
		//2.
		Statement st = null;
		//3.
		ResultSet rs = null;
		//4.
		try{
			conn = JDBCUtils.getConnection();
			st = conn.createStatement();
			int rows = st.executeUpdate("delete from category where cname='饮料'");
			System.out.println("成功删除"+rows+"行!!!");
		}catch(Exception e){
			System.out.println(e);
		}finally {
			JDBCUtils.closeAll(conn, st, rs);
		}
	}
	//修改
	public static void update(){
		//1.
		Connection conn = null;
		//2.
		Statement st = null;
		//3.
		ResultSet rs = null;
		//4.
		try{
			conn = JDBCUtils.getConnection();
			st = conn.createStatement();
			int rows = st.executeUpdate("update category set cname='乒乓球类' where cid = 11");
			System.out.println("成功修改"+rows+"行!!!");
		}catch(Exception e){
			System.out.println(e);
		}finally {
			JDBCUtils.closeAll(conn, st, rs);
		}
	}
	//查询
	public static void query(){
		//1.
		Connection conn = null;
		//2.
		Statement st = null;
		//3.
		ResultSet rs = null;
		//4.
		try{
			conn = JDBCUtils.getConnection();
			st = conn.createStatement();
			rs = st.executeQuery("select * from category where cid= 14");
			while(rs.next()){
				Object cid = rs.getObject("cid");
				Object cname = rs.getObject("cname");
				System.out.println(cid+"	"+cname);
			}
			
		}catch(Exception e){
			System.out.println(e);
		}finally {
			JDBCUtils.closeAll(conn, st, rs);
		}
	}
}
原文地址:https://www.cnblogs.com/Romantic-Chopin/p/12451065.html