本文主要是介绍Java 连接 pgsql (postgresql),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、导入包:
https://jdbc.postgresql.org/download/postgresql-9.4.1208.jar2、注意包版本:否则会报错:
org.postgresql.util.PSQLException: 不支援 10 验证类型。请核对您已经组态 ……package lgy20210521project.pgsql;import java.sql.DriverManager;import java.sql.Connection;import java.sql.SQLException;import java.sql.ResultSet;import java.sql.Statement;public class PGSQLUtil{public static void main(String []args){System.out.println("PostgreSQL JDBC...");try {Class.forName("org.postgresql.Driver");}catch (ClassNotFoundException e){System.out.println("could not find postgreSQL JDBC Driver");e.printStackTrace();return;}Connection connection = null;try {connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/数据库名","postgres","密码");}catch (SQLException e){System.out.println("Connection Failed");e.printStackTrace();return;}if (connection != null){System.out.println("connect db successful!");}else {System.out.println("Failed to make conn!");}try {Statement stmt = connection.createStatement();ResultSet rs = stmt.executeQuery("SELECT 1 AS A");if(rs!=null){rs.next();System.out.println(rs.getInt("a"));}else {System.out.println("null");}rs.close();stmt.close();connection.close();}catch (Exception exp){exp.printStackTrace();}}
}
这篇关于Java 连接 pgsql (postgresql)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!