File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes servets and jdbc connectivity Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "servets and jdbc connectivity" Watch "servets and jdbc connectivity" New topic
Author

servets and jdbc connectivity

arjita mahajan
Greenhorn

Joined: Nov 10, 2012
Posts: 11
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

Joined: Oct 31, 2011
Posts: 484

You login code is incorrect. You must create the SQL statement based on the input data (user and pass) like this:



PM Me If You Want to get Urgent Help on Java Programming
Ankit Dan
Ranch Hand

Joined: Aug 31, 2012
Posts: 47



Change it to
arjita mahajan
Greenhorn

Joined: Nov 10, 2012
Posts: 11
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

Joined: Apr 25, 2010
Posts: 507

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


Regards, Prasad
SCJP 5 (93%)
arjita mahajan
Greenhorn

Joined: Nov 10, 2012
Posts: 11
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

Joined: Apr 25, 2010
Posts: 507

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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servets and jdbc connectivity
 
Similar Threads
connectivity to databse through oracle 10g
How to separate Business Logic from the servlet
about hidden from fields
problem with while loop when ResultSet.next() is called
SessionServlet and Login.jsp