By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,651 Members | 1,316 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,651 IT Pros & Developers. It's quick & easy.

how to write query for access session query with database

P: 52
Expand|Select|Wrap|Line Numbers
  1. ResultSet res = st.executeQuery("select * from regist where Password='"+User_Password+"'");
  2.             if(res.next()==true)
  3.             {
  4.                 ps = con.prepareStatement("update regist set Password=?, Confirm_Password=? where Email_Id='"+session.getAttribute( "theEmail" )+"'");
  5.                 ps.setString(1, User_NewPassword);
  6.                 ps.setString(2, User_ConfirmPassword);
  7.                 ps.executeUpdate();
  8.                 con.close();
the above code i am write for maintain session and check condition. now i want to know am i write a right query or not. for DAO file bcoz i am write this code in java class file. and error is occur for session can not resolved
Mar 7 '12 #1
Share this Question
Share on Google+
5 Replies


10K+
P: 13,264
Did you declare session anywhere?
Don't tie your Http objects with your DAO classes. Your DAO should be reusable and testable without knowledge of your client type.
Make your method take the Email and password as String parameters.
Also, always close connections in a finally block.
Mar 7 '12 #2

P: 52
hi r035198x first thanks for quick response.
and sir as u told last time i am now using the java class files for my jsp.
but how to use that declare session
tell me that little code.
Mar 7 '12 #3

10K+
P: 13,264
Call that DAO class from your controller (Servlet). In the servlet, you have access to the request, response and session objects.
Mar 7 '12 #4

P: 52
changepassAction.jsp

String theEmail = session.getAttribute("theEmail").toString();

where i am declare the session in string varible.
and my DAO class file is,

changepassclass.java

String theEmail; //declare that again for use.

public String gettheEmail() {
return theEmail;
}
public void settheEmail(String theEmail) {
this.theEmail = theEmail;
}
//this is getter and setter for above declaration.

public String implement(){
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/registration","root","root");
Statement st = con.createStatement();
ResultSet res = st.executeQuery("select * from regist where Password='"+User_Password+"'");
if(res.next()==true)
{
ps = con.prepareStatement("update regist set Password=?, Confirm_Password=? where Email_Id="theEmail,;");
ps.setString(1, User_NewPassword);
ps.setString(2, User_ConfirmPassword);
ps.executeUpdate();
con.close();
System.out.println("record-success.jsp");

//out.print("LOGIN Success");

}
else
{

// this is the condition where i want to check of that particulate user who's session is started..
plz tell me where i am wrong.
Mar 7 '12 #5

10K+
P: 13,264
Java is object oriented. Think objects and stop doing everything in one file. It's really bad code. Create classes
Expand|Select|Wrap|Line Numbers
  1. class User {
  2.     private String email;
  3.     private String password;
  4.     public User(String email, String password) {
  5.     super();
  6.     this.email = email;
  7.     this.password = password;
  8.     }
  9.     public String getEmail() {
  10.         return email;
  11.     }
  12.     public void setEmail(String email) {
  13.         this.email = email;
  14.     }
  15.     public String getPassword() {
  16.         return password;
  17.     }
  18.     public void setPassword(String password) {
  19.         this.password = password;
  20.     }
  21.  
  22.  
  23. }
  24.  
  25. class DAO {
  26.  
  27.     public User getUser(String email) {
  28.     // Select from DB and create a user object and return that
  29.     }
  30.     public boolean changePassword(String email, String password) {
  31.     //do your update statement here
  32.     }
  33. }
  34.  
Then your JSP doesn't contain process code anymore. It just asks for data from the DAO.
Mar 7 '12 #6

Post your reply

Sign in to post your reply or Sign up for a free account.