File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes JSF database interaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF database interaction" Watch "JSF database interaction" New topic
Author

JSF database interaction

Adnan Gondal
Ranch Hand

Joined: Apr 30, 2011
Posts: 32
Hello all!

Following bean's database related code is working in simple Java application but when i use it within JSF page it is giving Java null pointer exception error.
Thanks in advance.

Bean class:-

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@ManagedBean
@SessionScoped

public class Db implements Serializable{
int eId;
public int geteId() {
return eId;
}
public void seteId(int eId) {
this.eId = eId;
}
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbcracle:thin:@localhost:1521:globldb3";
String username = "scott";
String password = "tiger";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
public String addEmployee() throws Exception{
Connection conn = null;
PreparedStatement pstmt = null;
try {
int a = this.eId;
conn = getConnection();
String query = "INSERT INTO c(n) VALUES(?)";
pstmt = conn.prepareStatement(query);
pstmt.setInt(1,a);
pstmt.executeUpdate(); // execute insert statement
return "success";
} catch (Exception e) {
e.printStackTrace();
return "failure";
} finally {
pstmt.close();
conn.close();
}
}
}

Following is my JSF page

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<body>
<h:form>
<p>Enter value <h:inputText value="#{db.eId}"/> </p>
<p> <h:commandButton value="Add record" action="#{db.addEmployee}"/> </p>
</h:form>
</body>
</html>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15957
    
  19

Nothing really jumps out at me. Can you indicate which line the nullpointerexception occurs on?

Incidentally, there's a "Code" button on the message editor that can be used to wrap special tags around formatted data such as Java source code or XML. It keeps the message display from messing up your indentation.

Customer surveys are for companies who didn't pay proper attention to begin with.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 506
    
    4

What line does the nullpointerexception occur? Is it when you create your preparedstatement? It could be your database driver/data.
Also, think you're supposed to use h:body in your facelets page. xml and doctype declaration? doubt that has anything to do with your problem though.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 200

The private member is eId but your getter method is geteId().

The EL is probably looking for the public method getEId() since it automatically searches for a method name based on Camel case.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSF database interaction
 
Similar Threads
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
A Very little problem in h:dataTable implementation
How to use INSERT INTO ALL statement in jdbc prepared statement with beans
doubts in setDate(i,date,cal) API of preparedStatement
A very little jsf bean question