PrimeFaces pagination not working and throwing exception - java

Iam using primefaces 5.0.i used data table in primefaces and displayed data from datatabase.Pagination tab displays in vertical wise doesn't display in proper manner. when i used pagination in datatable iam getting following exception
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.invoice.customerlisting.getcustomerlist(customerlisting.java:22)
at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:476)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
at org.apache.el.parser.AstValue.getValue(AstValue.java:173)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:731)
at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:867)
at org.primefaces.component.api.UIData.getDataModel(UIData.java:579)
at javax.faces.component.UIData.getRowCount(UIData.java:356)
at org.primefaces.component.api.UIData.calculateFirst(UIData.java:157)
at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:176)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:82)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:361)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
... 56 more
java.sql.SQLException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:199)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:480)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:413)
at oracle.jdbc.driver.PhysicalConnection.<init>(PhysicalConnection.java:508)
at oracle.jdbc.driver.T4CConnection.<init>(T4CConnection.java:203)
at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:33)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:510)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at com.invoice.customerlisting.getcustomerlist(customerlisting.java:22)
at sun.reflect.GeneratedMethodAccessor21.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at javax.el.BeanELResolver.invoke(BeanELResolver.java:476)
at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:161)
at org.apache.el.parser.AstValue.getValue(AstValue.java:173)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIData.getValue(UIData.java:731)
at org.primefaces.component.datatable.DataTable.getValue(DataTable.java:867)
at org.primefaces.component.api.UIData.getDataModel(UIData.java:579)
at javax.faces.component.UIData.isRowAvailable(UIData.java:343)
at org.primefaces.component.api.UIData.setRowModel(UIData.java:422)
at org.primefaces.component.api.UIData.setRowIndex(UIData.java:401)
at org.primefaces.component.datatable.DataTableRenderer.encodeTbody(DataTableRenderer.java:683)
at org.primefaces.component.datatable.DataTableRenderer.encodeRegularTable(DataTableRenderer.java:234)
at org.primefaces.component.datatable.DataTableRenderer.encodeMarkup(DataTableRenderer.java:196)
at org.primefaces.component.datatable.DataTableRenderer.encodeEnd(DataTableRenderer.java:82)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: oracle.net.ns.NetException: Listener refused the connection with the following error:
ORA-12519, TNS:no appropriate service handler found
at oracle.net.ns.NSProtocol.connect(NSProtocol.java:361)
at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:966)
at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:292)
... 59 more
Here is my jsf page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
>
<h:head></h:head>
<body>
<h:form>
<p:dataTable value="#{customerlisting.getcustomerlist()}" var="cml" style="width:25px;height:30px;" rows="3" paginatorPosition="bottom" paginator="true">
<p:column>
<f:facet name="header">
Customer Id
</f:facet>
<h:outputText value="#{cml.customerid}"></h:outputText>
</p:column>
<p:column>
<f:facet name="header">Customer Name</f:facet>
<h:outputText value="#{cml.customername}"></h:outputText>
</p:column>
<p:column><p:commandButton value="Edit"/></p:column>
<p:column><p:commandButton value="Delete"/></p:column>
</p:dataTable>
</h:form>
</body>
</html>
Here is my bean class
package com.invoice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.SessionScoped;
public class customerlisting {
public List<customer> getcustomerlist()
{
List<customer> cs=new ArrayList<customer>();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","invoice","google");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select customerid,customername from customer order by customerid asc");
while(rs.next())
{
customer cm=new customer();
cm.setCustomerid(rs.getInt(1));
cm.setCustomername(rs.getString(2));
cs.add(cm);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return cs;
}
}
My customer class is
package com.invoice;
public class customer {
int customerid;
String customername;
public String getCustomername() {
return customername;
}
public void setCustomername(String customername) {
this.customername = customername;
}
public int getCustomerid() {
return customerid;
}
public void setCustomerid(int customerid) {
this.customerid = customerid;
}
}
I'm totally stuck with this pagination in primefaces please could some one help me with good solution

You got a Problem with polluting your server and the database with db-connections. This leads to your error, the DB does not allow any further connections.
To avoid this you must close the connections and free the resources on your server, as well on the DB-server.
I will post a snippet with correct resource-handling. Doing this, I will also use correct Java Code Conventions, especially: Classnames should start with a uppercase letter.
Class.forName("oracle.jdbc.driver.OracleDriver");
List<Customer> cs = new ArrayList<Customer>();
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
con = DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE", "invoice", "google");
stmt = con.createStatement();
rs = stmt.executeQuery("select customerid,customername from customer order by customerid asc");
while (rs.next()) {
Customer cm = new Customer();
cm.setCustomerid(rs.getInt(1));
cm.setCustomername(rs.getString(2));
cs.add(cm);
}
} catch (Exception e) {
LOG.error("Error connecting to DB: " + e.getMessage(), e);
} finally {
try {
// close resources
rs.close();
stmt.close();
con.close();
} catch (Exception e) {// ignore
} finally {
// setting everything to null (allows Objects to be GC'd)
rs = null;
stmt = null;
con = null;
}
}
return cs;
Bare in mind: gathering a connection to DB is expensive. Consider using ConnectionPooling (like c3p0). ConnectionPooling will open a set of connections to the db for you and reuse them.

Your error is here:
at com.invoice.customerlisting.getcustomerlist(customerlisting.java:22)
That is this line:
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","invoice","google");
You have a problem here on connection and not in primefaces. Isn't trying to open many connections? Try to open the connection just one time in some other method, and on getcustomerlist just create the statement.
public class customerlisting {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:XE","invoice","google");
public List<customer> getcustomerlist()
{
List<customer> cs=new ArrayList<customer>();
try
{
Statement stmt=con.createStatement();

Related

java.sql.SQLException: ORA-01008: not all variables bound using Mybatis 3

So I'm using Mybatis 3 to make procedure calls. This is the first time I'm using this method to make calls to an oracle database (the other times it was a mysql database) and i get the above mentioned error. I have never had an issue making mysql database calls in the same fashion is there something special i need to do with oracle? It seems that this error message generally means that the number of parameters or '?' don't match the what's being set, but as can be seen from my xml file I am setting all the parameters.
10:07:22,079 [DEBUG] {call.authenticateCmplnr} - ooo Using Connection [oracle.jdbc.driver.OracleConnection#543d1aa8]
10:07:22,083 [DEBUG] {call.authenticateCmplnr} - ==> Preparing: CALL TDU_LOGIN_USER_CHECK( ?, ?, ?, ?, ? )
10:07:22,129 [DEBUG] {call.authenticateCmplnr} - ==> Parameters: m440109(String), TDU(String)
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.sql.SQLException: ORA-01008: not all variables bound
### The error may exist in com/cmplnr/datasource/dataservice.xml
### The error may involve call.authenticateCmplnr-Inline
### The error occurred while setting parameters
### SQL: CALL TDU_LOGIN_USER_CHECK( ?, ?, ?, ?, ? )
### Cause: java.sql.SQLException: ORA-01008: not all variables bound
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:107)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:98)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectOne(DefaultSqlSession.java:62)
at com.cmplnr.integration.AuthenticationServiceImpl.authorizeAccess(AuthenticationServiceImpl.java:80)
at com.cmplnr.integration.AuthenticationServiceImpl.authenticateCmplnr(AuthenticationServiceImpl.java:53)
at com.cmplnr.control.TDU_Control.test(TDU_Control.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:174)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:421)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:409)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1813)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.SQLException: ORA-01008: not all variables bound
at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134)
at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:289)
at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:590)
at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1973)
at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteFetch(TTC7Protocol.java:1119)
at oracle.jdbc.driver.OracleStatement.executeNonQuery(OracleStatement.java:2191)
at oracle.jdbc.driver.OracleStatement.doExecuteOther(OracleStatement.java:2064)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:2989)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:658)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:736)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:55)
at $Proxy14.execute(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:56)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:70)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:57)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:259)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:132)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:105)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:81)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:104)
... 34 more
My implementation
private User authorizeAccess(String id){
SqlSession session = sqlSessionFactory.openSession();
Map<String,Object>params = new HashMap<String,Object>();
params.put("id", id);
params.put("cycleType", "TDU");
User user = null;
try{
user = session.selectOne("call.authenticateCmplnr", params);
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
session.close();
}
return user;
}
The xml file
<mapper namespace="call">
<resultMap id = "userMap" type="com.cmplnr.core.User">
<result property="id" column="ATTRIBUTE1"/>
<result property="fullName" column="USER_NAME"/>
</resultMap>
<select id = "authenticateCmplnr" parameterType="java.util.Map">
CALL TDU_LOGIN_USER_CHECK(
#{id, mode=IN, jdbcType=VARCHAR},
#{cycleType, mode=IN, jdbcType=VARCHAR},
#{cursor, mode=OUT, jdbcType=CURSOR, resultMap=userMap},
#{errcode, mode=OUT, jdbcType=INTEGER},
#{errmsg, mode=OUT, jdbcType=VARCHAR}
)
</select>
</mapper>
Can you try putting your OUT parameters to the Map, with null values?
You can see this possibly dupe answer.
private User authorizeAccess(String id){
SqlSession session = sqlSessionFactory.openSession();
Map<String,Object>params = new HashMap<String,Object>();
params.put("id", id);
params.put("cycleType", "TDU");
params.put("cursor", null);
params.put("errcode", null);
params.put("errmsg", null);
User user = null;
try{
user = session.selectOne("call.authenticateCmplnr", params);
}catch(Exception e){
e.printStackTrace();
return null;
}finally{
session.close();
}
return user;
}

