java中Class.forName方法的作用是什么
创始人
2024-12-25 14:25:17
0次

Java的特点有哪些

Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。

java中Class.forName的作用

  连接数据库几大步.看以下代码

 import com.mysql.jdbc.Driver;
  import java.sql.*;
  public class JdbcDemo {
  public static void main(String[] args) throws SQLException, ClassNotFoundException {
  String url = "jdbc:mysql://127.0.0.1:3306/mydb";
  String username = "root";
  String password = "redhat";
  Class.forName("com.mysql.jdbc.Driver");
  Connection connection = DriverManager.getConnection(url, username, password);
  String sql = "SELECT * FROM msg";
  PreparedStatement prepareStatement = connection.prepareStatement(sql);
  ResultSet resultSet = prepareStatement.executeQuery();
  resultSet.next();
  String address = resultSet.getString("address");
  System.out.println(address);
  }
  }

  其中第一步,搞的我有点想不通.为啥Class.forName传入了一段字符串之后,就知道我连接的数据库是mysql? 有点不科学啊.Class.forName到底做了啥.下面就开始到源码中,一探究竟.

@CallerSensitive
  public static Class forName(String className)
  throws ClassNotFoundException {
  Class caller = Reflection.getCallerClass();
  return forName0(className, true, ClassLoader.getClassLoader(caller), caller);
  }

  发现它调用了forName0方法,继续跟踪再看看

  private static native Class forName0(String name, boolean initialize,

  ClassLoader loader,

  Class caller)

  throws ClassNotFoundException;

  native方法,源码也只能到此结束了.看下官方文档,怎么说吧.

  发现官方文档,还是描述的很清楚的.

  Returns the Class object associated with the class or interface with the given string name,

  using the given class loader.

  Given the fully qualified name for a class or interface (in the same format returned by getName)

  this method attempts to locate, load, and link the class or interface.

  The specified class loader is used to load the class or interface.

  If the parameter loader is null, the class is loaded through the bootstrap class loader.

  The class is initialized only if the initialize parameter is true and if it has not been

  initialized earlier.

  嗯,描述的还算是很清楚.返回一个给定类或者接口的一个Class对象,如果没有给定classloader,那么会使用根类加载器.如果initalize这个参数传了true,那么给定的类如果之前没有被初始化过,那么会被初始化.我们在JDBC第一步的时候,传入的参数是com.mysql.jdbc.Driver. 也就是说这个类会被初始化.我们看一下这个类里面的内容.

  public class Driver extends NonRegisteringDriver implements java.sql.Driver {

  static {

  try {

  java.sql.DriverManager.registerDriver(new Driver());

  } catch (SQLException E) {

  throw new RuntimeException("Can't register driver!");

  }

  }

  public Driver() throws SQLException {

  // Required for Class.forName().newInstance()

  }

  }

  我们发现这个类也是超级简单的.一个构造函数和一个静态代码块.我们知道,类在初始化的时候,静态代码块的内容会被执行的.也就是说我们Class.forName和直接写DriverManager.registerDriver(new Driver)两者功能是等同的.我们换成这种写法.再试试看.

  public class JdbcDemo {
  public static void main(String[] args) throws SQLException, ClassNotFoundException {
  String url = "jdbc:mysql://127.0.0.1:3306/mydb";
  String username = "root";
  String password = "redhat";
  //Class.forName("com.mysql.jdbc.Driver");
  DriverManager.registerDriver(new Driver());
  Connection connection = DriverManager.getConnection(url, username, password);
  String sql = "SELECT * FROM msg";
  PreparedStatement prepareStatement = connection.prepareStatement(sql);
  ResultSet resultSet = prepareStatement.executeQuery();
  resultSet.next();
  String address = resultSet.getString("address");
  System.out.println(address);
  }
  }

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...