简体   繁体   English

什么是'Class.forName(“org.sqlite.JDBC”);'做?

[英]What does 'Class.forName(“org.sqlite.JDBC”);' do?

I am trying to create a simple app with a SQLite database. 我正在尝试使用SQLite数据库创建一个简单的应用程序。 I chose to use the SQLiteJDBC driver . 我选择使用SQLiteJDBC驱动程序

The code below is taken from the above website. 以下代码取自上述网站。 My question is about the line after public static void main... 我的问题是关于public static void main之后的行...

It reads: Class.forName("org.sqlite.JDBC"); 它读作: Class.forName("org.sqlite.JDBC");

My question is, what does this line mean? 我的问题是,这条线是什么意思? And what does it do? 它做了什么? It doesn't seem to be connected to the rest of the code. 它似乎没有连接到其余的代码。 Class.forName() should return a class, but the line seems to stand alone inside the body. Class.forName()应返回一个类,但该行似乎独立于体内。 Whatever it returns isn't used by another part of the code, that I can see. 无论它返回的是代码的另一部分都没有使用,我可以看到。

Please help clarify this. 请帮助澄清一下。 Thanks in advance. 提前致谢。

public class Test {
 public static void main(String[] args) throws Exception {
    Class.forName("org.sqlite.JDBC");
    Connection conn =
      DriverManager.getConnection("jdbc:sqlite:test.db");
    Statement stat = conn.createStatement();
    stat.executeUpdate("drop table if exists people;");
    stat.executeUpdate("create table people (name, occupation);");
    PreparedStatement prep = conn.prepareStatement(
      "insert into people values (?, ?);");

prep.setString(1, "Gandhi");
prep.setString(2, "politics");
prep.addBatch();
prep.setString(1, "Turing");
prep.setString(2, "computers");
prep.addBatch();

conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);

ResultSet rs = stat.executeQuery("select * from people;");
while (rs.next()) {
  System.out.println("name = " + rs.getString("name"));
  System.out.println("job = " + rs.getString("occupation"));
}
rs.close();
conn.close();
}
  }

It loads a class dynamically. 它动态加载一个类。 What does Class.forname method do? Class.forname方法有什么作用? is a good article about it and it also explains why database drivers needs it: 是一篇很好的文章,它也解释了为什么数据库驱动程序需要它:

Let's see why you need Class.forName() to load a driver into memory. 让我们看看为什么需要Class.forName()将驱动程序加载到内存中。 All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has static an initializer only. 所有JDBC驱动程序都有一个静态块,它使用DriverManager注册自己,而DriverManager只有静态初始化程序。

The MySQL JDBC Driver has a static initializer looks like this: MySQL JDBC驱动程序有一个静态初始化程序,如下所示:

static {
    try {
        java.sql.DriverManager.registerDriver(new Driver());
    } catch (SQLException E) {
        throw new RuntimeException("Can't register driver!");
    }
}

JVM executes the static block and the Driver registers itself with the DriverManager. JVM执行静态块,驱动程序使用DriverManager注册自身。

You need a database connection to manipulate the database. 您需要数据库连接来操作数据库。 In order to create the connection to the database, the DriverManager class has to know which database driver you want to use. 为了创建与数据库的连接,DriverManager类必须知道要使用哪个数据库驱动程序。 It does that by iterating over the array (internally a Vector) of drivers that have registered with it and calls the acceptsURL(url) method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL. 它通过遍历已向其注册的驱动程序的数组(内部为Vector)并在数组中的每个驱动程序上调用acceptsURL(url)方法来实现,有效地要求驱动程序告诉它是否可以处理JDBC URL。

The Class.forName statement is making sure that the class that implements the JDBC driver for sqlite3 is loaded and registered with the JDBC factory mechanism. Class.forName语句确保为JDBC工厂机制加载和注册实现sqlite3的JDBC驱动程序的类。

When you call DriverManager.getConnection(), it looks for classes that are registered and claim to be able to handle the connection string. 当您调用DriverManager.getConnection()时,它会查找已注册并声称能够处理连接字符串的类。 If no such class is found, it can't create the connection. 如果找不到这样的类,则无法创建连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM