什么是'Class.forName(" org.sqlite.JDBC");'做? [英] What does 'Class.forName("org.sqlite.JDBC");' do?

查看:132
本文介绍了什么是'Class.forName(" org.sqlite.JDBC");'做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SQLite数据库创建一个简单的应用程序。我选择使用 SQLiteJDBC驱动程序

I am trying to create a simple app with a SQLite database. I chose to use the SQLiteJDBC driver.

以下代码取自上述网站。
我的问题是关于public static void main之后的行......

The code below is taken from the above website. My question is about the line after public static void main...

它的内容如下: Class.forName(org.sqlite .JDBC);

我的问题是,这一行是什么意思?它做了什么?它似乎没有连接到其余的代码。 Class.forName()应该返回一个类,但该行似乎独立于体内。无论它返回的是什么,我都看不到代码的另一部分。

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. 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();
}
  }


推荐答案

它动态加载一个类。 Class.forname方法有什么用?是好的关于它的文章,它也解释了为什么数据库驱动程序需要它:

It loads a class dynamically. What does Class.forname method do? is a good article about it and it also explains why database drivers needs it:


让我们看看为什么你需要Class.forName()来加载驱动程序记忆。所有JDBC驱动程序都有一个静态块,它使用DriverManager注册自己,而DriverManager只有静态初始化程序。

Let's see why you need Class.forName() to load a driver into memory. All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has static an initializer only.

MySQL JDBC驱动程序有一个静态初始化程序,如下所示:

The MySQL JDBC Driver has a static initializer looks like this:



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




JVM执行静态块和驱动程序使用DriverManager注册自己。

JVM executes the static block and the Driver registers itself with the DriverManager.

您需要数据库连接来操作数据库。为了创建与数据库的连接,DriverManager类必须知道要使用哪个数据库驱动程序。它通过遍历已向其注册的驱动程序的数组(内部为Vector)并在数组中的每个驱动程序上调用acceptsURL(url)方法来实现,有效地要求驱动程序告诉它是否可以处理JDBC网址。

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. 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.

这篇关于什么是'Class.forName(" org.sqlite.JDBC");'做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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