• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsf and database connection problem

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 =
"jdbc:odbc: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;

}
}
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic