File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Iam unable to execute insert and select statements using batches

 
Raghavendra ReddyI
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Raghavendra ReddyI
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Raghavendra ReddyI wrote:
Raghavendra ReddyI wrote:
Raghavendra ReddyI wrote:import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class DeleteFlightData extends HttpServlet
{
Connection con=null;
Statement stmt=null;
int fid1,pid1;
String pname1;

public void init(ServletConfig config)throws ServletException
{
super.init(config);
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","system");
}
catch(Exception e)
{
}
}

public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
fid1=Integer.parseInt(req.getParameter("fid"));
try
{
stmt=con.createStatement();
con.setAutoCommit(false);
String query="select pid,pname form pilot where fid="+fid1;
ResultSet rs=stmt.executeQuery(query);
while(rs.next())
{
pid1=rs.getInt("pid");
pname1=rs.getString("pname");
}
String query1="delete from flight where fid="+fid1;
stmt.addBatch(query1);
String query2="insert into pilot (pid,pname,fid,onduty,free,leave) values ("+pid1+",'"+pname1+"',0,'-','free','-')";
stmt.addBatch(query2);
int[] count = stmt.executeBatch();
con.commit();
}
catch(SQLException sqle)
{
out.println("<div align='center' style='color:#FF0000;font-size:25px;'>");
out.println("

Cannot be deleted........!!!

");
out.println("</div>");
}
catch(Exception e)
{

out.println("<div align='center' style='color:#FF0000;font-size:25px;'>");
out.println("

Cannot be added........

");
out.println("</div>");
}
}//End of doPost

}//end of class
 
Paul Clapham
Sheriff
Pie
Posts: 20206
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem, I think, is that when an exception is thrown, your code throws away all the information in the exception and just displays something generic in the response. You should at least log the exception before doing that.

The reason I think this is the problem is that when you have "select ... form ..." instead of "select ... from ...", as your posted code does, then an exception will be thrown telling you that your SQL is not well-formed. But since you ignore the exception, you have no way of telling that.
 
Raghavendra ReddyI
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but still iam unable to execute
exception thrown is
java.sql.BatchUpdateException: error occurred during batching: ORA-02291: integrity constraint (SYSTEM.FK_PILOT) violated - parent key not found
 
Paul Clapham
Sheriff
Pie
Posts: 20206
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's good -- at least you have an error message. Now you need the full stack trace, not just the error message. It will tell you what line of code threw the exception, and that's where you should start looking. So you should write out the full stack trace when an exception is thrown, and you can do that by calling the exception's "printStackTrace()" method.
 
Balaji Vankadaru
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your first sql query select has an issue. Revise your sql is needed to get your code getting executed correctly.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic