码迷,mamicode.com
首页 > 数据库 > 详细

JDBC(数据库的使用优化)

时间:2016-01-01 21:00:09      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

一、业务代码的优化:(使用创建数据库连接工具类)
  1. 将获取Connection的代码抽取
    a.先抽取驱动,把驱动写在静态代码块中,提升运行效率,不用每次都加载驱动
    b.在抽取getConnection的方法。放回Connection(获得的连接对象)
  2. 将驱动等配置提取成配置文件
    a.由于数据库的连接信息都是相同的,所有把数据库连接的参数信息抽取成配置文件,方便作为参数读取使用。(比如连接路径,数据库密码等)
    b.读取配置文件有很多方法(主要知道的四种)
    1. //传统的读取方式,使用Properties对象的load方法读取
    2. public void test1() throws Exception{
    3. InputStream in = new FileInputStream("F:\\workspace\\android_web05\\src\\jdbc.properties");
    4. Properties p = new Properties();
    5. p.load(in);
    6. System.out.println(p.get("driver"));
    7. System.out.println(p.get("url"));
    8. System.out.println(p.get("username"));
    9. System.out.println(p.get("password"));
    10. }
    11. //由于路径在服务器端会时时变化,所以使用类的加载器动态获取路径
    12. public void test2() throws Exception{
    13. //类加载器:加载的是类路径的代码(.class字节码文件所在的目录)
    14. String path = ReadFileDemo.class.getClassLoader().getResource("jdbc.properties").getPath();
    15. System.out.println(path);
    16. InputStream in = new FileInputStream(path);
    17. Properties p = new Properties();
    18. p.load(in);
    19. System.out.println(p.get("driver"));
    20. System.out.println(p.get("url"));
    21. System.out.println(p.get("username"));
    22. System.out.println(p.get("password"));
    23. }
    24. //这种方式和第2中方式相似,不过是直接通过类的加载器获得文件的输入流。
    25. public void test3() throws Exception{
    26. //类加载器:加载的是类路径的代码(.class字节码文件所在的目录)
    27. InputStream in = ReadFileDemo.class.getClassLoader().getResourceAsStream("jdbc.properties");
    28. Properties p = new Properties();
    29. p.load(in);
    30. System.out.println(p.get("driver"));
    31. System.out.println(p.get("url"));
    32. System.out.println(p.get("username"));
    33. System.out.println(p.get("password"));
    34. }
    35. //通过创建ResourceBundle对象来读取配置文件,ResourceBundle底层也是封装了类的加载器专门读取配置文件使用,开发者一般使用这种方法,最为简单。
    36. public void test4() throws Exception{
    37. ResourceBundle rb = ResourceBundle.getBundle("jdbc");//这里直接写文件名,不用加扩展名
    38. System.out.println(rb.getString("driver"));
    39. System.out.println(rb.getString("username"));
    40. System.out.println(rb.getString("password"));
    41. System.out.println(rb.getString("url"));
    42. }

  3. 关闭资源进行方法抽取
    1. public static void close(Connection conn,Statement stmt,ResultSet rs){
    2. try {
    3. if(rs!=null){
    4. rs.close();
    5. }
    6. } catch (SQLException e) {
    7. e.printStackTrace();
    8. } finally{
    9. rs = null;
    10. try {
    11. if(stmt!=null){
    12. stmt.close();
    13. }
    14. } catch (SQLException e) {
    15. e.printStackTrace();
    16. } finally{
    17. stmt = null;
    18. try {
    19. if(conn!=null){
    20. conn.close();
    21. }
    22. } catch (SQLException e) {
    23. e.printStackTrace();
    24. } finally{
    25. conn = null;
    26. }
    27. }
    28. }
    29. }
    注意:需要判断连接是否为空,同时在finally里,把上一个try块中的对象置为空(null),已经本次try块中的对象关闭。
  4. 完整优化代码
    1. import java.sql.Connection;
    2. import java.sql.DriverManager;
    3. import java.sql.ResultSet;
    4. import java.sql.SQLException;
    5. import java.sql.Statement;
    6. import java.util.ResourceBundle;
    7. public class JDBCUtils {
    8. private static String driver = null;
    9. private static String url = null;
    10. private static String username = null;
    11. private static String password = null;
    12. static{
    13. ResourceBundle rb = ResourceBundle.getBundle("jdbc");
    14. driver = rb.getString("driver");
    15. url = rb.getString("url");
    16. username = rb.getString("username");
    17. password = rb.getString("password");
    18. }
    19. static{
    20. try {
    21. Class.forName(driver);
    22. } catch (ClassNotFoundException e) {
    23. e.printStackTrace();
    24. }
    25. }
    26. public static Connection getConnection() throws Exception{
    27. Connection conn = DriverManager.getConnection(url, username, password);
    28. return conn;
    29. }
    30. public static void close(Connection conn,Statement stmt,ResultSet rs){
    31. try {
    32. if(rs!=null){
    33. rs.close();
    34. }
    35. } catch (SQLException e) {
    36. e.printStackTrace();
    37. } finally{
    38. rs = null;
    39. try {
    40. if(stmt!=null){
    41. stmt.close();
    42. }
    43. } catch (SQLException e) {
    44. e.printStackTrace();
    45. } finally{
    46. stmt = null;
    47. try {
    48. if(conn!=null){
    49. conn.close();
    50. }
    51. } catch (SQLException e) {
    52. e.printStackTrace();
    53. } finally{
    54. conn = null;
    55. }
    56. }
    57. }
    58. }
    59. }






JDBC(数据库的使用优化)

标签:

原文地址:http://www.cnblogs.com/didixyy/p/912d9d79462c480080876d552142d2f8.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!