i have created a delete.jsp page which checks for datatype entered in the fields and when user enters the student id will delete it from database.i am succesfull while following insert.jsp but i am geting proublem while updating and deleting. i have linked insertiion,updation and deletion jsp files to 1 home.html page which run in tomcat server. When i click delete or update it shows me fields to modify but when i click submit nothing happens. i have a feeling something about preparestatement not working. plz help

my database is as below

SQL> desc student_info;
Name Null? Type
----------------------------------------- -------- -------------------

SID NUMBER(9)
SNAME VARCHAR2(8)
SADD VARCHAR2(16)

and my delete.jsp page is as below

<html>
<script language='javascript'>
function isValid()
{
	if(f.stu_id.value=="")
	{
		alert(f.stu_id.name+" "+"field missing");
		f.stu_id.focus();
		return false;
	}
	if(isNaN(f.stu_id.value))
	{
		alert("stu_id should be numeric value");
		f.stu_id.focus();
		return false;
	}
	return true;
}
</script>
<body>
<form name=f action='./delete' method='post' onSubmit='return isValid()'>
<table border=1 cellpadding =7 cellspacing=7 align=center>
<caption><i><font size=4><u>Delete records</caption>
<tr>
<th>student id<th><input type="text" name="stu_id"></tr>
<tr>
<th>click<th><input type="submit" value="delete"></tr>
</table>
</form>
</body>
</html>

and my deleterecords.java file

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.sql.PreparedStatement;

public class deletedemo extends HttpServlet
{
	public void doPost(HttpServletRequest req,HttpServletResponse res) 
	{
		Connection con=null;
		PreparedStatement ps=null;
		int rs=0;
		try
		{
			PrintWriter pw=res.getWriter();
			res.setContentType("text/html");
			pw.println("<html><form target='display'>");
			int sid=Integer.parseInt(req.getParameter("stu_id"));
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con=DriverManager.getConnection("jdbc:odbc:thin:@localhost:8080:ravi","system","tiger");
			ps=con.prepareStatement("delete from student_info where stu_id=?");
			ps.setInt(1,sid);
			rs=ps.executeUpdate();
			if(rs!=1)
				pw.println("<h2>student id is proublem</h2>");
			else
				pw.println("<h2 style='position:absolute;left:50;top:50'> one record deleted </h2>");
			ps.close();
			con.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	public void doGet(HttpServletRequest req,HttpServletResponse res)
	{
		try
		{
			doPost(req,res);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
}

Your table schema is
SID NUMBER(9)
SNAME VARCHAR2(8)
SADD VARCHAR2(16)

but in the delete query is
delete from student_info where stu_id=?

Given the above schema, I think it should be as follows,
delete from student_info where sid=?

sti_id from jsp page is returned and it gets substituted in

ps=con.prepareStatement("delete from student_info where stu_id=?");
ps.setInt(1,sid);

it worked for insertion but not working for deletion and updation plz help me

Try to run the query first at the sql prompt and see if it works then takes it and put it at your statement. Post the errors that you get.

Just change url jdbc:odbc to jdbc:oracle your problem will get solved. also check if in database query is running and is commited

running the query at sql prompt works well. i am geting this error message at stdout.log message at tomcat 6/log folder as i redirected error to e.printStacktrace() .

java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
	at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknown Source)
	at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at java.sql.DriverManager.getConnection(Unknown Source)
	at deletedemo.doPost(deletedemo.java:21)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:574)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1527)
	at java.lang.Thread.run(Unknown Source)

and i have opened data source odbc from control panel and under user data have added microsoft odbc for oracle and gave data source name as ravi, username as system and server as localhost. plz help me . i am using oracle 10g XE edition. there is also a driver oracle in xe at create new data source . i even tried that but still geting error. i have even added drivers ie ojdbc14.jar to Tomcat 6.0\webapps\pro\WEB-INF\lib

plz provide me with a servlet program for the above jsp page that works, like dynamically taking values at jsp page and then performing deletion or updation to database.

From the exception stack trace it seems to be problem with the line

con=DriverManager.getConnection("jdbc:odbc:thin:@localhost:8080:ravi","system","tiger");

Please check if the servername i.e. localhost and the port i.e. 8080 are correct

8080 almost definately not, and you should, of course, be using "jdbc:oracle:thin", not "jdbc:odbc:thin", of course.

i have changed it to

con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:8081:ravi","system","tiger");

but i am getting error
java.sql.SQLException: Io exception: Got minus one from a read call

@masijade thank you for the help
@parry_kulk thank you for the help

And did you check the port? Oracle normally listens on 1521 (or is it 1541? something like that, anyway) look at your oracle configuration. You seem to be attempting to connect to a web server. That won't work, of course.

The default port for oracle is 1521. If you havn't changed it explicitly than try using the statement

con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ravi","system","tiger");
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.