How to read properties from resource file using java

For displaying error message value in JSF page using Java when I read the name lblFathersOrGuardianName from resource mysqlmaven.properties file I got below error.
Mar 04, 2014 2:53:21 PM com.sun.faces.application.ActionListenerImpl processAction
SEVERE: java.lang.ExceptionInInitializerError
javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.ExceptionInInitializerError
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.validationRegistration(RegistrationBean.java:198)
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.submitRegistrationAction(RegistrationBean.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
Caused by: java.lang.NullPointerException
at java.util.ResourceBundle$CacheKey.calculateHashCode(ResourceBundle.java:593)
at java.util.ResourceBundle$CacheKey.<init>(ResourceBundle.java:522)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1259)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
at com.uk.mysqlmaven.util.ResourceKeys.<init>(ResourceKeys.java:12)
at com.uk.mysqlmaven.util.ResourceKeys.<clinit>(ResourceKeys.java:8)
... 34 more
Mar 04, 2014 2:53:21 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{registrationBean.submitRegistrationAction}: java.lang.ExceptionInInitializerError
javax.faces.FacesException: #{registrationBean.submitRegistrationAction}: java.lang.ExceptionInInitializerError
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
... 23 more
Caused by: java.lang.ExceptionInInitializerError
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.validationRegistration(RegistrationBean.java:198)
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.submitRegistrationAction(RegistrationBean.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
Caused by: java.lang.NullPointerException
at java.util.ResourceBundle$CacheKey.calculateHashCode(ResourceBundle.java:593)
at java.util.ResourceBundle$CacheKey.<init>(ResourceBundle.java:522)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1259)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
at com.uk.mysqlmaven.util.ResourceKeys.<init>(ResourceKeys.java:12)
at com.uk.mysqlmaven.util.ResourceKeys.<clinit>(ResourceKeys.java:8)
... 34 more
javax.faces.FacesException: #{registrationBean.submitRegistrationAction}: java.lang.ExceptionInInitializerError
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:85)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: javax.faces.FacesException: #{registrationBean.submitRegistrationAction}: java.lang.ExceptionInInitializerError
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114)
at javax.faces.component.UICommand.broadcast(UICommand.java:311)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77)
... 19 more
Caused by: javax.faces.el.EvaluationException: java.lang.ExceptionInInitializerError
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98)
... 23 more
Caused by: java.lang.ExceptionInInitializerError
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.validationRegistration(RegistrationBean.java:198)
at com.uk.mysqlmaven.jsf.beans.RegistrationBean.submitRegistrationAction(RegistrationBean.java:189)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84)
... 24 more
Caused by: java.lang.NullPointerException
at java.util.ResourceBundle$CacheKey.calculateHashCode(ResourceBundle.java:593)
at java.util.ResourceBundle$CacheKey.<init>(ResourceBundle.java:522)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1259)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
at com.uk.mysqlmaven.util.ResourceKeys.<init>(ResourceKeys.java:12)
at com.uk.mysqlmaven.util.ResourceKeys.<clinit>(ResourceKeys.java:8)
... 34 more
mysqlmaven.properties
#registration.xhtml
lblFirstName=First Name
lblMiddleName=Middle Name
lblLastName=Last Name
lblDateOfBirth=Date Of Birth
lblFathersOrGuardianName=Father's / Guardian Name
RegistrationBean.java
package com.uk.mysqlmaven.jsf.beans;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import com.uk.mysqlmaven.util.ResourceKeys;
public class RegistrationBean {
private String fathersOrGuardianName;
public String getFathersOrGuardianName() {
return fathersOrGuardianName;
}
public void setFathersOrGuardianName(String fathersOrGuardianName) {
this.fathersOrGuardianName = fathersOrGuardianName;
}
public String submitRegistrationAction() {
if (validationRegistration()) {
return "views/home.faces?faces-redirect=true&param=98";
} else {
return "";
}
}
public Boolean validationRegistration() {
if (this.fathersOrGuardianName.length() == 0) {
FacesContext.getCurrentInstance().addMessage(fathersOrGuardianName, new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceKeys.getInstance().get("lblFathersOrGuardianName"), "Fathers/GurdianName can't be empty."));
}
return Boolean.FALSE;
}
public void clearRegistrationAction(ActionEvent event) {
this.fathersOrGuardianName = "";
}
}
ResourceKeys.java
package com.uk.mysqlmaven.util;
import java.util.ResourceBundle;
public class ResourceKeys {
private static ResourceKeys instance = new ResourceKeys();
private final ResourceBundle resourceBundle;
private ResourceKeys() {
resourceBundle = ResourceBundle.getBundle(BUNDLE_NAME);
}
/**
* Return singleton instance of this class
* #return singleton instance
*/
public synchronized static ResourceKeys getInstance() {
return instance;
}
private static String BUNDLE_NAME = "/MySqlMavenJSF/com/uk/mysqlmaven/resources/mysqlmaven";
/**
* Return value of the key
* #param key
* #return value
*/
public String get(String key) {
return resourceBundle.getString(key);
}
}
How to get name from resource file in java.
mysqlmaven.properties file is placed in my project as shown in below screenshot.
mysqlmaven.properties file path is not accessed in ResourceKeys.java file.
For accessing properties file from package in java I used below path in Constants.java.
Constants.java
package com.uk.mysqlmaven.util;
public class Constants {
public class Resources {
public static final String BUNDLENAME_MYSQLMAVEN = "com.uk.mysqlmaven.resources.mysqlmaven";
}
}
ResourceKeys.java
package com.uk.mysqlmaven.util;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
public class ResourceKeys {
private static ResourceKeys instance = new ResourceKeys();
private ResourceBundle resourceBundle;
private ResourceKeys() {
}
/**
* Return singleton instance of this class
* #return singleton instance
*/
public synchronized static ResourceKeys getInstance() {
return instance;
}
/**
* Return value of the key
* #param key
* #param bundleName
* #return value
*/
public String get(String key, String bundleName) {
Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
resourceBundle = ResourceBundle.getBundle(bundleName, locale);
return resourceBundle.getString(key);
}
}
RegistrationBean.java
package com.uk.mysqlmaven.jsf.beans;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import com.uk.mysqlmaven.util.Constants;
import com.uk.mysqlmaven.util.ResourceKeys;
public class RegistrationBean {
private String fathersOrGuardianName;
public String getFathersOrGuardianName() {
return fathersOrGuardianName;
}
public void setFathersOrGuardianName(String fathersOrGuardianName) {
this.fathersOrGuardianName = fathersOrGuardianName;
}
public String submitRegistrationAction() {
if (validationRegistration()) {
return "views/home.faces?faces-redirect=true&param=98";
} else {
return "";
}
}
public Boolean validationRegistration() {
if (this.fathersOrGuardianName.length() == 0) {
String commonName = ResourceKeys.getInstance().get("lblPleaseEnter", Constants.Resources.BUNDLENAME_MYSQLMAVEN);
String displayName = ResourceKeys.getInstance().get("lblFathersOrGuardianName", Constants.Resources.BUNDLENAME_MYSQLMAVEN);
FacesContext.getCurrentInstance().addMessage(fathersOrGuardianName, new FacesMessage(FacesMessage.SEVERITY_ERROR, commonName +" "+ displayName , "Fathers/GurdianName can't be empty."));
}
return Boolean.FALSE;
}
public void clearRegistrationAction(ActionEvent event) {
this.fathersOrGuardianName = "";
}
}
Registration.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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" xmlns:p="http://primefaces.org/ui">
<f:view>
<h:head>
<title>Registration</title>
<f:loadBundle var="mysqlmaven" basename="com.uk.mysqlmaven.resources.mysqlmaven" />
<h:outputStylesheet name="style/mysqlmaven.css"></h:outputStylesheet>
</h:head>
<h:form id="registrationForm">
<table align="center">
<tr>
<td><h:panelGrid>
<h:messages id="registrationMessageId" errorClass="lblRed" warnClass="lblWarning" infoClass="lblGreen"></h:messages>
<h:message for="registrationMessageId"></h:message>
</h:panelGrid> <h:panelGrid columns="1" cellpadding="2" frame="hsides" border="0">
<h:panelGrid columns="2">
<h:panelGroup>
<p:outputLabel value="*" styleClass="lblRed"></p:outputLabel>
<p:outputLabel value="#{mysqlmaven.lblFathersOrGuardianName}"></p:outputLabel>
<p:inputText id="fathersOrGuardianNameId" label="#{mysqlmaven.lblFathersOrGuardianName}" value="#{registrationBean.fathersOrGuardianName}"></p:inputText>
</h:panelGroup>
</h:panelGrid>
</h:panelGrid> <h:panelGrid columns="2">
<p:commandButton id="submitId" value="Submit" title="Submit" actionListener="#{registrationBean.submitRegistrationAction}" update="registrationMessageId"></p:commandButton>
<p:commandButton id="clearId" value="Clear" actionListener="#{registrationBean.clearRegistrationAction}"></p:commandButton>
</h:panelGrid>
</td>
</tr>
</table>
</h:form>
</f:view>
</html>

