Java Class.forName,JDBC连接加载驱动程序 [英] Java Class.forName, JDBC connection loading driver

查看:215
本文介绍了Java Class.forName,JDBC连接加载驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行简单的JDBC连接时,所有资源都提供与

While doing a simple JDBC connection, all the resources gives the same code that

String driver = "com.mysql.jdbc.Driver";
Statement statement = null; 
Class.forName(driver); 
Connection conn  = DriverManager.getConnection(url + dbName,userName, password);

但是我们实际上与"Class.forName(driver)"无关.我们没有将其存储在任何地方. 它的用途是什么,因为我们与Class.forName(driver)的返回无关.

But we actually nothing do with "Class.forName(driver)". We didn't stored it anywhere. What is the use of that as we nothing do with Class.forName(driver)'s return.

推荐答案

Class.forName()尝试加载命名的类.在JDBC的早期版本中,这是必需的,因为Driver类要求以这种方式加载该类.年龄不是必需的.

Class.forName() attempts to load the named class. In early versions of JDBC, this was necessary as the Driver class required the class to be loaded in this way. This hasn't been required for ages.

省去电话,不会有任何不好的事情.

Leave out the call and nothing bad will happen.

由于某些原因,教程和示例仍沿用旧方法.

For some reason, tutorials and examples persist with the old way.

手动加载类的唯一微小好处是,它可以告诉您确切的问题,以防您在类路径中没有正确的类.

The only tiny benefit of loading the class manually is that it tells you exactly what the problem is in case you haven't got the right class in the classpath.

这篇关于Java Class.forName,JDBC连接加载驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