postgresql PSQLException:基于密码的身份验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34783370/
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
PSQLException: password-based authentication
提问by Dead End
Here is the Exception:when I run my testDao file, working on springs.Is there any path given to database in springs?
这是例外:当我运行我的 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)
HERE is my >>testDao file where connection with postgresql is made
这是我的 >>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();
}
}
}
HERE is my >>springNew.xml
这是我的 >>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>
采纳答案by Dead End
I got the issue and finally resolved it firstly schema was needed to be mentioned as below
我得到了这个问题并最终解决了它首先需要提到的架构如下
conn=DriverManager.getConnection("jdbc:postgresql://localhost:5432/testdb?currentSchema=testdb&user=postgres&password=postgres");
Secondly I was closing the conn thus it was returning empty rs and was closing the connection before it could be used.
其次,我正在关闭 conn,因此它返回空的 rs 并在可以使用之前关闭连接。