generating a JavaAbstractMethod exception

i've written this code for adding photos to database and i'm not much into web application
so please help me out
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
Connection conn = null; // connection to the database
String message = null; // message will be sent back to client
try {
// connects to the database
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/opmdb", "root", "sourabh");
// constructs SQL statement
String sql = "INSERT INTO photos (userId, photo) values (?, ?)";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, userName);
statement.setBinaryStream(2, inputStream);
here it is telling the error i've also used setBlob but the error remains the same
Exceptions and stacktrace is
java.lang.AbstractMethodError:com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;)V
at org.onlinepicturemanager.addphotoservlet.AddPhotoServlet.doPost(AddPhotoServlet.java:81)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
change your code statement.setBinaryStream(2, inputStream);
to
statement.setBinaryStream(2, inputStream,file.length);
'setBinaryStream' method need 3 parameteres,add your file length in this method

Oracle JDBC connection issue

I'm trying to connect to Oracle dataBase in Java web project and this is the exception that I got :
java.sql.SQLException: Exception d'E/S: Got minus one from a read
call at
oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:134) at
oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:179) at
oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:333) at
oracle.jdbc.driver.OracleConnection.(OracleConnection.java:404) at
oracle.jdbc.driver.OracleDriver.getConnectionInstance(OracleDriver.java:468)
at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:314) at
java.sql.DriverManager.getConnection(Unknown Source) at
java.sql.DriverManager.getConnection(Unknown Source) at
mypack1.login.doPost(login.java:60) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:641) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at
org.ow2.jonas.web.tomcat7.CheckOpenResourcesValve.invoke(CheckOpenResourcesValve.java:69)
at
org.ow2.jonas.web.tomcat7.tx.TransactionValve.invoke(TransactionValve.java:89)
at
org.ow2.jonas.web.tomcat7.ResetAuthenticationValve.invoke(ResetAuthenticationValve.java:95)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at
org.ow2.jonas.web.tomcat7.versioning.VersioningValve.invoke(VersioningValve.java:105)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
and this's my connection code:
try {
Class.forName("oracle.jdbc.OracleDriver");
} catch (ClassNotFoundException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
out.println("<html><head><title>Hello World</title></head><body>");
out.println ("<h1>Where is your Oracle JDBC Driver?</h1>"+exceptionAsString+"</body></html>");
return;
}
out.println("<html><head><title>Hello World</title></head><body>");
out.println ("<h1>Oracle JDBC Driver Registered!</h1></body></html>");
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:oracle:thin:#localhost:3010/apex/:XE","JEE","admin");
} catch (SQLException e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
out.println("<html><head><title>Hello World</title></head><body>");
out.println ("<h1>Connection Failed! Check output console</h1>"+exceptionAsString+"</body></html>");
e.printStackTrace();
return;
}
if (connection != null) {
out.println("<html><head><title>Hello World</title></head><body>");
out.println ("<h1>You made it, take control your database now!</h1></body></html>");
} else {
out.println("<html><head><title>Hello World</title></head><body>");
out.println ("<h1>Failed to make connection!</h1></body></html>");
}

JSF interaction with database

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 = "jdbc:oracle: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>
Following is stack trace
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:787)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1252)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at erpJavaFiles.Employee.addEmployee(Employee.java:118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 23 more
Here,
} finally {
pstmt.close();
conn.close();
}
you aren't checking if they are null before you close them. If an exception is been thrown upon connecting, they will stay null. Fix it as follows:
} finally {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
}
There are no other potential causes for a NPE in the addEmployee() method. I see that you've an e.printStackTrace() call in the catch block. Read this one for the real cause of the problem. I guess that it's a ClassNotFoundException on the JDBC driver class, because you forgot to put the JDBC driver in /WEB-INF/lib folder of the webapp.

Categories

Resources