简体   繁体   English

Java JAR(库)用法

[英]Java JAR (library) usage

I've used once a GSON library in Eclipse and it was very easy. 我曾经在Eclipse中使用过GSON库,这非常容易。 Just added it as external library and in source file I've imported it and was able to use it just like any other class in Java. 刚刚将其添加为外部库,并且在源文件中,我已经将其导入,并且能够像Java中的任何其他类一样使用它。 Example: 例:

Gson gson = new Gson(); //and just use it

Recently I had to work with SQLIte database file, so I've downloaded JDBC driver library and added it in my Eclipse project. 最近,我不得不使用SQLIte数据库文件,因此我已经下载了JDBC驱动程序库并将其添加到我的Eclipse项目中。 But I've noticed that there is kind of strange (at least for me) syntax for using it. 但是我注意到使用它有一种奇怪的语法(至少对我而言)。 I've imported java.sql.* but to be able to use its classes I had to do the following: 我已经导入了java.sql.*但是为了能够使用其类,我必须执行以下操作:

 Class.forName("org.sqlite.JDBC");

I know that the return value from this command is Class object (during runtime) but as you can see from the syntax it is never used. 我知道该命令的返回值是Class对象(在运行时),但是从语法上可以看出,它从未使用过。

Please explain whats going on there and why I can't just use SQL package classes without invoking Class.forName first. 请解释发生了什么,以及为什么我不能不先调用Class.forName就使用SQL包类。

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。

Source - What does 'Class.forName("org.sqlite.JDBC");' 源代码-'Class.forName(“ org.sqlite.JDBC”);'是什么? do? 做?

Basically it lets the JVM register which implementation of the java.sql.* libraries your code is using. 基本上,它使JVM注册您的代码正在使用java.sql.*库的哪个实现。 This way you can rely on a standard interface without going through the hoops and hurdles that require implementation-level details. 这样,您可以依靠标准接口,而无需经历要求实现级详细信息的障碍。

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

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