Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet program , sql error in coding, helpppp

 
ramesh kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI pals,
my coding gives me error like below in a browser.
I know that it is a sql error. but donno how to solve it, plss help.
the error message is as below.
oops, error[Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause
the coding is as below.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class Delete extends HttpServlet {
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {

String username = req.getParameter("name");
String address = req.getParameter("address");
PrintWriter pw=res.getWriter();
pw.println("Name "+username);
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("jdbcdbc:ramesh","","");
if(con ==null){
pw.println("tere is error in connection");}
else{pw.println("wawww there is connection");}

Statement statement=con.createStatement();
ResultSet resultSet=statement.executeQuery("select * from members");
boolean temp=false;
String userID="";

while (resultSet.next())
{
userID=resultSet.getString(1);
temp=userID.equals(username);
if (temp)
{
pw.println("hi user");temp=true;break;}

}
if(temp){pw.println("u r a registered user");

while (resultSet.next())
{
userID=resultSet.getString(1);
temp=userID.equals(username);
if(temp){pw.println("hi user");temp=true;break;}
}
if(temp){pw.println("u r a registered user");
String stri = "delete from members (name, address)" + "values(?,?)";
PreparedStatement stmt = con.prepareStatement(stri);

stmt.setString(1,username);
stmt.setString(2,address);

int row = stmt.executeUpdate();
pw.println("your record has been deleted in the database");

statement.close();}

}

}
catch(ClassNotFoundException e) {pw.println("there is error");}
catch(SQLException e){pw.println("oops, error" +e.getMessage());}

}
}


------------------
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String stri = "delete from members (name, address)" + "values(?,?)";
PreparedStatement stmt = con.prepareStatement(stri);
stmt.setString(1,username);
stmt.setString(2,address);
int row = stmt.executeUpdate();

I would watch the abv stmts. more precisely, the stri
definition.
Regds.
- satya
 
madhu babu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai,
the mistake is u want to delete a row from database.
u r query should be like this
stri="delete from members where name=? and address=?"
PreparedStatement pst=Connection.prepareStatement(stri);
pst.setString(1,username);
pst.setString(2,password);
int i=pst.executeUpdate();

now u r application works perfectly.
if any problem
madhu_r77@webveda.com.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic