【问题标题】:PSQLException: password-based authenticationPSQLException:基于密码的身份验证
【发布时间】:2016-04-19 09:53:15
【问题描述】:

这里是例外:当我运行我的 testDao 文件时,在 springs 上工作。springs 中是否有任何路径给数据库?

    org.postgresql.util.PSQLException: The server requested password-based authentication, but no password was provided.
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:473)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:203)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:159)
    at org.postgresql.Driver.makeConnection(Driver.java:416)
    at org.postgresql.Driver.connect(Driver.java:283)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.test.dao.java.TestDao.getConnection(TestDao.java:37)
    at com.test.dao.java.TestDao.getTest(TestDao.java:61)
    at com.test.main.java.TestMain.main(TestMain.java:33)

这里是我的 >>testDao 文件,用于与 postgresql 建立连接

   @Component 
    public class TestDao {
    static PreparedStatement ps;
    ResultSet rs;
    Connection conn= null;

     /**
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    private Connection getConnection() throws SQLException, ClassNotFoundException,FileNotFoundException,NullPointerException{


         if(conn==null)
         {
        try {
            Class.forName("org.postgresql.Driver");
            conn = DriverManager.getConnection(
                       "jdbc:postgresql://localhost:5432/testdb?user=postgres & password=postgres");
                    conn.close();
        }
                catch(Exception e) {
            e.printStackTrace();

        }
     }
         return conn;


    }

    /**
     * @param testId
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     * @throws NullPointerException 
     * @throws FileNotFoundException 
     */
    public Test getTest(int testId) throws SQLException, ClassNotFoundException, FileNotFoundException, NullPointerException  {

            conn = getConnection();
        try {

            conn = getConnection();
            ps =conn.prepareStatement("SELECT * FROM testdb.testtab where id =?");
            ps.setInt(1, testId);
            Test test =null;
            rs = ps.executeQuery();
            if(rs.next())
            {
                test = new Test(testId, rs.getString("name"));
            }

    return test;
        }
        finally
        {
            rs.close();
            ps.close();
            conn.close();
        }


    }
    }

这里是我的 >>springNew.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd "

xmlns:context="http://www.springframework.org/schema/context">
<!-- <context-annotation-config/> -->
<context:component-scan base-package="com.test.dao"/>
</beans>

【问题讨论】:

  • Check this。您是否尝试过手动登录?
  • @TheLostMind-数据库已经打开。
  • 尝试使用DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb", "postgres","postgres");
  • @ankur-singhal - 我试过然后它给出了 org.postgresql.util.PSQLException: FATAL: database "testdb" does not exist
  • @PalakChugh - 你检查过那个数据库是否存在

标签: java spring postgresql


【解决方案1】:

我遇到了问题并最终解决了它首先需要提到以下架构

 conn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb?currentSchema=testdb&user=postgres&password=postgres");

其次,我关闭了 conn,因此它返回了空的 rs,并在连接可用之前关闭了连接。

【讨论】:

  • 感谢上帝的回答。做一些 PySpark + PostgreSQL 工作并在字符串中明确指定模式是唯一有效的方法。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 2013-02-15
  • 2019-07-22
  • 2016-04-19
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多