I have a form that inserts into a database however before I preform the insert I check if there are existing records. I am unable to insert the record and I am getting a StackOverflow Error.
Can someone tell me what causes this error and what I can do to get rid of it? If I remove the check hasRecords the code preforms fine. Its almost like the code doesn't want to access the database at this point. I have created different types of function to preform he check here for records but at this point I keep getting the same error StackOverflow
Code
com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
public String getMonitoringStDate(Integer crimeRecNo,
Integer socialSecurityNumber) throws Exception {
return this.getMonitoringStDate(crimeRecNo, socialSecurityNumber);
}
DAO for getMonitoringStDate
public String getMonitoringStDate(Integer crimeRecNo, Integer socialSecurityNumber){
//select the minimum start date in event there is several records although this will not be allowed
String sql = "select IFNULL(min(monitoringStDate),'0') as monitoringStDate from tblmonitoring where crimeRecNo = ? and socialSecurityNumber = ?";
String monitoringStDate = (String)getJdbcTemplate().queryForObject(sql, new Object[]{crimeRecNo,socialSecurityNumber}, String.class);
return monitoringStDate;
}
I also have the following listeners registered:
Listeners
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
Code
DAO
public boolean hasMonitoringRecord(Integer crimeRecNo, Integer socialSecurityNumber){
//select a count of monitoring records there should be only one monitoring record
String sql = "select count(*) as monitoringStDate from tblmonitoring where crimeRecNo = ? and socialSecurityNumber = ?";
Integer count = (Integer)getJdbcTemplate().queryForObject(sql, new Object[]{crimeRecNo,socialSecurityNumber}, Integer.class);
if(count > 0 ){
return true;
}else{
return false;
}
}
Controller
if(result.hasErrors()){
myMonitoringTypeList.put("monitoringTypeList",this.monitoringTypeManager.getListOfMonitoringType());
model.addAttribute("icon", "ui-icon ui-icon-circle-close");
model.addAttribute("results", "Error: Unable to Save Record!");
model.addAttribute("monitoringType",myMonitoringTypeList);
model.addAttribute("monitoring",monitoring);
model.addAttribute("records", session.getAttribute("records"));
model.addAttribute("crimeRecNo",session.getAttribute("crimeRecNo"));
//return the user to the page they were on initially not the first page but the one with the error
return new ModelAndView("monitoringList","page",session.getAttribute("page"));
}else{
int crimeRecNo = monitoring.getCrimeRecNo();
int socialSecurityNumber = monitoring.getSocialSecurityNumber();
logger.info("No errors going to process records");
logger.info("CrimeRecNo is " + monitoring.getCrimeRecNo());
logger.info("SocialSecurity Number is " + monitoring.getSocialSecurityNumber());
//check if a record already exists. if one exist do an update else do an insert
//to check if the record exists we can simply get the start date
String dbMonitoringStDate = monitoringManager.getMonitoringStDate(crimeRecNo,socialSecurityNumber);
logger.info("The dbMonitoringStDate is " + dbMonitoringStDate);
if(monitoringManager.hasMonitoringRecord(crimeRecNo, socialSecurityNumber)){
logger.info("Has monitoring record");
//do an update
monitoringManager.updateMonitoringRecord(monitoring);
model.addAttribute("icon", "ui-icon ui-icon-circle-check");
model.addAttribute("results", "Record Was Updated");
}else {
logger.info("Does not have monitoring record");
//do an insert
monitoringManager.saveMonitoringRecord(monitoring);
model.addAttribute("icon", "ui-icon ui-icon-circle-check");
model.addAttribute("results", "Record Was Saved");
}
Error
7035 [http-bio-8084-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade#6f138f45
7035 [http-bio-8084-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.StackOverflowError
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:972)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
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:936)
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:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
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.lang.StackOverflowError
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
at com.crimetrack.service.MonitoringManager.getMonitoringStDate(MonitoringManager.java:60)
Look at your method definition:
public String getMonitoringStDate(Integer crimeRecNo,
Integer socialSecurityNumber) throws Exception {
return this.getMonitoringStDate(crimeRecNo, socialSecurityNumber);
}
The only thing this method does is calling itself. So obviously, you have an infinite recursion loop.
Related
I got Null pointer exception
"java.lang.NullPointerException"
refer below code for reference
Everything should be fine but not able to update data to the database(mySql)
<% try{
//reading request parameters
String projectName=request.getParameter("projectName").trim();
String status=request.getParameter("status").trim();
//String user = session.getAttribute("user").toString();
Connection connection = DAO.getConnection();
PreparedStatement pstmt = connection.prepareStatement("update project set status=? where projectName=?");
pstmt.setString(1,status);
pstmt.executeUpdate();
out.println("<h2 style='color:green'>Project Status has been updated</h2>");
}
catch(Exception ex)
{
ex.printStackTrace();
out.println("<h2 style='color:red'>Please login to update your Status</h2>");
}
%>
Not able to update because of exception
refer below
Please anyone suggest me how to solve this exception...
java.lang.NullPointerException at org.apache.jsp.MyWorkPanelAction_jsp._jspService(MyWorkPanelAction_jsp.java:93)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
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:502)
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:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
You missed the projectName...Try the code below
try{
//reading request parameters
String projectName=request.getParameter("projectName").trim();
String status=request.getParameter("status").trim();
//String user = session.getAttribute("user").toString();
Connection connection = DAO.getConnection();
PreparedStatement pstmt = connection.prepareStatement("update project set status=? where projectName=?");
pstmt.setString(1,status);
pstmt.setString(2,projectName); //Try this
pstmt.executeUpdate();
out.println("<h2 style='color:green'>Project Status has been updated</h2>");
}
catch(Exception ex)
{
ex.printStackTrace();
out.println("<h2 style='color:red'>Please login to update your Status</h2>");
}
This is the error I get while I try to cast my LONGTEXT object from mysql back to my ProcessInputs java object using FlexJSON.
java.lang.ClassCastException: java.util.HashMap cannot be cast to org.juzzy.server.ProcessInputs
at org.juzzy.server.FLSDAO.RetrieveFLSFromDatabase(FLSDAO.java:68)
at org.juzzy.client.CreateFLS.doPost(CreateFLS.java:48)
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.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(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
This is what my ProcessInput class looks like
public class ProcessInputs {
//inputs contain names of all the inputs and their objects
Map<String,Input> inputs= new LinkedHashMap<String,Input>();
Map<String,Output> outputs= new HashMap<String,Output>();
Map<String,Object> fuzzyTerms=new HashMap<String,Object>();
Map<String,String> fuzzyTermMapper=new HashMap<String,String>();
ArrayList<T1_Antecedent> antecedentArrayList;
String outputxml;
T1_Rulebase rulebase = new T1_Rulebase(1);
//getters,setters, constructor and functions
}
My retrieval code in FLEXJSON:
while(rs.next()){
String jsonInString= rs.getString("fls");
JSONDeserializer<ProcessInputs> der = new JSONDeserializer<ProcessInputs>();
ProcessInputs fls = der.deserialize(jsonInString);
}
This is how the serialization happens:
public boolean InsertFLStoDatabase(ProcessInputs object, String xml) throws IOException {
Connection connection = DatabaseConnect.getConnection();
String query = "insert into fls_systems (fls, xml) values (?,?)";
try {
JSONSerializer ser = new JSONSerializer();
String jsonInString = ser.deepSerialize(object);
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, jsonInString);
From your exception this should work (i guess...):
while(rs.next()){
String jsonInString= rs.getString("fls");
JSONDeserializer<ProcessInputs> der = new JSONDeserializer<ProcessInputs>();
// exception is here, right? der.deserialize(String) is giving you a Map!!
Map<String,Input> fls = der.deserialize(jsonInString);
}
Don't have a deep knowledge of flexjson, but I'm not casting like
Map<String,Input> fls = (Map<String,Input>) der.deserialize(jsonInString);
Because of the message:
java.util.HashMap cannot be cast to org.juzzy.server.ProcessInputs
And because API:
Deserialize the given json formatted input into a Java object.
But may be a possibility if first option does not work.
I get an error when I am trying to insert records using prepared statement:
Registration Servlet INSIDE try block:
Printing USER Values
====================
john
smith
john#gmail.com
john
Connection is: null
username: rth
password: rth
driver: oracle.jdbc.OracleDriver
url is: jdbc:oracle:thin:#localhost:1521:orcl
####Record insertion error in Registration DAO####
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.RTH.commons.DBConnection.getConnection(DBConnection.java:42)
at com.RTH.DAO.RegistrationDAO.insert(RegistrationDAO.java:29)
at com.RTH.Registration.RegistrationServlet.doPost(RegistrationServlet.java:43)
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:936)
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:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:#localhost:1521:orcl
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.RTH.commons.DBConnection.getConnection(DBConnection.java:49)
at com.RTH.DAO.RegistrationDAO.insert(RegistrationDAO.java:29)
at com.RTH.Registration.RegistrationServlet.doPost(RegistrationServlet.java:43)
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:936)
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:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
java.lang.NullPointerException
at com.RTH.DAO.RegistrationDAO.insert(RegistrationDAO.java:31)
at com.RTH.Registration.RegistrationServlet.doPost(RegistrationServlet.java:43)
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:936)
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:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
I am able to print connection details and also the properties file:
public class DBConnection {
public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException
{
Properties props = new Properties();
props.load(DBConnection.class.getResourceAsStream("DBDetails.properties"));
String username = props.getProperty("dbusername");
String password = props.getProperty("dbpassword");
String driver = props.getProperty("driver");
String url = props.getProperty("url");
Class.forName(driver);
Connection con = DriverManager.getConnection(url, username, password);
System.out.println("Connection is: " + con);
System.out.println("username: " + username);;
System.out.println("password: " + password);;
System.out.println("driver: "+ driver);;
System.out.println("url is: " + url);;
return con;
}
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
DBConnection.getConnection();
}
}
RegistrationDao file:
public class RegistrationDAO {
public void insert(User user) {
try {
System.out.println("Printing USER Values");
System.out.println("====================");
System.out.println(user.getFirstName());
System.out.println(user.getLastName());
System.out.println(user.getEmail());
System.out.println(user.getPassword());
Connection con = DBConnection.getConnection();
String query = "insert into TBL_USER(USR_FIRST_NAME,USR_LST_NAME,USR_PRIMARY_EMAIL,USR_PASSWORD) values(?,?,?,?)";
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, user.getFirstName()); //prints value
pst.setString(2, user.getLastName()); //prints value
pst.setString(3, user.getEmail()); //prints value
pst.setString(4, user.getPassword()); //prints value
pst.executeUpdate();
} catch (Exception e) {
System.out.println("####Record insertion error in Registration DAO####");
e.printStackTrace();
}
}
}
Properties file:
dbusername=rth
dbpassword=rth
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:#localhost:1521:orcl
I have checked that the database username and password is not expired and still working. Connection test succeeded. All the column types are varchar2 200 bytes. I am not sure why it is still throwing error.
You are able to see the sysout statement as you must be running the program in your IDE where the properties file is in your classpath. Your properties file is not in your classpath when you deploy the app.
You can change the code as :
props.load(DBConnection.class.getResourceAsStream("../conf/DBDetails.properties"));
Create a directory 'conf' || to the lib folder containing your code(jars) and try running it.
The problem occurs a lot earlier in this very first line of your exception:
props.load(DBConnection.class.getResourceAsStream("DBDetails.properties"));
That means: the file "DBDetails.properties" is obviously not in the current directory nor in one of the class path settings.
Check, where the file is located and set the classpath accordingly or move it in to the right place.
EDIT AFTER UPDATE OF Q
Same with the oracle driver. The oracle driver class is not loaded, probably it's not on the class path as well - or even not present at all (what I would bet for, as this needs to be downloaded/installed explicitly from Oracle)
java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
Following these steps resolved my issue:
Removed ojdbc6-11.1.0.7.0.jar from the class path and added
ojdbc6.jar
Changed the driver name from:
driver=oracle.jdbc.OracleDriver to driver=oracle.jdbc.driver.OracleDriver
Pasted a ojdbc6.jar file to
apache-tomcat-7.0.35-windows-x86\apache-tomcat-7.0.35\lib folder
And it worked!!
I have Java Spring MVC application. I am trying to do CRUD on one table, say Employee.
When I am trying to create a new Employee, it saves and adds a new entry in database. But when I am trying to update an Employee, I am updating that row, and I am trying to add a new row, with the old values of the same employee and setting one field to false.
Here is my ServiceImpl
public class EmployeeServiceImpl extends
GenericManagerImpl<Employee, Long> implements
EmployeeService {
#Override
public void save(Employee employee) {
Employee newEmpl = employee;
Date date = new Date();
Long empKey = employee.getEmpKey();
if (empKey) != null) {
oldEmp = (Employee) employeeDao.get();
oldEmp.setActive(false);
oldEmp.setEmpKey(null);
employeeDao.save(oldEmp);
} else {
employee.setCreated(date);
}
employee.setUpdated(date);
employee.setActive(true);
employeeDao.save(employee);
}
}
When I am creating a new entry, it is working fine. But when I am trying to update one entry, then it showing the exception
org.springframework.orm.hibernate4.HibernateSystemException: identifier of an instance of com.table.model.Employee was altered from 4 to null; nested exception is org.hibernate.HibernateException: identifier of an instance of com.table.model.Employee was altered from 4 to null
at org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:206)
at org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:606)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:488)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:392)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at com.sun.proxy.$Proxy77.save(Unknown Source)
at com.table.webapp.controllers.EmployeeController.saveEmployee(EmployeeController.java:164)
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.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
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.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
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:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
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:745)
Caused by: org.hibernate.HibernateException: identifier of an instance of com.table.model.Employee was altered from 4 to null
at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:194)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:156)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:225)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1213)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:402)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:480)
... 43 more
I have checked this SO question, but they are using Query. My logic is in the Service Impl and not in DAO.
How can I alter the id and save as a new entry in database?
Thanks.
You shouldn't do that - it's in the JPA spec somewhere that one must not change a primary key.
What you could do is add a clone/copy method to your Employee class and use that to copy everything but the ID and then persist that new object.
Cheers,
So I have a register Form that is able to send a profile picture to be stored in my database using ibatis but when I try to do the insert i get a nullpointerexception
stacktrace
SEVERE: Servlet.service() for servlet [volts] in context with path [/Mugenjou] threw exception [Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in com/mugenjou/integration/sql.xml.
--- The error occurred while applying a parameter map.
--- Check the sql.register-InlineParameterMap.
--- Check the parameter mapping for the 'picture' property.
--- Cause: java.lang.NullPointerException; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/mugenjou/integration/sql.xml.
--- The error occurred while applying a parameter map.
--- Check the sql.register-InlineParameterMap.
--- Check the parameter mapping for the 'picture' property.
--- Cause: java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.ibatis.sqlmap.engine.type.UnknownTypeHandler.setParameter(UnknownTypeHandler.java:69)
at com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap.setParameter(ParameterMap.java:166)
at com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap.setParameters(ParameterMap.java:126)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeUpdate(SqlExecutor.java:78)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.sqlExecuteUpdate(MappedStatement.java:216)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeUpdate(MappedStatement.java:94)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.insert(SqlMapExecutorDelegate.java:393)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.insert(SqlMapSessionImpl.java:82)
at org.springframework.orm.ibatis.SqlMapClientTemplate$8.doInSqlMapClient(SqlMapClientTemplate.java:366)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:200)
at org.springframework.orm.ibatis.SqlMapClientTemplate.insert(SqlMapClientTemplate.java:364)
at com.mugenjou.integration.BusinessServiceImpl.registerUser(BusinessServiceImpl.java:76)
at com.mugenjou.control.controller.register(controller.java:101)
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)
Once I get the file I tried converting it to a form that could be read using these too methods, both yield the same results as shown in the stacktrace. One converts to a SerialBlob the other to a ByteArrayInputStream.
private ByteArrayInputStream convertMultipartFile(MultipartFile file) throws IOException, SQLException {
byte[] bytes = file.getBytes();
return new ByteArrayInputStream(bytes);
//return new SerialBlob(bytes);
}
SQL.XML:
<insert id ="register" parameterClass ="java.util.Map">
INSERT INTO ACCOUNT_INFO (FNAME, LNAME, ACCOUNT_NAME, PASSWORD, STATUS, CREATION_DATE, LAST_ACCESSED, PROFILE_PICTURE)
VALUES (#fname#, #lname#, #account_name#, #pwd#, 0, #creation_date#, #last_accessed#, #picture#)
</insert>
BusinessServiceImpl
#Override
#SuppressWarnings({ "unchecked", "rawtypes" })
public void registerUser(ByteArrayInputStream picture, String fname, String lname, String account_name,
String pwd) {
sqlMap = getSqlMapClientTemplate();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
Map params = new HashMap();
params.put("fname", fname);
params.put("lname", lname);
params.put("account_name", account_name);
params.put("pwd", pwd);
params.put("creation_date", dateFormat.format(date));
params.put("last_accessed", "0000-00-00 00:00:00");
params.put("picture", picture);
sqlMap.insert("sql.register", params);
}
Solved this by converting the MultipartFile to an InputStream and then storing