JDBC连结中Class.forName()详解
来源:互联网 发布:大数据采集业务 编辑:程序博客网 时间:2024/05/17 22:29
java开发中,采用JDBC连接数据库,最经常用到的就是Class.forName()这个方法.
Class.forName(String className)在JDK帮助文档中是这样说的:返回与带有给定字符串名的类或接口相关联的Class对象,
参数className是所需类的完全限定名;返回值是具有指定名的类的Class对象.如调用Class.forName("x") 将导致名为x的类被初始化.
JDBC连接中,以mysql为例,取得数据库连结代码如下:
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
- String user = "test";
- String psw = "test";
- Connection con = DriverManager.getConnection(url,user,psw);
DriverManager.getConnection()可以取到driver是因为Class.forName("...")方法已经要求JVM查找并加载指定的类(即com.mysql.jdbc.Driver类),而jdk对Driver的说明中有以下一段话:
When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager
查看com.mysql.jdbc的原代码如下:
- package com.mysql.jdbc
- public class Driver extends NonRegisteringDriver implements java.sql.Driver {
- // ~ Static fields/initializers
- // --------------------------------------------- //
- // Register ourselves with the DriverManager
- //
- static {
- t ry {
- java.sql.DriverManager.registerDriver(new Driver());
- } catch (SQLException E) {
- throw new RuntimeException("Can't register driver!");
- }
- }
- // ~ Constructors
- // -----------------------------------------------------------
- /**
- * Construct a new driver and register it with DriverManager
- *
- * @throws SQLException
- * if a database error occurs.
- */
- public Driver() throws SQLException {
- // Required for Class.forName().newInstance()
- }
- }
从而通过Class.forName(DriverString)会向DriverManager注册该Driver类.所以可以直接调用.
而Class.forName("").newInstance()则等于是将该Driver驱动类实例化,返回该类的一个实例,所以,如果只是取JDBC的Driver驱动,可
以不必用newInstance().
原话是这样的:
we just want to load the driver to jvm only, but not need to user the instance of driver, so call Class.forName(xxx.xx.xx) is enough, if you call Class.forName(xxx.xx.xx).newInstance(), the result will same as calling Class.forName(xxx.xx.xx), because Class.forName(xxx.xx.xx).newInstance() will load driver first, and then create instance, but the instacne you will never use in usual, so you need not to create it.
- JDBC连结中Class.forName()详解
- JDBC连结中Class.forName()详解
- jdbc中的class.forName详解
- JDBC中Class.forName()的作用
- JDBC中Class.forName()的作用
- JDBC中是否使用Class.forName()
- JDBC之Class.forName()
- Java中Class.forName()用法详解
- Java中Class.forName()用法详解
- JDBC 连接数据库 中Class.forName的作用
- 【解惑】JDBC中使用Class.forName("xxx")的意义
- JDBC 连接数据库 中Class.forName的作用
- JDBC中注册驱动为什么使用Class.forName
- 【解惑】JDBC中使用Class.forName("xxx")的意义
- Class.forName()详解
- Class.forName()用法详解
- Class.forName()使用详解
- Class.forName详解
- IOS 学习之 —— Foundation & CoreFoundation 的转换
- Zepto 使用中的一些注意点
- 解决Eclipse中SVN版本信息不显示的问题
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
- jtag
- JDBC连结中Class.forName()详解
- BZOJ 1170 [Balkan2007]Cipher Hash
- Volley的使用笔记
- Installing Wireshark on Ubuntu 12.04 LTS
- android versionCode versionName的获取
- Nutch1.9安装
- A tricky way to create a lookup field in Visualforce page
- nginx编译和配置
- C程序中对时间的处理——time库函数详解以及系统时间结构体类型