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

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

问题描述

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

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

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

我的问题是,这条线是什么意思?它有什么作用?它似乎没有连接到代码的其余部分.Class.forName() 应该返回一个类,但该行似乎独立于主体内部.代码的另一部分没有使用它返回的任何内容,我可以看到.

请帮助澄清这一点.提前致谢.

公共类测试{public static void main(String[] args) 抛出异常 {Class.forName("org.sqlite.JDBC");连接连接 =DriverManager.getConnection("jdbc:sqlite:test.db");语句 stat = conn.createStatement();stat.executeUpdate("如果有人,则删除表;");stat.executeUpdate("创建表人(姓名,职业);");PreparedStatement prep = conn.prepareStatement("插入人们的价值观 (?, ?);");prep.setString(1, "甘地");prep.setString(2, "政治");prep.addBatch();prep.setString(1, "图灵");prep.setString(2, "计算机");prep.addBatch();conn.setAutoCommit(false);prep.executeBatch();conn.setAutoCommit(true);ResultSet rs = stat.executeQuery("select * from people;");而(rs.next()){System.out.println("name = " + rs.getString("name"));System.out.println("job = " + rs.getString("职业"));}rs.close();连接.关闭();}}

解决方案

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

<块引用>

让我们看看为什么需要 Class.forName() 将驱动程序加载到内存中.所有 JDBC 驱动程序都有一个静态块,用于向 DriverManager 注册自身,而 DriverManager 仅具有静态初始化程序.

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

静态{尝试 {java.sql.DriverManager.registerDriver(new Driver());} 捕获(SQLException E){throw new RuntimeException("无法注册驱动程序!");}}

<块引用>

JVM 执行静态块,Driver 将自己注册到 DriverManager.

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

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

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

It reads: 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. 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? 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. All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has static an initializer only.

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 executes the static block and the Driver registers itself with the 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. 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天全站免登陆