我在PostgreSQL中创建了这个简单的表:
CREATE TABLE "User"(
"id" Integer NOT NULL,
"user_name" Text,
"first_name" Text,
"last_name" Text,
"last_login" Date,
"date_registered" Date,
"role" Integer,
"can_login" Integer
)
;
-- Add keys for table User
ALTER TABLE "User" ADD CONSTRAINT "Key1" PRIMARY KEY ("id")
;
我尝试从Tomcat servlet
创建简单的查询代码@Resource(name = "jdbc/DefaultDB")
private DataSource ds;
public void databaseQuery() throws SQLException
{
if (ds == null)
{
throw new SQLException("Can't get data source");
}
Connection conn = ds.getConnection();
if (conn == null)
{
throw new SQLException("Can't get database connection");
}
PreparedStatement ps = null;
try
{
ps = conn.prepareStatement("SELECT * FROM USER");
// ps.setString(1, user);
// Get data from Oracle Database
ResultSet result = ps.executeQuery();
while (result.next())
{
System.out.println(">>>>> value " + result);
}
}
finally
{
if (ps != null)
{
ps.close();
}
conn.close();
}
}
但是在插入一些测试数据之后没有任何反应。使用Java代码从PostgreSQL获取数据的正确方法是什么?