Eclipse帮助中的Postgres JDBC连接 [英] Postgres JDBC connection in Eclipse Help
问题描述
我正在尝试在eclipse中使用postgres jdbc连接。使用Data Source Explorer会很不错,但是现在我只想尝试获得基本连接。到目前为止我所做的是下载postgres JDBC连接器。然后我尝试了两种不同的东西。首先,首选项 - >数据管理,我试图添加postgres连接器。其次,我将jar添加到我的项目中并尝试使用Class.forName(org.postgresql.Driver)加载驱动程序;但都没有奏效。有没有人有任何想法?
谢谢,
查理
这就是我建立连接的方式:(我不知道这是否是最佳做法,但它确实有效。)
导入驱动程序:
- 右键点击您的项目
- 选择房产
- 选择
Java构建路径
- 选择
添加外部JARS ..
并选择这个是我的代码:尝试{
Class.forName(org.postgresql.Driver);
} catch(ClassNotFoundException cnfe){
System.out.println(找不到JDBC驱动程序!);
System.exit(1);
}
连接conn = null;
try {
conn = DriverManager.getConnection
(String url,String user,String password);
} catch(SQLException sqle){
System.out.println(无法连接);
System.exit(1);
}
网址可以采用以下格式之一:
jdbc:postgresql:database
jdbc:postgresql:// host / database
jdbc:postgresql:// host:port / database
I'm trying to get a postgres jdbc connection working in eclipse. It would be nice to use the Data Source Explorer, but for now I'm just trying to get a basic connection. What I have done so far is download the postgres JDBC connector. I then tried two different things. First, Preferences-> Data Management, I tried to add the postgres connector. Second, I added the jar to my project and tried to load the driver using Class.forName("org.postgresql.Driver"); but neither worked. Does anyone have any ideas?
Thanks, Charlie
解决方案This is how I have made a connection: (I do not know if this is "best practice", but it works.)
Importing the driver:
- Right click on your project
- Choose property
- Choose
Java build path
- Choose
Add external JARS..
and select the location to the JDBC driver.
Here is my code:
try{ Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException cnfe){ System.out.println("Could not find the JDBC driver!"); System.exit(1); } Connection conn = null; try { conn = DriverManager.getConnection (String url, String user, String password); } catch (SQLException sqle) { System.out.println("Could not connect"); System.exit(1); }
The url can be of one of the following formats:
jdbc:postgresql:database jdbc:postgresql://host/database jdbc:postgresql://host:port/database
这篇关于Eclipse帮助中的Postgres JDBC连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!