This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JDBC and the fly likes how to delete row in database?, pls help in my coding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to delete row in database?, pls help in my coding" Watch "how to delete row in database?, pls help in my coding" New topic
Author

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

ramesh kumar
Ranch Hand

Joined: Nov 16, 2000
Posts: 32
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

Joined: Dec 22, 2000
Posts: 28
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

 
GeeCON Prague 2014
 
subject: how to delete row in database?, pls help in my coding