aspose file tools*
The moose likes JSF and the fly likes jsf and database connection problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "jsf and database connection problem" Watch "jsf and database connection problem" New topic
Author

jsf and database connection problem

tuntun zaw
Greenhorn

Joined: Oct 23, 2007
Posts: 4
Data centric jsf application

what book i should read for data centric jsf application?

I have some problem with my jsf application using database connection.
I use jsf, managed bean and database bean.I use normal plain java.
The problem is occured during linking between managed bean and database bean.

Error is variable result might not be initialized.
If any error do not occcur, result is not generated.
The method in managed bean cannot generate result.

why?please give me some example.What book i should read.

I want to use jsf, jdbc and normal plain java.That is possible?
What effects can cause?
I have no experience in other database component such as entity bean.

My web service directory is apache tomcat.I use odbcjdbc driver and ms access database.

Here is my code.

DatabaseBean.java

package mainclasses;

import java.io.*;
import java.sql.*;

public class DatabaseBean {

private static final String jdbcDriverClass =
"sun.jdbc.odbc.JdbcOdbcDriver";
private static final String jdbcDatabaseURL =
"jdbcdbc:Employees";
private Connection conn;

public DatabaseBean() throws Exception {
try {
// Load the JDBC-ODBC driver.
Class.forName(jdbcDriverClass);
// Open a database connection.
connect();
} catch(SQLException ex) {
// Perform any cleanup.
cleanup();
// Rethrow exception.
throw(ex);
}
}

public void connect() throws SQLException {
if(conn != null) {
cleanup();
conn = null;
}
// Open a database connection.
conn = DriverManager.getConnection(jdbcDatabaseURL);
}

public void cleanup() throws SQLException {
// Close the Connection.
if(conn != null) {
conn.close();
}
}

// The onus is on the user to close the Statement that created
// the returned ResultSet.
public ResultSet query(String queryStatement)
throws SQLException {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();

rs = stmt.executeQuery(queryStatement);
} catch(SQLException ex) {

try {
if(stmt != null) {
stmt.close();
}
} catch(SQLException ex2) {
}
finally {
throw(ex);
}
}
return(rs);
}

public int insert(String insertStatement) throws SQLException {
return(update(insertStatement));
}

public int delete(String deleteStatement) throws SQLException {
return(update(deleteStatement));
}

public int update(String updateStatement) throws SQLException {
Statement stmt = null;
int numRows = -1;
try {
stmt = conn.createStatement();
numRows = stmt.executeUpdate(updateStatement);
} catch(SQLException ex) {
try {
if(stmt != null) {
stmt.close();
}
} catch(SQLException ex2) {
} finally {
throw(ex);
}
} finally {
try {
if(stmt != null) {
stmt.close();
}
} catch(SQLException ex) {
}
}
return(numRows);
}


public Connection getConnection() {
return(conn);
}
}


simplelogin.java

package mainclasses;

import java.sql.*;
import java.io.*;
import mainclasses.*;

public class SimpleLogin{
String loginname;
String password;
String result;
DatabaseBean db;

public SimpleLogin(){}

public String getLoginname(){
return loginname;
}

public void setLoginname(String loginname){
this.loginname = loginname;
}

public String getResult(){
return result;
}


public String getPassword(){
return password;
}

public void setPassword(String password){
this.password = password;
}

public String CheckValidUser(){
String query="SELECT username,password FROM login ";

try{

ResultSet rs=db.query(query);


while(rs.next()){
result=null;

if(loginname.equals(rs.getString("username"))&&password.equals(rs.getString("password")))
{
result="success";
rs.afterLast();
}
else
result="fail";
}
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}

return result;

}
}
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16140
    
  21

First of all, a warning: NEVER use MS-Access, MS-Foxpro or the jdbc/ODBC bridge for real-world webapps. It's OK for playing around, but the two databases in question aren't capable of handling multi-user workloads and you'll end up with corrupted databases in short order.

There's no special magic in doing database access using JSF. The only real trick is that if you put the database management code in a JavaBean, you want to make sure that the bean gets created before anyone tries to use it. You can do that either by having the client beans invoke the database bean's constructor explicitly or in JSF, you can make the database bean be a managed bean of greater or equal scope to the client bean and then inject that bean into the client bean(s).

There are several things that an industrial-grade webapp usually does to access databases - things like connection pooling, external parameter definitions, persistency frameworks such as Hibernate, and support frameworks like Spring. But they're all refinements. You have the basic idea already, you just need to master the art of getting things connected.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsf and database connection problem