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);
}
}
vishalanuj 0 Newbie Poster
masijade 1,351 Industrious Poster Team Colleague Featured Poster
peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster
vishalanuj 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.