problem in integer field in java servlet

A

ashutosh

i m making a servlet which stores two field (one is interger type and
other is string type) in mysql database. when i do so i call java beans
to save into the database.
i m comfortable to store any string value into the database by the
following code:
String myResource = req.getParameter("Resource_Name");
if (myResource.length() != 0)
{

establishConnection();
DaoResources obj = new DaoResources(dbConn);

obj.setM_r_name(myResource);
obj.insert();
}

Here DaoResources is javaBean of the Resources table in the database.
and Resources_Name is of type varchar.
but i don't know how to write code for an integer number like
Resource_id. Because when i give
int myResource1 = req.getParameter("Resource_id");
....
obj.setM_r_id(myResource1);
obj.insert();
it gives a compile time error.
please help..
 
S

Shorty

Maybe post your compile error ?

I guess, the error you get is that getParameter(String) doesn't return
an int.
It returns a String that you will need to convert into an int.
Try
int myResource1 = Integer.parseInt(req.getParameter("Resource_id"));
(throws a NumberFormatException if "Resource_Id" is not an integer)
 
A

ashutosh

here i m sending the complete code for which i m working:

import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.*;



public class EmployeeServlet extends HttpServlet {

Connection dbConn = null;
public void init(ServletConfig conf) throws ServletException
{
super.init(conf);
} // end init

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{

/* HttpSession session = req.getSession(false);
if (session == null)
{
res.sendRedirect("http://localhost:8080/error.html");
} // end if

Vector
Employeelist=(Vector)session.getValue("Employee.Employeecart");
*/ //String action = req.getParameter("action");

//any previous buys of same cd?
///boolean match=false;

res.setContentType("text/html");
PrintWriter out = res.getWriter();
String myResource = req.getParameter("empname");
String myResource1 = req.getParameter("Password");

//String myResource2 = req.getParameter("Employee_id");
int myResource2 =
Integer.parseInt(req.getParameter("Employee_id"));

// integer test = Integer.parseInt(myResource);
if (myResource.length() != 0)
{


//out.println(myResource);
//out.println("you are correct");

establishConnection();
DaoEmployee obj = new DaoEmployee(dbConn);

//int test = Integer.parseInt(myResource2);
//DaoEmployee obj1 = new DaoEmployee(dbConn);
try
{

obj.setM_employee_name(myResource);

obj.setM_employee_passwd(myResource1);

obj.setM_employee_no(myResource2);
obj.insert();

//obj.fetchByPk(new Integer(1)) ;
//String url = "Admin_confirm.jsp";
//RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher("http://localhost:8080/Admin_confirm.jsp");
//dispatcher.forward(req,res);
out.println("the record has been added");
out.println("the record2 has been added");
out.println("the record3 has been added");
//out.println(obj.getM_employee_name());
// out.close();
}
catch (Exception objExp){
out.println(objExp);
}
}else out.println("You have entered a Blank Text Box");

// end MyOption


//if (Employeelist==null)

/*--gaurav //add first cd to the cart
Employeelist = new Vector(); //first order
Employeelist.addElement(obj);
} // end if
// shount b herere } // end do post

session.putValue("Employee.Employeecart", Employeelist);
String url="/jsp/employee.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
}
--- gaurav */


}


private void establishConnection()
{

try{

Class.forName("com.mysql.jdbc.Driver");

//dbConn = DriverManager.getConnection(prompt("JDBC URL:
", "jdbc:mysql:///brocade_lab"),
// prompt("User Name: ", "root"),
// prompt("Password: ", "gaurav"));
dbConn =
DriverManager.getConnection("jdbc:mysql:///brocade_lab","root","gaurav");


}catch(SQLException se){
System.err.println(se.getMessage());

}catch(Exception s){
System.err.println(s.getMessage());

}

} // end of establisConnection()

}
--------------------------------------------------------------------

and in this code when i compile the following error message comes:
EmployeeServlet.java:59: setM_employee_no(java.lang.Integer)in
DaoEmployee can not be applied to (int)
obj.setM.employee_no(myResource2);
 
A

Arnaud Berger

Hi,

Of course :

setM_employee_no expects an Integer Object, and it receives an int value
instead !

You may want to call :
obj.setM_employee_no(new Integer(myResource2));

instead of :
obj.setM_employee_no(myResource2);

Regards,

Arnaud
 
A

ashutosh

thanx arnaud

but now my servlet is compiling without any errors but when i store the
data into the database through my jsp page employee_id field is not
taking the values which i give.It takes bydefault 0 value. employee_no
is the primary key atribute in our database.please reply
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top