通过jdbc连接数据库读取propertites文件的两种方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012430402/article/details/79568131

工程目录结构如下:

测试类-JDBC_03.java:


    
    
  1. package com.xiao.jdbc;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import org.junit.Test;
  6. import com.xiao.utils.JDBCUtils_02;
  7. import com.xiao.utils.JDBCUtils_03;
  8. /**
  9. * @author 笑笑
  10. * @Date 2018年3月15日下午2:42:15
  11. *
  12. */
  13. //通过加载properties文件来连接数据库的两种方式测试
  14. public class JDBC_03 {
  15. //通过加载properties文件来连接数据库的方式一 测试
  16. @Test
  17. public void test_01() throws Exception{
  18. //1.使用工具类JDBCUtils_02注册并获取连接
  19. Connection cn = JDBCUtils_02.getConnection();
  20. //2.书写SQL语句
  21. String sql = "select * from user_tb";
  22. //3.获取SQL语句预编译对象
  23. PreparedStatement ps = cn.prepareStatement(sql);
  24. //4.执行SQL语句
  25. ResultSet rs = ps.executeQuery();
  26. //5.处理结果集
  27. while(rs.next()){
  28. System.out.println(rs.getInt( "uid")+ " "+rs.getString( "username"));
  29. }
  30. //6.释放资源
  31. JDBCUtils_02.relase(cn, ps, rs);
  32. }
  33. //通过加载properties文件来连接数据库的方式二 测试
  34. @Test
  35. public void test_02() throws Exception{
  36. //1.使用工具类JDBCUtils_03注册并获取连接
  37. Connection cn = JDBCUtils_03.getConnection();
  38. //2.书写SQL语句
  39. String sql = "select * from user_tb";
  40. //3.获取SQL语句预编译对象
  41. PreparedStatement ps = cn.prepareStatement(sql);
  42. //4.执行SQL语句
  43. ResultSet rs = ps.executeQuery();
  44. //5.处理结果集
  45. while(rs.next()){
  46. System.out.println(rs.getInt( "uid")+ " "+rs.getString( "username"));
  47. }
  48. //6.释放资源
  49. JDBCUtils_02.relase(cn, ps, rs);
  50. }
  51. }

工具类-JDBCUtils_02.java:


     
     
  1. package com.xiao.utils;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.Statement;
  6. import java.util.ResourceBundle;
  7. /**
  8. * @author 笑笑
  9. * @Date 2018年3月15日下午2:38:39
  10. *
  11. */
  12. //通过加载properties文件来连接数据库的方式一
  13. public class JDBCUtils_02 {
  14. private static String driver;
  15. private static String url;
  16. private static String username;
  17. private static String password;
  18. /**
  19. * 静态代码块加载配置文件信息
  20. */
  21. static{
  22. ResourceBundle bundle = ResourceBundle.getBundle( "jdbc");
  23. driver = bundle.getString( "driver");
  24. url = bundle.getString( "url");
  25. username = bundle.getString( "username");
  26. password = bundle.getString( "password");
  27. }
  28. //注册并获取数据库连接
  29. public static Connection getConnection() throws Exception{
  30. //1.注册驱动
  31. Class.forName(driver);
  32. //2.获取数据库连接
  33. Connection cn = DriverManager.getConnection(url, username, password);
  34. return cn;
  35. }
  36. //释放资源
  37. public static void relase(Connection cn,Statement st,ResultSet rs) throws Exception{
  38. if(rs!= null){
  39. rs.close();
  40. }
  41. if(st!= null){
  42. st.close();
  43. }
  44. if(cn!= null){
  45. cn.close();
  46. }
  47. }
  48. }

工具类-JDBCUtils_03.java:


      
      
  1. package com.xiao.utils;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8. import java.util.Properties;
  9. /**
  10. * @author 笑笑
  11. * @Date 2018年3月15日下午2:38:39
  12. *
  13. */
  14. //通过加载properties文件来连接数据库的方式二
  15. public class JDBCUtils_03 {
  16. private static String driver;
  17. private static String url;
  18. private static String username;
  19. private static String password;
  20. /**
  21. * 静态代码块加载配置文件信息
  22. */
  23. static {
  24. try {
  25. // 1.通过当前类获取类加载器
  26. ClassLoader classLoader = JDBCUtils_03.class.getClassLoader();
  27. // 2.通过类加载器的方法获得一个输入流
  28. InputStream in = classLoader.getResourceAsStream( "jdbc.properties");
  29. // 3.创建一个properties对象(集合)
  30. Properties props = new Properties();
  31. // 4.加载输入流
  32. props.load(in);
  33. // 5.获取相关参数的值
  34. driver = props.getProperty( "driver");
  35. url = props.getProperty( "url");
  36. username = props.getProperty( "username");
  37. password = props.getProperty( "password");
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. //注册并获取数据库连接
  43. public static Connection getConnection() throws Exception{
  44. //1.注册驱动
  45. Class.forName(driver);
  46. //2.获取数据库连接
  47. Connection cn = DriverManager.getConnection(url, username, password);
  48. return cn;
  49. }
  50. //释放资源
  51. public static void relase(Connection cn,Statement st,ResultSet rs) throws Exception{
  52. if(rs!= null){
  53. rs.close();
  54. }
  55. if(st!= null){
  56. st.close();
  57. }
  58. if(cn!= null){
  59. cn.close();
  60. }
  61. }
  62. }

properties文件:


      
      
  1. driver=com.mysql.jdbc.Driver
  2. url=jdbc:mysql://localhost:3306/all_db?useUnicode=true&characterEncoding=utf8
  3. username=root
  4. password=root





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值