Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to delete row in database?, pls help in my coding

 
ramesh kumar
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
I am doing a projects which has to delete a row in a data base
, and the it is a servlet coding.
when executing the code in a browser, i am getting a message viz.
"oops, error[Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause"
kindly go through my coding and let me know the bug plzz

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());}

}
}


------------------
 
paul sun
Greenhorn
Posts: 28
  • 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);
May be changed to:

String stri = "delete from members WHERE USERNAMECOLUMN = ?";
PreparedStatement stmt = con.prepareStatement(stri);
stmt.setString(1,username); //suppose 1 -> USERNAMECOLUMN of the database

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic