将PostgreSQL连接到Java动态Web项目
我正试图让我的java动态web项目与postgresql数据库一起工作,但我不知道如何正确地做到这一点 我所做的:将PostgreSQL连接到Java动态Web项目,java,postgresql,jakarta-ee,tomcat8,Java,Postgresql,Jakarta Ee,Tomcat8,我正试图让我的java动态web项目与postgresql数据库一起工作,但我不知道如何正确地做到这一点 我所做的: 创建在TomcatV8.0上运行的动态Web项目 下载postgresql jdbc驱动程序 将postgresql-9.4-1205.jdbc4.jar文件放入我的WEB-INF/lib 文件夹 创建数据库java_lab2,使用密码qwerty创建用户lab2,将所有权限授予用户lab2并创建表users,所有这些都在psql中 创建META-INF/context.xml文
postgresql-9.4-1205.jdbc4.jar
文件放入我的WEB-INF/lib
文件夹java_lab2
,使用密码qwerty
创建用户lab2
,将所有权限授予用户lab2
并创建表users
,所有这些都在psql中META-INF/context.xml
文件WEB-INF/WEB.xml
文件这是我的context.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/connectorDs" auth="Container"
type="javax.sql.DataSource" username="lab2" password="qwerty"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://localhost:8080/java_lab2"
maxTotal="10" maxIdle="4" />
</Context>
现在我不知道如何真正开始使用数据库 我如何连接到它?连接后,如何在servlet中查询它 更新 我在servlet中添加了以下函数:
private void attemptConnection() {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:8080/java_lab2", "lab2",
"qwerty");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}
并在doGet
方法中运行它
这为我提供了以下输出:
PostgreSQL JDBC驱动程序已注册
连接失败!检查输出控制台
org.postgresql.util.PSQLException:连接尝试
失败。在
org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:257)
在
org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65)
………........................ 原因: 位于的java.io.EOFException org.postgresql.core.PGStream.ReceiveChar(PGStream.java:284)位于 org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:405) 在 org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:203) ... 35多
. 另外,localhost:8080是Tomcat web服务器的地址。默认情况下,PostgreSQL在端口5432上运行(如文档中所示)。@JBNizet是的,端口是问题所在,我必须在我的函数以及context.xml中更改它。你能把它作为一个答案发布,这样我就可以接受了吗?没错,你需要指定Postgres侦听的端口,而不是Tomcat端口。另外:如果定义了JNDI数据源,则不需要(也不应该使用)
DriverManager.getConnection()
。有关示例,请参阅JDBC教程:@DenisYakovenko阅读文档。端口并不是主要问题:您在Tomcat中定义了一个连接池,但没有向Tomcat提供驱动程序,而且您在代码中也没有使用该池:每次使用DriverManager都会创建一个全新的连接。只需按照文档中的说明操作即可。这就是它的目的。
private void attemptConnection() {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your PostgreSQL JDBC Driver? "
+ "Include in your library path!");
e.printStackTrace();
return;
}
System.out.println("PostgreSQL JDBC Driver Registered!");
Connection connection = null;
try {
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:8080/java_lab2", "lab2",
"qwerty");
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database now!");
} else {
System.out.println("Failed to make connection!");
}
}