DBUtils工具类,听说你没用过?( 二 )


public static Connection getConnection() throws Exception {/*** 获取数据库的连接*///1.读取配置文件的4个基本信息InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");Properties pros = new Properties();pros.load(is);String user = pros.getProperty("user");String password = pros.getProperty("password");String url = pros.getProperty("url");String driverClass = pros.getProperty("driverClass");//2.加载驱动Class.forName(driverClass);//3.获取连接Connection con = DriverManager.getConnection(url,user,password);System.out.println(con);return con; }说明:

  • 首先通过InputStream读取配置文件的4个基本信息 , 利用ClassLoader调用getSystemClassLoader()方法实际上相当ConnectionTest.class.getClassLoader()的作用 , 但使用前者的好处是避免了出现第三方API;
  • 定义4个String类型的字符串接收获取到的基本信息;
  • 此处省略了注册驱动操作 , 是因为在mysql的Driver实现类中 , 声明如下操作 , 故在实际的代码编写中不需要在重新进行注册驱动 。
static {try {java.sql.DriverManager.registerDriver(new Driver());} catch (SQLException E) {throw new RuntimeException("Can't register driver!");}}非查询类释放资源方法:
/*** 关闭连接和Statement* @param con* @param ps*/ public static void closeResource(Connection con,Statement ps) {//资源关闭try {if(ps != null)ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(con != null)con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} }说明: 非查询类释放资源过程中只需要关闭连接Connection和Statement , 通过判断Statement的对象ps和Connection的对象con是否非空即可执行资源关闭 。 查询类释放资源方法:
/*** 关闭连接、Statement和ResultSet* @param con* @param ps*/ public static void closeResource(Connection con,Statement ps,ResultSet rs) {//资源关闭try {if(ps != null)ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(con != null)con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(rs != null)rs.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} }说明: 查询类释放资源过程中不仅仅要关闭连接Connection和Statement , 还需要关闭查询过程中得到的结果集ResultSet 。
4.实现对数据表的增删改查(1)向student表中插入一条数据说明: 预期目的是为了将(“郑**” , “304728796@qq.com” , “2000-01-01”)数据插入到数据库中 , 运行后结果如图所示 。
@Test //向student表中插入一条数据 public void testInsert(){Connection con = null;PreparedStatement ps = null;try {//1.读取配置文件的4个基本信息InputStream is = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");Properties pros = new Properties();pros.load(is);String user = pros.getProperty("user");String password = pros.getProperty("password");String url = pros.getProperty("url");String driverClass = pros.getProperty("driverClass");//2.加载驱动Class.forName(driverClass);//3.获取连接con = DriverManager.getConnection(url,user,password);System.out.println(con);//4.预编译SQL语句 , 返回PreparedStatement的实例String sql = "insert into student(name,email,birth)values(?,?,?)";//?:占位符ps = con.prepareStatement(sql);//5.填充占位符ps.setString(1, "郑**");ps.setString(2, "304728796@qq.com");SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");java.util.Date date = sdf.parse("2000-01-01");ps.setDate(3,new Date(date.getTime()));//6.执行SQLps.execute();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {//7.资源关闭try {if(ps != null)ps.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}try {if(con != null)con.close();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }


推荐阅读