Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple java bean problem

 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below is the simple example of JavaBean to insert the data in the database. After submitting the values of the textfields in the database(oracle), the value of LoginId is always 0 (zero)....name and password's value are setting correctly. Do we have to perform some conversion for int data types? In the database the name of columns are same as that of data types.
Also when we use '*' in the property attribute of jsp:useBean, the bean class invokes (set<property_name) setLoginid(), setName() and setPassword() methods? If this is true then at the console statement (1) should be printed, which does not? Am I wrong?

BeanPack.RegisterBean.java


index.html


register.jsp


register_instead.jsp
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Carox,

How could you set String to LoginId which is int ? So it assumes there is no setter for LoginId.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the value of LoginId is always 0 (zero).


Well, thats the default value for the int as instance variable.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65111
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the JDBC forum.
 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya its the default value but the value in the database should be what the user inputs in the form textfield......not the default value...
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Did you change like i said earlier ?
The old one will not compile first ? Storing String in int ?


# private int LoginId;
#
# public void setLoginId(String login)
# {
# LoginId=login; // this will not work
# System.out.println("setLoginId is invoked");------------------------------(1)
# }
 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry....I was trying all the possible ways to solve the problem and by mistake I forgot to rectify that. I have edited the post now. There could be two ways:
I want LoginId field should be of int type,

1.

2.
In both the cases, this field value in the database is 0 irrespective of what the user has entered.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Both will not work. Its not a valid bean at all. For a valid bean the setter argument and getter return type must match. In this case use int for both setter and getter and declare the variable as int. Java Bean does the cast from String to int for you.

Also ,

use jsp:setProperty inside the jsp:useBean , though it does not make difference in this context .


In both the cases, this field value in the database is 0 irrespective of what the user has entered.


What about the other fields , they got stored ?
 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was not aware of that rule, neways I have remodified the code posted in my first post. Yes other two values i.e name and password are called correctly. On the execution of the <jsp:getProperty> tag, only the loginId is printed as 0 on the HTML page otherwise the other two values are showing user inputs...... I am not able to catch where the problem might be....
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yes there are rules that Java Bean should be conform to. This allows the flexiblity to call using reflection and set the properties with out ever knowing any details.

Ok back to the problem. use "loginId " and not LoginId. This code below should work.



 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used "register_instead.jsp" (posted in my first post) instead of "register.jsp" and its working.... on the server the output is also coming:
setLoginId is invoked

which was not coming before. The value of LoginId field in the oracle database is what I gave in the html page instead of zero that was stored before.

But the thing is what chages I have done in registed.jsp to make it register_instead.jsp-
I am just explicitly setting the value through param attribute in register_instead.jsp:

<jsp:setProperty name="register" property="LoginId" param="LoginId"/>
which was before:
<jsp:setProperty name="register" property="*" />

In the html page the name of text field of login id is:

Login Id: <input type="text" name="LoginId"><br>

which is correct and same as that of property name in bean class. Then why register_instead.jsp is working and not register.jsp?




 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carox,

Dint you read my pervious reply. I mentioned not to use "LoginId" and instead use "loginId" as property in the bean. And in the html change it as name="loginId".

A variable name should start with lowercase , which is java standard and especially if you have to use JavaBeans , you got follow the nomenclatures defined.

 
carox kaur
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya thanks its working now.....my faculty has told only 3 rules excluding the one you mentioned (it should be packaged, should have getter and setter method, should have default constructor)......
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yeah practice makes perfect. you are welcome.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic