Java Eclipse 帮助中的 Postgres JDBC 连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/199774/
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
Postgres JDBC connection in Eclipse Help
提问by Charlie White
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?
我正在尝试让 postgres jdbc 连接在 eclipse 中工作。使用数据源资源管理器会很好,但现在我只是想获得一个基本的连接。到目前为止,我所做的是下载 postgres JDBC 连接器。然后我尝试了两种不同的方法。首先,Preferences-> Data Management,我尝试添加 postgres 连接器。其次,我将 jar 添加到我的项目中,并尝试使用 Class.forName("org.postgresql.Driver"); 加载驱动程序。但都没有奏效。有没有人有任何想法?
Thanks, Charlie
谢谢,查理
回答by Charlie White
Here's one way to get PostgreSQL connectivity to your application:
这是使 PostgreSQL 连接到您的应用程序的一种方法:
- Get an instance of
org.postgresql.ds.PGSimpleDataSource
- Setup it with values matching to your database (see methods below)
- Proceed using the DataSource as you would use any other, I'd assume at this point you'd be interested in the
DataSource.getConnection()
method.
- 获取一个实例
org.postgresql.ds.PGSimpleDataSource
- 使用与您的数据库匹配的值设置它(请参阅下面的方法)
- 继续使用 DataSource 就像使用其他任何数据源一样,我假设此时您会对该
DataSource.getConnection()
方法感兴趣。
The proprietary methods for configuring this particular DataSource are setServerName()
, setDatabaseName()
, setUser()
and setPassword()
.
配置这个特定的数据源专有的方法是setServerName()
,setDatabaseName()
,setUser()
和setPassword()
。
I wouldn't recommend doing this for anything else than testing though and it's possible your problem lies in the way you're trying to get an instance of the object using Class.forName()
There's almost a dozen different ways to get an instance of an object with subtle differences, I suggest Googling for it since it is a subject a lot of people have already written about all over the Internet.
除了测试之外,我不建议为其他任何事情执行此操作,并且您的问题可能在于您尝试使用获取对象实例的Class.forName()
方式 几乎有十几种不同的方法可以获取具有细微差异的对象实例,我建议谷歌搜索它,因为它是一个很多人已经在互联网上写过的主题。
回答by eflles
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.
- 右键单击您的项目
- 选择物业
- 选择
Java build path
- 选择
Add external JARS..
JDBC 驱动程序的位置。
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
回答by Vjeux
I had the same problem using GWT.
我在使用 GWT 时遇到了同样的问题。
I fixed it by copying the jar file inside the "lib" folder: (Project\war\WEB-INF\lib). When you add a jar to the Build Path it seems to do the link statically, however we want the lib at run time!
我通过复制“lib”文件夹中的 jar 文件来修复它:(Project\war\WEB-INF\lib)。当您将 jar 添加到构建路径时,它似乎静态地执行链接,但是我们希望在运行时使用 lib!
Hope it fixes your problem.
希望它能解决您的问题。
回答by Jamie Carl
I was also having this problem as well and Vjeux's answer helped point me in the right direction.
我也遇到了这个问题,Vjeux 的回答帮助我指明了正确的方向。
I have a local copy of Tomcat6 that was installed and is managed by Eclipse. It was installed into '$HOME/bin/tomcat6'. To get the PostgreSQL JDBC driver working I simply copied my postgresql.jar file into the '$HOME/bin/tomcat6/lib' directory.
我有一个 Tomcat6 的本地副本,它已安装并由 Eclipse 管理。它被安装到“$HOME/bin/tomcat6”中。为了让 PostgreSQL JDBC 驱动程序正常工作,我只需将我的 postgresql.jar 文件复制到“$HOME/bin/tomcat6/lib”目录中。
Also, if you don't know where to get the driver from in the first place, try this. I'm running Ubuntu so I ran 'sudo apt-get install libpg-java' which installed the driver into '/usr/share/java/postgresql.jar' and so I just copied it from there.
另外,如果您一开始不知道从哪里获取驱动程序,请尝试此操作。我正在运行 Ubuntu,所以我运行了“sudo apt-get install libpg-java”,它将驱动程序安装到“/usr/share/java/postgresql.jar”中,所以我只是从那里复制了它。
回答by jsina
you can write this code in persistence.xml
您可以在persistence.xml中编写此代码
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/yourDataBaseName"/> <property name="javax.persistence.jdbc.user" value="postgres"/> <property name="javax.persistence.jdbc.password" value="yourPassword"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/> <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/yourDataBaseName"/> <property name="javax.persistence.jdbc.user" value="postgres"/> <property name="javax.persistence.jdbc.password" value="yourPassword"/>