Jar URL规范

jar URL的语法为:

jar:<url>!/{entry}.
如:jar:http://www.example.com/ex.jar!/com/demo/Class.class

以下是官方的用法,具体见官方JavaSE API.

A Jar entry
jar:http://www.foo.com/bar/baz.jar!/COM/foo/Quux.class
A Jar file
jar:http://www.foo.com/bar/baz.jar!/
A Jar directory
jar:http://www.foo.com/bar/baz.jar!/COM/foo/
!/ is refered to as the separator.


使用URL存取jar文件的例子



package net.java2000.net;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
 * 使用URL存取jar文件。
 *
 */
public class URLGetJarFile {
  public static void main(String[] args) {
    try {
      // 创建指向jar文件的URL
      URL url = new URL("jar:http://hostname/my.jar!/");
      // 创建指向文件系统的URL
      url = new URL("jar:file:/c:/almanac/my.jar!/");
      // 读取jar文件
      JarURLConnection conn = (JarURLConnection) url.openConnection();
      JarFile jarfile = conn.getJarFile();
      // 如果URL没有任何入口,则名字为null
      String entryName = conn.getEntryName(); // null
      // 创建一个指向jar文件里一个入口的URL
      url = new URL("jar:file:/c:/almanac/my.jar!/com/mycompany/MyClass.class");
      // 读取jar文件
      conn = (JarURLConnection) url.openConnection();
      jarfile = conn.getJarFile();
      // 此时的入口名字应该和指定的URL相同
      entryName = conn.getEntryName();
      // 得到jar文件的入口
      JarEntry jarEntry = conn.getJarEntry();
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
  }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PostgreSQL是一个开源的关系型数据库管理系统。为了在Java应用程序中连接和操作PostgreSQL数据库,就需要使用PostgreSQL的JDBC驱动程序。这个驱动程序通常以一个名为"postgresql.jar"的JAR文件的形式提供。 在Java应用程序中使用PostgreSQL JDBC驱动程序,需要首先将postgresql.jar文件添加到项目的classpath中。一旦这个JAR文件添加到了classpath中,就可以使用JDBC API来连接和操作PostgreSQL数据库了。通过JDBC API,可以创建连接、执行SQL查询、获取查询结果等操作。 使用PostgreSQL的JDBC驱动程序连接数据库的代码示例如下: ```java import java.sql.*; public class PostgreSQLExample { public static void main(String[] args) { try { Class.forName("org.postgresql.Driver"); String url = "jdbc:postgresql://localhost:5432/mydatabase"; String username = "username"; String password = "password"; Connection connection = DriverManager.getConnection(url, username, password); // 执行SQL查询 Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT * FROM mytable"); // 处理查询结果 while (resultSet.next()) { System.out.println(resultSet.getString("column1")); } // 关闭连接 resultSet.close(); statement.close(); connection.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 上面的代码演示了使用PostgreSQL JDBC驱动程序连接数据库,并执行查询的过程。通过添加postgresql.jar到项目的classpath中,并按照JDBC API的规范来使用,就可以非常方便地在Java应用程序中连接和操作PostgreSQL数据库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值