how to access value from mysql database to jsp form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #16
    How did you put the main method and what errors do you get?
    You need to learn how to create and test Java programs before starting to write JSPs.

    Comment

    • vishal prada
      New Member
      • Mar 2012
      • 52

      #17
      Code:
      package com.proname;
      
      import java.sql.Connection;
      import java.sql.DriverManager;
      //import java.sql.ResultSet;
      import java.sql.Statement;
      import java.sql.PreparedStatement;
      
      public class editproclass {
      	
      	String vFirstName;
      	String vLastName;
      	String vMobileNumber;
      	String vAddress;
      	String vCountryId;
      	String vCityName;
      	String vPinCode;
      	String theEmail;
      	Statement stmt = null;
      	PreparedStatement ps = null;
      	
      	
      	public String getvFirstName() {
      		return vFirstName;
      	}
      	public void setvFirstName(String vFirstName) {
      		this.vFirstName = vFirstName;
      	}
      	public String getvLastName() {
      		return vLastName;
      	}
      	public void setvLastName(String vLastName) {
      		this.vLastName = vLastName;
      	}
      	public String getvMobileNumber() {
      		return vMobileNumber;
      	}
      	public void setvMobileNumber(String vMobileNumber) {
      		this.vMobileNumber = vMobileNumber;
      	}
      	public String getvAddress() {
      		return vAddress;
      	}
      	public void setvAddress(String vAddress) {
      		this.vAddress = vAddress;
      	}
      	public String getvCountryId() {
      		return vCountryId;
      	}
      	public void setvCountryId(String vCountryId) {
      		this.vCountryId = vCountryId;
      	}
      	public String getvCityName() {
      		return vCityName;
      	}
      	public void setvCityName(String vCityName) {
      		this.vCityName = vCityName;
      	}
      	public String getvPinCode() {
      		return vPinCode;
      	}
      	public void setvPinCode(String vPinCode) {
      		this.vPinCode = vPinCode;
      	}
      	public String getTheEmail() {
      		return theEmail;
      	}
      	public void setTheEmail(String theEmail) {
      		this.theEmail = theEmail;
      	}
      	public Statement getStmt() {
      		return stmt;
      	}
      	public void setStmt(Statement stmt) {
      		this.stmt = stmt;
      	}
      	public PreparedStatement getPs() {
      		return ps;
      	}
      	public void setPs(PreparedStatement ps) {
      		this.ps = ps;
      	}
      
      	public class editproclass(){
      		
      	}
      
      	public  main(String[] args) {
      		 
      		
      		try
      		{
      			Class.forName("com.mysql.jdbc.Driver");
      			Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/registration","root","root");
      			//String query ="update regist set First_Name='"+vFirstName+"'", Last_Name='"+vLastName+"'", Mobile_Number='"+vMobileNumber+"'", Address='"+vAddress+"'", City_Name='"+vCityName+"'", Country_Id='"+vCountryId+"'", Pin_Code='"+vPinCode+"'" where Email_Id='"+theEmail+"'";
      			PreparedStatement ps ;
      			String vFirstName ;
      			String vLastName ;
      			String vMobileNumber ;
      			String vAddress ;
      			String vCountryId ;
      			String vCityName ;
      			String vPinCode ;
      			String theEmail ;
      			ps = con.prepareStatement("update regist set First_Name=?, Last_Name=?, Mobile_Number=?, Address=?, City_Name=?, Country_Id=?, Pin_Code=? where Email_Id='"+theEmail+"'");
      			
      			 ps.setString(1, vFirstName);
      			 ps.setString(2, vLastName);
      			ps.setString(3,vMobileNumber);
      			ps.setString(4,vAddress);
      			ps.setString(5,vCityName);
      			ps.setString(6,vCountryId);
      			ps.setString(7,vPinCode);
      			  			  
      			 ps.executeUpdate();
      			  con.close();
      		}
      	catch(Exception e)
      	{
      		e.printStackTrace();
      	
      	}
      
      }
      
      }
      
      //like that.
      Last edited by Rabbit; Mar 9 '12, 04:48 PM. Reason: Please use code tags when posting code.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #18
        Initialize the values of String vFirstName ;
        String vLastName ;
        String vMobileNumber ;
        String vAddress ;
        String vCountryId ;
        String vCityName ;
        String vPinCode ;
        String theEmail ;


        to some test values. You can't use variables that you have not initialized in Java.

        Comment

        • vishal prada
          New Member
          • Mar 2012
          • 52

          #19
          Code:
          //editproAction.jsp
          
          <%@page import="java.sql.*"%>
          <%@page import="com.proname.editproclass" %>
          <%
           
           // PreparedStatement ps = null;
            //ResultSet rs = null;
           //Statement st = null;
           
            String vFirstName=request.getParameter("firstName");
            String vLastName=request.getParameter("lastName");
            String vMobileNumber=request.getParameter("mobileNumber");
            String vAddress=request.getParameter("address");
            String vCountryId=request.getParameter("countryId");
            String vCityName=request.getParameter("cityName");
            String vPinCode=request.getParameter("pinCode");
            
            String theEmail = session.getAttribute("theEmail").toString();
            
            editproclass e=new editproclass();
            e.setvFirstName(vFirstName);
            e.setvLastName(vLastName);
            e.setvMobileNumber(vMobileNumber);
            e.setvAddress(vAddress);
            e.setvCountryId(vCountryId);
            e.setvCityName(vCityName);
            e.setvPinCode(vPinCode);
            
            
           // System.out.print("hello");
          %>
          
          <jsp:forward page="record-success.jsp"></jsp:forward>
          
          
          //editproclass.java
          
          package com.proname;
          
          import java.sql.Connection;
          import java.sql.DriverManager;
          //import java.sql.ResultSet;
          import java.sql.Statement;
          import java.sql.PreparedStatement;
          
          public class editproclass {
          	
          	String vFirstName;
          	String vLastName;
          	String vMobileNumber;
          	String vAddress;
          	String vCountryId;
          	String vCityName;
          	String vPinCode;
          	String theEmail;
          	Statement stmt = null;
          	PreparedStatement ps = null;
          	
          	
          	public String getvFirstName() {
          		return vFirstName;
          	}
          	public void setvFirstName(String vFirstName) {
          		this.vFirstName = vFirstName;
          	}
          	public String getvLastName() {
          		return vLastName;
          	}
          	public void setvLastName(String vLastName) {
          		this.vLastName = vLastName;
          	}
          	public String getvMobileNumber() {
          		return vMobileNumber;
          	}
          	public void setvMobileNumber(String vMobileNumber) {
          		this.vMobileNumber = vMobileNumber;
          	}
          	public String getvAddress() {
          		return vAddress;
          	}
          	public void setvAddress(String vAddress) {
          		this.vAddress = vAddress;
          	}
          	public String getvCountryId() {
          		return vCountryId;
          	}
          	public void setvCountryId(String vCountryId) {
          		this.vCountryId = vCountryId;
          	}
          	public String getvCityName() {
          		return vCityName;
          	}
          	public void setvCityName(String vCityName) {
          		this.vCityName = vCityName;
          	}
          	public String getvPinCode() {
          		return vPinCode;
          	}
          	public void setvPinCode(String vPinCode) {
          		this.vPinCode = vPinCode;
          	}
          	public String getTheEmail() {
          		return theEmail;
          	}
          	public void setTheEmail(String theEmail) {
          		this.theEmail = theEmail;
          	}
          	public Statement getStmt() {
          		return stmt;
          	}
          	public void setStmt(Statement stmt) {
          		this.stmt = stmt;
          	}
          	public PreparedStatement getPs() {
          		return ps;
          	}
          	public void setPs(PreparedStatement ps) {
          		this.ps = ps;
          	}
          
          	//public class editproclass(){
          		
          	//}
          
          	public  void main(String[] args) {
          		 
          		
          		try
          		{
          			Class.forName("com.mysql.jdbc.Driver");
          			Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/registration","root","root");
          			//String query ="update regist set First_Name='"+vFirstName+"'", Last_Name='"+vLastName+"'", Mobile_Number='"+vMobileNumber+"'", Address='"+vAddress+"'", City_Name='"+vCityName+"'", Country_Id='"+vCountryId+"'", Pin_Code='"+vPinCode+"'" where Email_Id='"+theEmail+"'";
          			PreparedStatement ps ;
          			String vFirstName = null ;
          			String vLastName = null ;
          			String vMobileNumber = null ;
          			String vAddress = null ;
          			String vCountryId = null ;
          			String vCityName = null ;
          			String vPinCode = null ;
          			String theEmail = null ;
          			ps = con.prepareStatement("update regist set First_Name=?, Last_Name=?, Mobile_Number=?, Address=?, City_Name=?, Country_Id=?, Pin_Code=? where Email_Id='"+theEmail+"'");
          			
          			ps.setString(1, vFirstName);
          			ps.setString(2, vLastName);
          			ps.setString(3,vMobileNumber);
          			ps.setString(4,vAddress);
          			ps.setString(5,vCityName);
          			ps.setString(6,vCountryId);
          			ps.setString(7,vPinCode);
          			  			  
          			 ps.executeUpdate();
          			  con.close();
          		}
          	catch(Exception e)
          	{
          		e.printStackTrace();
          	
          	}
          
          }
          
          }
          // am i right now but it till not working .
          its nowt updating my record in database.
          Last edited by Rabbit; Mar 9 '12, 04:48 PM. Reason: Please use code tags when posting code.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #20
            You have set all values to null including the email that is used for the where clause. You should identify a record in your DB that you want to use use for the test and use it's email. Also set the other values to meaningful test values.

            Comment

            • vishal prada
              New Member
              • Mar 2012
              • 52

              #21
              but if i put there nothing then that give error to me
              what is the right value for that plz tell me.

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #22
                You are trying to update a record in the database, right?
                Put the email of the record in the database. If you have nothing in the database then you have nothing to update.

                Comment

                • vishal prada
                  New Member
                  • Mar 2012
                  • 52

                  #23
                  actually there is one edit profile form ok,
                  and i maintain the session form user login,
                  as soon as user get login session is start also,
                  and then if user want to edit there profile then he/she
                  opens the link of edit_profile.js p page
                  and he/she look the values which he already submited in the registration form.
                  then that means table have some values but till my code is not update that existing values.

                  Comment

                  • vishal prada
                    New Member
                    • Mar 2012
                    • 52

                    #24
                    one problem is
                    Code:
                    <!-- code for testing  -->
                    		<%
                    	String First_Name1="",Last_Name1="", Mobile_Number1="", Address1="", City_Name1="", Country_Id1="", Pin_Code1="";
                    					
                    	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();
                    		String sql="select First_Name, Last_Name, Mobile_Number, Address, City_Name, Country_Id, Pin_Code from regist where Email_Id='"+session.getAttribute( "theEmail" )+"'";
                    		ResultSet rs=st.executeQuery(sql);
                    		System.out.println(sql);
                    		while(rs.next())
                    		{
                    			First_Name1 = rs.getString("First_Name");
                    			Last_Name1 = rs.getString("Last_Name");
                    			Mobile_Number1 = rs.getString("Mobile_Number");
                    			Address1 = rs.getString("Address");
                    			City_Name1 = rs.getString("City_Name");
                    			Country_Id1 = rs.getString("Country_Id");
                    			Pin_Code1 = rs.getString("Pin_Code");
                    		
                    		}
                    		}
                    	catch(Exception e)
                    	{
                    		out.println(e);
                    	}
                    	
                    %>
                    	<!-- end of code testing -->
                    <table width="99%" align="center">
                    <tbody><tr>
                    	<td width="80%" valign="top">
                    		<!--table width="99%" bgColor="#FF0000" cellspacing="1" cellpadding="5"><tr><td align="center" bgColor="#FCB1A7"><b><br>Warning - we noticed that you are not participating in Special Promotions. Please register on the advertiser pages with genuine data, else the frequency of Special Promotions will be reducted in your account.<br><br></b></td></tr></table-->
                    <table cellpadding="0" cellspacing="0" border="0" width="500" align="center" style="font-family:verdana;font-size:12px" background="images/11.jpg">
                    <tbody><tr>
                    	<td width="4">&nbsp;</td>
                    	<td width="71"><font color="#66FFFF" size="2">Edit Profile</font></td>
                    	<td width="8">&nbsp;</td>
                    	<td background="images/table_bg.gif" width="406">&nbsp;</td>
                    	<td width="10" background="images/table_bg.gif"><img src="pixel(1).gif" width="10" height="1" border="0"></td>
                    </tr>
                    <tr>
                    	<td background="line_l.gif"><img src="images/pixel(1).gif" border="0"></td>
                    	<td colspan="3">
                    	<img src="pixel(1).gif" width="1" height="10" border="0"><br>
                    	<table cellpadding="0" cellspacing="0" border="0" width="102%">
                    	  <tbody><tr><td bgcolor="#DBEAF5">
                    	  <!-- This is the profile edit form -->
                    		<form action="editproAction.jsp" method="post">  
                    		<table cellspacing="1" cellpadding="2" border="0" width="100%" style="font-family:verdana;font-size:12px">
                    		<tbody><tr bgcolor="#99FFFF">
                    			<td id="t_email">&nbsp;Email:<span class="inputHighlighted">*</span></td>
                    			<td><b><input type="text" name="emailId" size="30" value="<%= session.getAttribute( "theEmail" ) %>" disabled="disabled"></input></b></td>
                    		</tr><tr bgcolor="#99FFFF">
                    			<td id="t_uname" width="30%">&nbsp;First Name:<span class="inputHighlighted">*</span></td>
                    			<td><input type="text" name="firstName" id="firstName" size="35" class="ctrl" value=<%=First_Name1%>></td>
                    		</tr>
                    		<tr bgcolor="#99FFFF">
                    			<td id="t_uname" width="30%">&nbsp;Last Name:<span class="inputHighlighted">*</span></td>
                    			<td><input type="text" name="lastName" id="lastName" size="35" class="ctrl" value="<%=Last_Name1%>"></td>
                    		</tr>
                    		<tr bgcolor="#99FFFF"> 
                    			<td id="t_password">&nbsp;Mobile Number: <span class="inputHighlighted">*</span></td>
                    			<td><input type="text" name="mobileNumber" id="mobileNumber" size="35" class="ctrl" value="<%=Mobile_Number1%>"></td>
                    		</tr>
                    		<tr bgcolor="#99FFFF"> 
                    			<td id="t_password">&nbsp;Address: <span class="inputHighlighted">*</span></td>
                    			<td><textarea name="address" id="address" rows="4" cols="35" ><%=Address1%></textarea><small><br><br><b>Note:</b> We will send your payment cheques to this address.</small></td>
                    		</tr>
                    		<tr bgcolor="#99FFFF"> 
                    			<td id="t_password_copy" nowrap="">&nbsp;Country :<span class="inputHighlighted">*</span>&nbsp;</td>
                    			<td><input type="text" name="countryId" id="countryId" size="35" class="ctrl" value="<%=Country_Id1%>">
                    				<%--	 //  <option value=<%=Country_Id1%>><%=Country_Id1%></option>
                    					 // <option>"+rs.getString("Country_Id")+"</option>
                    				 --%>
                    			</td>
                    		</tr>
                    		<tr bgcolor="#99FFFF"> 
                    			<td id="t_password_copy" nowrap="">&nbsp;City :<span class="inputHighlighted">*</span>&nbsp;</td>
                    			<td><input type="text" name="cityName" id="cityName" size="35" class="ctrl" value="<%=City_Name1%>">
                    			</td>
                    		</tr>
                    		<tr bgcolor="#99FFFF"> 
                    			<td id="t_password">&nbsp;PIN Code (Zip): <span class="inputHighlighted">*</span></td>
                    			<td><input type="text" name="pinCode" id="pinCode" size="35" class="ctrl" value="<%=Pin_Code1%>"></td>
                    this is my old edit_profile form page and in this the above jsp code use to retrieve values from database and show's on the page form.
                    but in action tage i provide editproAction.j sp which is...
                    Code:
                    <%@page import="java.sql.*"%>
                    <%@page import="com.proname.editproclass" %>
                    <%
                     
                     // PreparedStatement ps = null;
                      //ResultSet rs = null;
                     //Statement st = null;
                     
                      String vFirstName=request.getParameter("firstName");
                      String vLastName=request.getParameter("lastName");
                      String vMobileNumber=request.getParameter("mobileNumber");
                      String vAddress=request.getParameter("address");
                      String vCountryId=request.getParameter("countryId");
                      String vCityName=request.getParameter("cityName");
                      String vPinCode=request.getParameter("pinCode");
                      
                      String theEmail = session.getAttribute("theEmail").toString();
                      
                      editproclass e=new editproclass();
                      e.setvFirstName(vFirstName);
                      e.setvLastName(vLastName);
                      e.setvMobileNumber(vMobileNumber);
                      e.setvAddress(vAddress);
                      e.setvCountryId(vCountryId);
                      e.setvCityName(vCityName);
                      e.setvPinCode(vPinCode);
                      e.setTheEmail(theEmail);
                      String V=e.implement();
                      if(V.equals("success"))
                      {
                    	  %>
                    		<jsp:forward page="record-success.jsp"></jsp:forward>	
                    		<% 
                    		}
                    		else
                    		{
                    			%>
                    			
                    			<jsp:forward page="edit_profile.jsp"></jsp:forward>
                    		<%
                    		
                    		}
                    		
                    	%>
                    and my editproclass.ja va page is ...
                    Code:
                    package com.proname;
                    
                    import java.sql.Connection;
                    import java.sql.DriverManager;
                    import java.sql.PreparedStatement;
                    import java.sql.ResultSet;
                    import java.sql.Statement;
                    
                    public class editproclass {
                    	
                    	String vFirstName;
                    	String vLastName;
                    	String vMobileNumber;
                    	String vAddress;
                    	String vCountryId;
                    	String vCityName;
                    	String vPinCode;
                    	String theEmail;
                    	String var="";
                    	
                    	PreparedStatement ps = null;
                    	
                    	
                    	public String getvFirstName() {
                    		return vFirstName;
                    	}
                    	public void setvFirstName(String vFirstName) {
                    		this.vFirstName = vFirstName;
                    	}
                    	public String getvLastName() {
                    		return vLastName;
                    	}
                    	public void setvLastName(String vLastName) {
                    		this.vLastName = vLastName;
                    	}
                    	public String getvMobileNumber() {
                    		return vMobileNumber;
                    	}
                    	public void setvMobileNumber(String vMobileNumber) {
                    		this.vMobileNumber = vMobileNumber;
                    	}
                    	public String getvAddress() {
                    		return vAddress;
                    	}
                    	public void setvAddress(String vAddress) {
                    		this.vAddress = vAddress;
                    	}
                    	public String getvCountryId() {
                    		return vCountryId;
                    	}
                    	public void setvCountryId(String vCountryId) {
                    		this.vCountryId = vCountryId;
                    	}
                    	public String getvCityName() {
                    		return vCityName;
                    	}
                    	public void setvCityName(String vCityName) {
                    		this.vCityName = vCityName;
                    	}
                    	public String getvPinCode() {
                    		return vPinCode;
                    	}
                    	public void setvPinCode(String vPinCode) {
                    		this.vPinCode = vPinCode;
                    	}
                    	public String getTheEmail() {
                    		return theEmail;
                    	}
                    	public void setTheEmail(String theEmail) {
                    		this.theEmail = theEmail;
                    	}
                    	public String getVar() {
                    		return var;
                    	}
                    	public void setVar(String var) {
                    		this.var = var;
                    	}
                    	public PreparedStatement getPs() {
                    		return ps;
                    	}
                    	public void setPs(PreparedStatement ps) {
                    		this.ps = ps;
                    	}
                    		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 Email_Id='"+theEmail+"'");
                    			if(res.next()==true)
                    				{
                    				ps = con.prepareStatement("update regist set First_Name=?, Last_Name=?, Mobile_Number=?, Address=?, City_Name=?, Country_Id=?, Pin_Code=? where Email_Id='"+theEmail+"'");
                    				ps.setString(1, vFirstName);
                    				ps.setString(2, vLastName);
                    				ps.setString(3, vMobileNumber);
                    				ps.setString(4, vAddress);
                    				ps.setString(5, vCountryId);
                    				ps.setString(6, vCityName);
                    				ps.setString(7, vPinCode);
                    				
                    				ps.executeUpdate();
                    				con.close();
                    				
                    				var="success";
                    											
                    				}
                    			else
                    				{
                    					var="fail";
                    				}
                    			}
                    			catch(Exception e)
                    			{
                    				System.out.println(e);
                    			}	
                    			return var;
                    		}
                    
                    }
                    now i want to remove the code which show the jsp code within html how's that becoz i already make one class file to store connection stream.
                    how to remove that code but i must work page same without that code.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #25
                      Not understanding the question. I suggest you write the code where you post to a servlet and not to JSPs and then when you have a problem, post a new thread explaining the details of the problem there.

                      Comment

                      Working...