zoukankan      html  css  js  c++  java
  • Class.forName("com.mysql.jdbc.Driver");的作用

    对于大的项目当然我们都已经有了原有基本框架,但是对于一些新的技术探讨的时候,我们还是直接调用Class.forName("com.mysql.jdbc.Driver")连接数据库进行相关的测试

    今天用HTTP大文件上传断点续传控件发布-Xproer.HttpUploader5的时候发现这有点有点忘记,所有整理一些文档进行做记录

     1 public class HttpUploaderDB {
     2     String m_dbDriver    ="com.mysql.jdbc.Driver"; 
     3     String m_dbUrl         ="jdbc:mysql://127.0.0.1/xdb_files?useUnicode=true&characterEncoding=utf-8";   
     4     
     5     /*
     6      * 参数:
     7      *         sc = this.getServletContext()
     8      * */
     9     public HttpUploaderDB(ServletContext sc)
    10     {        
    11     }
    12     
    13     public Connection GetCon()
    14     {        
    15         Connection con = null;
    16         
    17         try 
    18         {
    19             Class.forName(this.m_dbDriver).newInstance();//加载驱动。
    20             con = DriverManager.getConnection(this.m_dbUrl,"root","123456");
    21         }
    22         catch (SQLException e) 
    23         {
    24             // TODO Auto-generated catch block
    25             e.printStackTrace();
    26         } catch (ClassNotFoundException e) {
    27             // TODO Auto-generated catch block
    28             e.printStackTrace();
    29         } catch (InstantiationException e) {
    30             // TODO Auto-generated catch block
    31             e.printStackTrace();
    32         } catch (IllegalAccessException e) {
    33             // TODO Auto-generated catch block
    34             e.printStackTrace();
    35         }
    36         return con;
    37     }
    38 }

    Class.forName("com.mysql.jdbc.Driver");的作用

    使用JDBC时,我们都会很自然得使用下列语句:java 代码
    1. Class.forName("com.mysql.jdbc.Driver");   
    2. String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";   
    3. String user = "";   
    4. String psw = "";   
    5. Connection con = DriverManager.getConnection(url,user,psw);  


        为什么说很自然呢,因为无论是网上还是书本教程上得例子都是这样的,而且程序也确实正常运行了,于是大家也就心安理得的找葫芦画瓢下去了。
        一定要有这一句吗?不是的,我们完全可以用这样一句代替它:

    java 代码
    1 com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();   
    2 //or:   
    3 //new com.mysql.jdbc.Driver();   
    4 String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";   
    5 String user = "";   
    6 String psw = "";   
    7 Connection con = DriverManager.getConnection(url,user,psw);  

        大家可能都看出个大概来了,我们只需要在调用DriverManager的getConnection方法之前,保证相应的Driver类已经被加载到 jvm中,并且完成了类的初始化工作就行了,而具体是怎样实现这个功能却是没有讲究的。上面两种方法都可以实现这个功能,因此程序可以正常运行。注意了, 如果我们进行如下操作,程序是不能正常运行的,因为这样仅仅使Driver类被装载到jvm中,却没有进行相应的初始化工作。

    java 代码
    1 com.mysql.jdbc.Driver driver = null;   
    2 //or:   
    3 ClassLoader cl = new ClassLoader();   
    4 cl.loadClass("com.mysql.jdbc.Driver"); 


         我们都知道JDBC是使用Bridge模式进行设计的,DriverManager就是其中的Abstraction,java.sql.Driver是 Implementor,com.mysql.jdbc.Driver是Implementor的一个具体实现(请参考GOF的Bridge模式的描 述)。大家注意了,前一个Driver是一个接口,后者却是一个类,它实现了前面的Driver接口。
         Bridge模式中,Abstraction(DriverManager)是要拥有一个Implementor(Driver)的引用的,但是我们在使 用过程中,并没有将Driver对象注册到DriverManager中去啊,这是怎么回事呢?jdk文档对Driver的描述中有这么一句:
         When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager 
    哦,原来是com.mysql.jdbc.Driver在装载完后自动帮我们完成了这一步骤。源代码是这样的:

    java 代码
     1 package com.mysql.jdbc   
     2   
     3 public class Driver extends NonRegisteringDriver implements java.sql.Driver {   
     4 // ~ Static fields/initializers   
     5 // --------------------------------------------- //   
     6 // Register ourselves with the DriverManager   
     7 //   
     8 static {   
     9      t ry {   
    10                java.sql.DriverManager.registerDriver(new Driver());   
    11            } catch (SQLException E) {   
    12               throw new RuntimeException("Can't register driver!");   
    13            }   
    14    }   
    15 // ~ Constructors   
    16 // -----------------------------------------------------------   
    17 /**
    18    * Construct a new driver and register it with DriverManager
    19    *
    20    * @throws SQLException
    21    *              if a database error occurs.
    22    */  
    23 public Driver() throws SQLException {   
    24      // Required for Class.forName().newInstance()   
    25 }   
    26 }  
  • 相关阅读:
    xtoi (Hex to Integer) C function Nanoseconds Network
    Learning Scrapy | 王晨的博客
    Facebook搜索项目多名工程师均来自Google
    Beyond the C++ Standard Library: An Introduction to Boost: Björn Karlsson: 9780321133540: Amazon.com: Books
    归并排序 详解
    NewsFeed 3.0 发布,移植到 Python 3 开源中国 OSChina.NET
    对于拷贝构造函数和赋值构造函数的理解
    python 的os.fork()
    Install C++ Boost on Ubuntu
    石川的blog ,注意
  • 原文地址:https://www.cnblogs.com/visec479/p/3909246.html
Copyright © 2011-2022 走看看