Hello all,
I'm a bit new at JSF and Tomcat, and I'm havign some trouble connecting to a Postgres DB. I'm using the steps outlined in chapter 10 of the book "Core JavaServer Faces", but clearly I have something wrong. I've seen similar problems in various forums, but thus far none of the solutions have worked for me. My pages load fine, but any time I try to runt he form that hits the DB, I get the following error:
SQL exception Cannot create JDBC driver of class '' for connect URL 'jdbcostgresql://<host name>:5432/<database name>'
I'm fairly certain I have all the right Jar files and I know the DB connection parameters are right. I've pasted what I think are the relevant excerpts from server.xml and my applciation files below, any suggestions would be much appreciated.
*********
server.xml
*********
<!-- connection for PostgreSQL DB -->
<DefaultContext>
<Resource name="jdbc/postgres" auth="Container" type="javax.sql.DataSource" />
<ResourceParams name="jdbc/postgres">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>DriveClassName</name>
<value>org.postgresql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbcostgresql://<host name>:5432/<database name></value>
</parameter>
<parameter>
<name>username</name>
<value><username></value>
</parameter>
<parameter>
<name>password</name>
<value><password></value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>poolPreparedStatements</name>
<value>true</value>
</parameter>
</ResourceParams>
</DefaultContext>
*******
web.xml
*******
<resource-ref>
<description>DataSource Reference</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
************
Java bean file
************
public void doLogin() throws SQLException, NamingException {
Context ctx = new InitialContext();
if (ctx == null) throw new NamingException("No initial context");
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/postgres");
if (ds == null) throw new NamingException("No data source");
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException("No connection");
try {
PreparedStatement passwordQuery = conn.prepareStatement(
"SELECT password from account WHERE username = ?");
passwordQuery.setString(1, this.userName);
ResultSet result = passwordQuery.executeQuery();
if (!result.next()) return;
String storedPassword = result.getString("password");
loggedIn = this.myPassword.equals(storedPassword.trim());
}
finally {
conn.close();
}
}
I'm a bit new at JSF and Tomcat, and I'm havign some trouble connecting to a Postgres DB. I'm using the steps outlined in chapter 10 of the book "Core JavaServer Faces", but clearly I have something wrong. I've seen similar problems in various forums, but thus far none of the solutions have worked for me. My pages load fine, but any time I try to runt he form that hits the DB, I get the following error:
SQL exception Cannot create JDBC driver of class '' for connect URL 'jdbcostgresql://<host name>:5432/<database name>'
I'm fairly certain I have all the right Jar files and I know the DB connection parameters are right. I've pasted what I think are the relevant excerpts from server.xml and my applciation files below, any suggestions would be much appreciated.
*********
server.xml
*********
<!-- connection for PostgreSQL DB -->
<DefaultContext>
<Resource name="jdbc/postgres" auth="Container" type="javax.sql.DataSource" />
<ResourceParams name="jdbc/postgres">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>DriveClassName</name>
<value>org.postgresql.Driver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbcostgresql://<host name>:5432/<database name></value>
</parameter>
<parameter>
<name>username</name>
<value><username></value>
</parameter>
<parameter>
<name>password</name>
<value><password></value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>poolPreparedStatements</name>
<value>true</value>
</parameter>
</ResourceParams>
</DefaultContext>
*******
web.xml
*******
<resource-ref>
<description>DataSource Reference</description>
<res-ref-name>jdbc/postgres</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
************
Java bean file
************
public void doLogin() throws SQLException, NamingException {
Context ctx = new InitialContext();
if (ctx == null) throw new NamingException("No initial context");
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/postgres");
if (ds == null) throw new NamingException("No data source");
Connection conn = ds.getConnection();
if (conn == null) throw new SQLException("No connection");
try {
PreparedStatement passwordQuery = conn.prepareStatement(
"SELECT password from account WHERE username = ?");
passwordQuery.setString(1, this.userName);
ResultSet result = passwordQuery.executeQuery();
if (!result.next()) return;
String storedPassword = result.getString("password");
loggedIn = this.myPassword.equals(storedPassword.trim());
}
finally {
conn.close();
}
}