java 什么是'Class.forName("org.sqlite.JDBC");' 做?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6740601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does 'Class.forName("org.sqlite.JDBC");' do?
提问by Jonas
I am trying to create a simple app with a SQLite database. I chose to use the SQLiteJDBC driver.
我正在尝试使用 SQLite 数据库创建一个简单的应用程序。我选择使用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. Whatever it returns isn't used by another part of the code, that I can see.
我的问题是,这条线是什么意思?它有什么作用?它似乎没有连接到代码的其余部分。Class.forName()
应该返回一个类,但该行似乎独立于主体内部。我可以看到,代码的另一部分没有使用它返回的任何内容。
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();
}
}
回答by Jonas
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 方法有什么作用?是一篇关于它的好文章,它还解释了为什么数据库驱动程序需要它:
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:
让我们看看为什么需要 Class.forName() 将驱动程序加载到内存中。所有 JDBC 驱动程序都有一个静态块,它向 DriverManager 注册自己,而 DriverManager 仅具有静态初始化程序。
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.
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.
JVM 执行静态块,驱动程序向 DriverManager 注册自己。
您需要一个数据库连接来操作数据库。为了创建到数据库的连接,DriverManager 类必须知道您要使用哪个数据库驱动程序。它通过迭代已注册的驱动程序数组(内部为 Vector)并调用数组中每个驱动程序的 acceptsURL(url) 方法,有效地要求驱动程序告诉它它是否可以处理 JDBC网址。
回答by antlersoft
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 语句确保为 sqlite3 实现 JDBC 驱动程序的类被加载并注册到 JDBC 工厂机制。
When you call DriverManager.getConnection(), it looks for classes that are registered and claim to be able to handle the connection string. If no such class is found, it can't create the connection.
当您调用 DriverManager.getConnection() 时,它会查找已注册并声称能够处理连接字符串的类。如果未找到此类,则无法创建连接。