please help

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_Nam­e");
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..


Reply





Shorty May 11, 6:00 am show options

Newsgroups: comp.lang.java.programmer
From: "Shorty" <[email protected]> - Find messages by this
author
Date: 11 May 2005 03:00:37 -0700
Local: Wed,May 11 2005 6:00 am
Subject: Re: problem in integer field in java servlet
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

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.getParame­ter("Resource_id"));
(throws a NumberFormatException if "Resource_Id" is not an integer)


Reply





ashutosh May 11, 7:36 am show options

Newsgroups: comp.lang.java.programmer
From: "ashutosh" <[email protected]> - Find messages by this author

Date: 11 May 2005 04:36:58 -0700
Local: Wed,May 11 2005 7:36 am
Subject: Re: problem in integer field in java servlet
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

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.ht­ml");
} // end if


Vector
Employeelist=(Vector)session.g­etValue("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.getParame­ter("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(myResou­rce);


obj.setM_employee_passwd(myRes­ource1);


obj.setM_employee_no(myResourc­e2);
obj.insert();


//obj.fetchByPk(new
Integer(1)) ;
//String url =
"Admin_confirm.jsp";
//RequestDispatcher
dispatcher =
getServletContext().getRequest­Dispatcher("http://localhost:8080/Admin_co­nfirm.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_employe­e_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.Emp­loyeecart", 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(pr­ompt("JDBC URL:
", "jdbc:mysql:///brocade_lab"),
// prompt("User Name: ", "root"),
// prompt("Password: ", "gaurav"));
dbConn =
DriverManager.getConnection("j­dbc:mysql:///brocade_lab","roo­t","gaurav");



}catch(SQLException se){
System.err.println(se.getMessa­ge());


}catch(Exception s){
System.err.println(s.getMessag­e());


}


} // end of establisConnection()



}


------------------------------­------------------------------­--------


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


Reply





Arnaud Berger May 11, 7:44 am show options

Newsgroups: comp.lang.java.programmer
From: "Arnaud Berger" <[email protected]> - Find messages by
this author
Date: Wed, 11 May 2005 13:44:31 +0200
Local: Wed,May 11 2005 7:44 am
Subject: Re: problem in integer field in java servlet
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Report Abuse

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(myResourc­e2);


Regards,


Arnaud


"ashutosh" <[email protected]> a écrit dans le message 1115811418.835508.188...@g49g2­000cwa.googlegroups.com...



- Hide quoted text -
- Show quoted text -
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.ht­ml");
} // end if

Vector
Employeelist=(Vector)session.g­etValue("Employee.Employeecart­");
*/ file://String action = req.getParameter("action");

file://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");

file://String myResource2 = req.getParameter("Employee_id"­);
int myResource2 =
Integer.parseInt(req.getParame­ter("Employee_id"));

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

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

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

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







file://obj.fetchByPk(new Integer(1)) ;
file://String url = "Admin_confirm.jsp";
file://RequestDispatcher dispatcher =



getServletContext().getRequest­Dispatcher("http://localhost:8080/Admin_co­nfir

m.jsp");


- Hide quoted text -
- Show quoted text -
file://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");
file://out.println(obj.getM_em­ployee_name());
// out.close();
}
catch (Exception objExp){
out.println(objExp);
}
}else out.println("You have entered a Blank Text Box");
// end MyOption

file://if (Employeelist==null)

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

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



private void establishConnection()
{





file://dbConn =
DriverManager.getConnection(pr­ompt("JDBC URL:
", "jdbc:mysql:///brocade_lab"),
// prompt("User Name: ", "root"),
// prompt("Password: ", "gaurav"));
dbConn =
DriverManager.getConnection("j­dbc:mysql:///brocade_lab","roo­t","gaurav");



}catch(SQLException se){
System.err.println(se.getMessa­ge());

}catch(Exception s){
System.err.println(s.getMessag­e());



} // end of establisConnection()

------------------------------­------------------------------­--------



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



Reply





ashutosh May 11, 8:22 am show options

Newsgroups: comp.lang.java.programmer
From: "ashutosh" <[email protected]> - Find messages by this author

Date: 11 May 2005 05:22:49 -0700
Local: Wed,May 11 2005 8:22 am
Subject: Re: problem in integer field in java servlet
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

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

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top