Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servets and jdbc connectivity

 
arjita mahajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a login page asking user for username and password, once submitted i am passing this info to servlet which will connect to database and check if login id and password is correct or not... here is my servlet code:


I know i should be using post instead of get ...but i was having some problem in using that that i ll resolve later... as of now now my problem is that the above code is conneccting to database but not giving the desired output... ie if i remove line
" if (username.equals(rs.getString("name"))) "
(in while statement) it is giving names of all users in database ... but no output otherwise... please help...
 
Nam Ha Minh
Ranch Hand
Posts: 504
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You login code is incorrect. You must create the SQL statement based on the input data (user and pass) like this:

 
Ankit Dan
Ranch Hand
Posts: 47
Eclipse IDE Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Change it to
 
arjita mahajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks both of you for replying.. I made the changes you said and here is my code :

But now it is showing no output not even this line is executing (line no 29)
"p.println("<b><h1>hello before while "+user+"</h1>");"
please help...
 
Prasad Krishnegowda
Ranch Hand
Posts: 671
4
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arjita mahajan wrote:


How do you expect the above query to execute? It should be

arjita mahajan wrote:
But now it is showing no output not even this line is executing (line no 29)
"p.println("<b><h1>hello before while "+user+"</h1>");"
please help...

In your code, line no.29, doesn't show this.. Have you posted the correct code?

Also, initially, variable flag is set to 0, and where is this flag assigned a new value like 1 or any other value? what's the use of checking if(flag==0) when the flag is not at all set after its initialized to 0? Its always going to be true..

EDIT:
arjita mahajan wrote:


This also is not a recommended approach, use e. printStackTrace(); this gives very useful information about exceptions, why just take out message, when we can get a detailed cause of the exception..

P:S: Accessing Database from servlet? not at all a good idea/design, also, emitting HTML tags from servlet also is not a good idea, you should use JSP's for this.
If you have started learning now, start with good practices
 
arjita mahajan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all for help, my problem is solved.
P.S: yes , its true that i am new to advance java, i didn't get the reason to use jsp when during compilation it is converted to servlet (what i have read) and web container can get response through servlets only. I am suppose to create a website using java, any help is appreciated , thanks in advance
 
Prasad Krishnegowda
Ranch Hand
Posts: 671
4
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arjita mahajan wrote:i didn't get the reason to use jsp when during compilation it is converted to servlet (what i have read) and web container can get response through servlets only. I am suppose to create a website using java, any help is appreciated , thanks in advance..

Why do you write Java code, when computer understands only machine level language? You can directly program in Machine Level Language.
Wish you best of luck for creating a website using servlets (without JSP's)..

Please go through the basics of JSP's then you will come to know, why JSP (View Layer in general, can be jsp or any other thing) play a very important role in any JEE application development..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic