JDBCUtil类

public class JDBCUtil {
	//读取和处理资源文件中的信息
	static Properties pros=null;
	static {//加载JDBCUtil类的时候调用
		pros=new Properties();
		//输入流读取资源文件
		try {
			pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static Connection getMysqlConn() {
		try {//连接数据库,封装写在资源文件中
			Class.forName(pros.getProperty("mysqlDriver"));
			return DriverManager.getConnection(pros.getProperty("mysqlURL"),
					pros.getProperty("mysqlUser"),pros.getProperty("mysqlPwd"));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	//关闭rs,ps,conn
	public static void close(ResultSet rs,Statement ps,Connection conn) {
		try {
			if (ps!=null) {
				ps.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (conn!=null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (rs!=null) {
				rs.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//关闭ps,conn
	public static void close(Statement ps,Connection conn) {
		try {
			if (ps!=null) {
				ps.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (conn!=null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//关闭conn
	public static void close(Connection conn) {
		try {
			if (conn!=null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

测试类

/**
 * @测试JDBC工具类来简化JDBC开发
 * @author www.zzjava.xyz
 *
 */
public class Testjdbc {
public static void main(String[] args) {
	Connection conn=null;
	PreparedStatement ps=null;
	
	try {
		conn=JDBCUtil.getMysqlConn();
		
		ps=conn.prepareStatement("insert into t_user (username,regtime) values (?,?)");
		ps.setString(1, "张三");
		java.sql.Date data=new java.sql.Date(System.currentTimeMillis());
		ps.setDate(2, data);
		ps.execute();
		
		System.out.println("执行成功");
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		JDBCUtil.close(conn);
	}
}
}

资源文件

mysqlDriver=com.mysql.jdbc.Driver
mysqlURL=jdbc:mysql://localhost:3306/tjdbc?serverTimezone=UTC&characterEncoding=utf-8
mysqlUser=root
mysqlPwd=zzjava

工程结构

aaa.png

mysql结构

tmysql.png