vishalanuj 0 Newbie Poster

Hi,I am new to struts.i want to make a database connection with the help of a method call in some other class.I am not able to make the method for the database connectivity.Please if someone could help me.

vishalanuj 0 Newbie Poster

For other's refernce.This is what i did.

function validpincode()
{
	var len = document.forms[0].pcList.length;
	for(i=0;i<len;i++)
	{
		var s=document.forms[0].pcList[i].value;
		var regInt = /^[0-9]+$/;
		if(!regInt.test(s))
		{
			alert("please enter numeric value");
			document.forms[0].pcList[i].focus();
			return false;
		}
	}
vishalanuj 0 Newbie Poster

Ya i just wanted to validate only numeric characters.

but I did it.Thanks for the reply neways.

vishalanuj 0 Newbie Poster

Hi i want the numeric validation of all the pcList values coming from Database in this update jsp which upon clicking save will get saved in the database.I am able to save the values in the database but not able to write(or perform) the required numeric validation javascript.

Please any help would be great.

Thanks Vishal

vishalanuj 0 Newbie Poster
<%@ page language="java" pageEncoding="ISO-8859-1"%>
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@taglib uri="/WEB-INF/struts-html-el.tld" prefix="html_el" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

   <html>
   <head>
       <title>Listing all cities</title>
	   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   </head>

   <body>
     <center><h3>city List</h3></center>
	 </body>
	  <html:form action="/UpdateSuccessAction.do" >
		 <c:forEach var="obj" items="${myBean.result}">
		 City: <c:out value='${obj.cn}' /><br>
		 Pincode: <html_el:text property="pcList" value="${obj.pc}"/><br><br>
		</c:forEach>
		<input type="submit" name="submit" value="save111" onClick="return validpincode(); onSave()">
		<p><a href="HomePage.do">HomePage</a></p>
	  </html:form>
	  </html>
	  <script>
function onSave()
{
	document.forms[0].action="UpdateSuccessAction.do";
	document.forms[0].submit();
}
vishalanuj 0 Newbie Poster

This javascript is giving an error

function checkpincode(elem,helperMsg)
{
	var numericExpression=[0-9];
	if(elem.value.match(numericExpression))
	{
		return true;
	}
	else
	{
		alert(helperMsg);
		elem.focus();
		return false;
	}
	
}

The page is giving an error "undefined is null or not an object".Perhaps i need to provide an action path for the checkpincode() javascript.But i don't know where.Or there might be some other reason.

vishalanuj 0 Newbie Poster

I want to add a javascript in the jsp form for the form validation of numbers in the pincode text area.I know it's easy but just not clicking :(.Any help would be appreciated.

vishalanuj 0 Newbie Poster
<%-- 
    Document   : AddCity
    Created on : Oct 6, 2009, 9:14:52 PM
    Author     : Vishal
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body onload="ShowMessages();">
	<html:form action="AddPage.do">
	
	<p><a href="HomePage.do">HomePage</a></p>
		Cityname:
		<input type="text" name="cn" value="">
		PinCode:
		<input type="text" name="pc" value=""><br>
		<input type="button" name="Submit" value="Submit1" onClick="checkpincode();onSubmit()">
		<div id="message" style="position:relative;left:0;top:0;display:none"><html:errors/></div>
	</html:form>
	</body>
</html>

<script>
function onSubmit()
{
	document.forms[0].action="AddPageSuccess.do";
	document.forms[0].submit();
}
function ShowMessages()
{
	var msg= document.getElementById("message").innerHTML;
	if(msg!=null && msg.length >0)
	{
		msg=msg.replace(/<BR>/g,"\n ");
		msg=msg.replace(/<br>/g,"\n ");
		alert(msg);
		document.getElementById("message").innerHTML="";
	}
}
</script>
vishalanuj 0 Newbie Poster
String pic = rs.getString("pincode");

not

String pic = rs.getString(pincode);

Also, use a preparedStatement, cobbling a statement together using String concatenation like you've done is just begging for trouble. Maybe only as innocent as a mistyped user input string that causes the SQL to fail with a syxntax error, or as damaging as an intentional SQL Injection attack.

Edit: And this

if(pic=="pincode")

is probably backwards, too.

Hi.i just rectified the error.i should have used if(pic.equals(pincode)) instead of if(pic=="pincode"). :P
i am a newbie.
thanks for replying neways. :)

vishalanuj 0 Newbie Poster
package Add;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class AddSuccessAction extends Action 
{
	private final static String SUCCESS = "success";

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
{
		MyBean bean = (MyBean) form;
		String cityName = bean.getCn();
		String pincode = bean.getPc();
		
		Connection con = null;
		ResultSet rs = null;
        PreparedStatement stmt = null;
		
		String connectionURL = "jdbc:mysql://localhost:3306/city"; 
		
		try 
		{ 
		    Class.forName("com.mysql.jdbc.Driver"); 
		    con = DriverManager.getConnection (connectionURL,"root","welcome12#"); 
		    String sq = "SELECT city_name,pincode from data ";
			stmt = con.prepareStatement(sq);
			rs = stmt.executeQuery();
			boolean flag = false;
			while (rs.next()) 
			{
				String pic = rs.getString(pincode);
				
				if(pic=="pincode")
				{
					System.out.println("Pincode already exists");
					flag = true;
				}
			}
			if(!flag)
			{
				String sql = "INSERT INTO city (city name,pincode) VALUES('"+cityName+','+pincode+"')";
				stmt = con.prepareStatement(sql);
				stmt.executeUpdate();
			}
		}
		catch (SQLException e) 
		{ 
		    e.printStackTrace(); 
		} 
		catch (Exception e) 
		{ 
		    e.printStackTrace(); 
		} 
		finally 
		{ 
			stmt.close();
			con.close(); 
		} 
		return mapping.findForward(SUCCESS);
	}

}