当前位置:Gxlcms > 数据库问题 > JDBC(数据库的使用优化)

JDBC(数据库的使用优化)

时间:2021-07-01 10:21:17 帮助过:2人阅读

  • public void test1() throws Exception{
  • InputStream in = new FileInputStream("F:\\workspace\\android_web05\\src\\jdbc.properties");
  • Properties p = new Properties();
  • p.load(in);
  • System.out.println(p.get("driver"));
  • System.out.println(p.get("url"));
  • System.out.println(p.get("username"));
  • System.out.println(p.get("password"));
  • }
  • //由于路径在服务器端会时时变化,所以使用类的加载器动态获取路径
  • public void test2() throws Exception{
  • //类加载器:加载的是类路径的代码(.class字节码文件所在的目录)
  • String path = ReadFileDemo.class.getClassLoader().getResource("jdbc.properties").getPath();
  • System.out.println(path);
  • InputStream in = new FileInputStream(path);
  • Properties p = new Properties();
  • p.load(in);
  • System.out.println(p.get("driver"));
  • System.out.println(p.get("url"));
  • System.out.println(p.get("username"));
  • System.out.println(p.get("password"));
  • }
  • //这种方式和第2中方式相似,不过是直接通过类的加载器获得文件的输入流。
  • public void test3() throws Exception{
  • //类加载器:加载的是类路径的代码(.class字节码文件所在的目录)
  • InputStream in = ReadFileDemo.class.getClassLoader().getResourceAsStream("jdbc.properties");
  • Properties p = new Properties();
  • p.load(in);
  • System.out.println(p.get("driver"));
  • System.out.println(p.get("url"));
  • System.out.println(p.get("username"));
  • System.out.println(p.get("password"));
  • }
  • //通过创建ResourceBundle对象来读取配置文件,ResourceBundle底层也是封装了类的加载器专门读取配置文件使用,开发者一般使用这种方法,最为简单。
  • public void test4() throws Exception{
  • ResourceBundle rb = ResourceBundle.getBundle("jdbc");//这里直接写文件名,不用加扩展名
  • System.out.println(rb.getString("driver"));
  • System.out.println(rb.getString("username"));
  • System.out.println(rb.getString("password"));
  • System.out.println(rb.getString("url"));
  • }

  • 关闭资源进行方法抽取
    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块中的对象关闭。
  • 完整优化代码
    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. }




  • 来自为知笔记(Wiz)

    JDBC(数据库的使用优化)

    标签:

    人气教程排行