简体   繁体   English

与PostgreSQL的Java连接

[英]Java connectivity with PostgreSQL

有人可以告诉我如何将Java文件连接到Postgresql数据库(如果可能的话,代码为n的说明)

Google is a good start Google是一个好的开始

http://jdbc.postgresql.org/ http://jdbc.postgresql.org/

Here is an example test.java 这是一个示例test.java

import java.sql.*;

class test
{
        public static void main(String[] args) {
                String hostname="", dbname="", username="", password="";
                try {
                        int argno = 0;
                        hostname = args[argno++]; 
                        dbname = args[argno++]; 
                        username = args[argno++]; 
                        password = args[argno++]; 
                } catch (Exception ex) {
                        System.err.println("Usage: java -cp driver.jar:. test [hostname] [dbname] [username] [password]");
                        System.exit(1);
                }
                try {
                        Class.forName("org.postgresql.Driver");
                        Connection connection =
                                DriverManager.getConnection(
                                        "jdbc:postgresql://"+hostname+"/"+dbname,
                                        username,
                                        password
                                );
                        ResultSet rs = connection.createStatement().executeQuery(
                                "select version() as version"  
                        );
                        while ( rs.next() ) {
                                System.out.println(rs.getString("version"));
                        }
                } catch (Exception ex) {
                        ex.printStackTrace();
                }
        }
}

Download a current driver from JDBC download page , compile this and run like this on Unices: JDBC下载页面下载当前驱动程序 ,对其进行编译并在Unices上像这样运行:

java -cp [driver_file_name].jar:. test [hostname] [dbname] [username] [password]

On Windows: 在Windows上:

java -cp [driver_file_name].jar;. test [hostname] [dbname] [username] [password]

Just wanted to expound on Tometzky's answer for other beginners using the Netbeans IDE in UNIX like me. 只是想对像我这样在UNIX中使用Netbeans IDE的其他初学者阐述Tometzky的答案。

I want the driver to be recognized as a library in the IDE. 我希望将驱动程序识别为IDE中的库。 If you go to Tools->Libraries, you will see the current list. 如果转到工具->库,您将看到当前列表。 Hit "New Library" and type "PostgreSQL JDBC Driver" or whatever name you want to give it. 点击“新库”并输入“ PostgreSQL JDBC Driver”或您想给它的任何名称。 Then in the Classpath tab, hit "Add JAR/Folder" and point to where you've saved your downloaded driver. 然后在“类路径”选项卡中,单击“添加JAR /文件夹”,然后指向保存已下载驱动程序的位置。 I'm not sure if there is a "correct" place to store it, I think it rather depends on how you back up your system and if multiple users share it. 我不确定是否存在“正确”的存储位置,我认为它取决于您如何备份系统以及是否有多个用户共享它。 Somewhere in your home directory is fine. 主目录中的某个位置可以。

After that, make a new project of type "Java Application" and paste Tometzky's code into main. 之后,创建一个“ Java应用程序”类型的新项目,并将Tometzky的代码粘贴到main中。 In your project tree, right click on Libraries and add the JDBC driver directly to the project. 在项目树中,右键单击“库”,然后将JDBC驱动程序直接添加到项目中。 Now you don't have to worry about specifying the driver on the command line. 现在,您不必担心在命令行上指定驱动程序。

Build your project and head over to its "dist" folder. 构建您的项目,然后转到其“ dist”文件夹。 Now you can run it with the command 现在您可以使用以下命令运行它

java -jar myprojectname.jar 127.0.0.1 [dbname] [user] [pw]

That of course assumes you are connect to the database server on your own machine. 当然,这假设您已连接到自己计算机上的数据库服务器。 [user] and [pw] refer to your PostgreSQL username and pw. [user]和[pw]是指您的PostgreSQL用户名和pw。

Also, when you download the documentation it comes as a bunch of html files. 另外,当您下载文档时,它是一堆html文件。 Save them somewhere and point your browser to the index.html file (in Firefox it is File-->Open File). 将它们保存在某个位置,然后将浏览器指向index.html文件(在Firefox中为File-> Open File)。

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

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