I am Pradeep Kundu. I am making a program in struts in which i want to access data from MySQL through struts. I am using Strut 1.3.8 , Netbean 6.7.1 and MySQL 5.5. In this program ,I want to insert, delete & search data. In insert statement , i take a number variabe with long Data Type. I found this error
java.lang.integer cannot be cast into java.lang.long
In delete page , My program is deliting data successfully but not giving any response.
In search page , when i click submit button , other page open but blanks.
These are my action classes code
InserDataAction.java
package com.myapp.struts; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.action.DynaActionForm; public class InsertDataAction extends org.apache.struts.action.Action { /* forward name="" path="" */ private static final String SUCCESS = "success"; private static final String FAILURE = "failure"; Connection con = null; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionErrors errors = new ActionErrors(); DynaActionForm actionForm = (DynaActionForm) form; int userId = (Integer) actionForm.get("userId"); String firstName = (String) actionForm.get("firstName"); String lastName = (String) actionForm.get("lastName"); int age = (Integer) actionForm.get("age"); long number = (Long) actionForm.get("number"); long size = Long.toString(number).length(); boolean flag1 = false; boolean flag2 = false; if (userId == 0 ) { errors.add("userId", new ActionMessage("error.userId.required")); } if (firstName == null || firstName.length() < 1) { errors.add("firstName", new ActionMessage("error.firstName.required")); } if (lastName == null || lastName.length() < 1) { errors.add("lastName", new ActionMessage("error.lastName.required")); } if (age == 0 || age < 16) { errors.add("age", new ActionMessage("error.age.required")); } if (number==0 || size < 10) { errors.add("number", new ActionMessage("error.number.required")); } saveErrors(request, errors); if (errors.isEmpty()) { flag1 = true; } else { flag1 = false; } try { Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql:///employee", "root", "1234"); if (!con.isClosed()) { PreparedStatement ps = (PreparedStatement) con.prepareStatement("INSERT INTO emp(`userId`,`firstName`,`lastName`,age,`number`)" + " VALUES (?,?,?,?,?) "); ps.setInt(1, userId); ps.setString(2, firstName); ps.setString(3, lastName); ps.setInt(4, age); ps.setLong(5, number); ps.execute(); } else { errors.add("SQL", new ActionMessage("error.SQLConnectivity")); } } catch (Exception ex) { errors.add("SQLException", new ActionMessage("error.SQLException")); throw new SQLException(ex.fillInStackTrace()); } finally { try { if (con != null) { con.close(); } } catch (SQLException e) { throw new SQLException(e.getSQLState() + e.fillInStackTrace()); } } saveErrors(request, errors); if (errors.isEmpty()) { flag2 = true; } else { flag2 = false; } if (flag1 == true && flag2 == true) { return mapping.findForward(SUCCESS); } else { return mapping.findForward(FAILURE); } } }
DeleteDataForm.java
package com.myapp.struts; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; /** * @author pradeep.kundu */ public class DeleteDataForm extends org.apache.struts.action.ActionForm { Connection con = null; int n; int userId; public int getuserId() { return userId; } public void setuserId(int userId) { this.userId = userId; } public DeleteDataForm() { super(); } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (getuserId()==0 || getuserId() < 1) { errors.add("userId", new ActionMessage("error.userId.required")); } try { String str = "DELETE from emp where userId =?"; Class.forName("com.mysql.jdbc.Driver").newInstance(); con = DriverManager.getConnection("jdbc:mysql:///employee", "root", "1234"); if (!con.isClosed()) { PreparedStatement ps = (PreparedStatement) con.prepareStatement(str); ps.setInt(1, userId); n = ps.executeUpdate(); if(n!=0){ System.out.println("Record is deleted"); } else { System.out.println("Record is not deleted"); } } else { errors.add("SQL", new ActionMessage("error.SQLConnectivity")); } } catch (Exception ex) { errors.add("SQLException", new ActionMessage("error.SQLException")); } finally { try { if (con != null) { con.close(); } } catch (SQLException e) { } } return errors; } }
DeleteDataAction.java
package com.myapp.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * @author pradeep.kundu */ public class DeleteDataAction extends org.apache.struts.action.Action { private static final String SUCCESS = "success"; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return null; } }
SearchDataForm.java
package com.myapp.struts; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; public class SearchDataForm extends org.apache.struts.action.ActionForm { Connection con = null; ResultSet rs; int userId; public int getuserId() { return userId; } public void setuserId(int userId) { this.userId = userId; } public SearchDataForm() { super(); } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if (getuserId()==0 || getuserId() < 1) { errors.add("userId", new ActionMessage("error.userId.required")); } try { String str = "SELECT * from emp where userId = ? " ; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql:///employee", "root", "1234"); PreparedStatement ps = con.prepareStatement(str); ps.setInt(1, userId); rs = ps.executeQuery(str); System.out.println("<html><head><title>search</title></head><body>"); System.out.println("<table><tr>"); System.out.println("<td><b>User Id</b></td>"); System.out.println("<td><b>First Name</b></td>"); System.out.println("<td><b>Last Name</b></td>"); System.out.println("<td><b>Age</b></td>"); System.out.println("<td><b>Number</b></td>"); System.out.println("</tr>"); while(rs.next()) { Integer Id = rs.getInt("userId"); String firstname = rs.getString("firstName"); String lastname = rs.getString("lastName"); Integer age = rs.getInt("age"); Integer number = rs.getInt("number"); System.out.println("<tr>"); System.out.println("<td>"+Id+"</td>"); System.out.println("<td>"+firstname+"</td>"); System.out.println("<td>"+lastname+"</td>"); System.out.println("<td>"+age+"</td>"); System.out.println("<td>"+number+"</td>"); System.out.println("</tr>"); } System.out.println("</table>"); System.out.println("</body></html>"); con.close(); ps.close(); } catch(SQLException sx) { System.out.println(sx); } catch(ClassNotFoundException cx) { System.out.println(cx); } return errors; } }
SearchDataAction.java
package com.myapp.struts; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; /** * @author pradeep.kundu */ public class SearchDataAction extends org.apache.struts.action.Action { private static final String SUCCESS = "success"; @Override public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return null ; } }
if you know about these problem , please give me suggesstion
thanks.
Ads