import和class.forName都会加载类文件.当我在jsp文件中导入mysql数据的示例时,需要通过class.forName导入驱动程序类.当我通过import语句导入jdbc驱动程序时,它无法从tomcat服务器中的mysql获取数据.
1 : import
==> loads the class when you call any instance of it or call anything by class reference
==> loads the class when call is made
2 : Class.forName("");
==> loads the class in the jvm immediately
Run Code Online (Sandbox Code Playgroud)
如果一个类有静态块,可以看出差异
==> import will not call the static block
==> Class.forName("") will call the static block
Run Code Online (Sandbox Code Playgroud)
在你的情况下
===> Driver class when loaded by Class.forName("") , executes its static block , which published the driver
==> Simply importing the Driver class wont execute the static block and thus your Driver will not be published for connection objects to be created
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5142 次 |
| 最近记录: |