10

Hello I am trying to delete a row from my database. I am getting no errors but it is doing nothing, any help or advice would be great!

public static void DeleteRow(String name) {
    try {  
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, username, password);

        PreparedStatement st = connection.prepareStatement("DELETE FROM Table WHERE name = " + name + ";");
        st.executeUpdate();
    } catch(Exception e) {
        System.out.println(e);
    }
}
1
  • What do you expect it to do? Commented Feb 4, 2014 at 4:13

6 Answers 6

22

I guess name is a varchar type in DB so do like this

PreparedStatement st = connection.prepareStatement("DELETE FROM Table WHERE name = '" + name + "';");

enclose name within single quotes '

Also this is not the way you are using is not the proper way of using Preparedstatement

Us the following way:

PreparedStatement st = connection.prepareStatement("DELETE FROM Table WHERE name = ?");
st.setString(1,name);
st.executeUpdate(); 

// your full code after Proper PreparedStatement

public static void DeleteRow(String name) {
    try {  
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, username, password);
        PreparedStatement st = connection.prepareStatement("DELETE FROM Table WHERE name = ?");
        st.setString(1,name);
        st.executeUpdate(); 
    } catch(Exception e) {
        System.out.println(e);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

@jeremyjjbrown no need to escape single quotes
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Table WHERE name = 'UserTest''
@user3268379 what is your table name? Table is a reserved keyword in mysql
@jeremyjjbrown yes thats why I mentioned that this is not the proper way of preparedstament and also I have suggested how to do
2

You should never create a SQL statement in Java with String concatenation, it will be vulnerable to sql injection. Please do it this way.

String selectSQL = "DELETE FROM Table WHERE name = ?";
connection.prepareStatement(selectSQL);
preparedStatement.setString(1, name);

Comments

1
          Class.forName("oracle.jdbc.driver.OracleDriver");
          Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "MANASH_APPN","MANASH");
          PreparedStatement ps = con.prepareStatement("delete from EMP21 where empid = ?");
           ps.setInt(1,90);
           ps.executeUpdate();
          con.commit();
          System.out.println("Records Delete Successfully....");
         con.close();

Comments

0

try this bro. use Statement

Statement stmt = connection.createStatement();
String SQL = "DELETE FROM Table WHERE name = '"+name+"'";
stmt.executeUpdate(SQL);

Comments

0

Every open connection must be closed, or it won't get implemented and no errors will be displayed. First learned lesson.

public static void DeleteRow(String name) {
    try {  
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, username, password);

        PreparedStatement st = connection.prepareStatement("DELETE FROM Table WHERE name = " + name + ";");
        st.executeUpdate(); 
        connection.close();
    } catch(Exception e) {
        System.out.println(e);
    }
}

Hope this helps

1 Comment

Your code is vulnerable to SQl injection. owasp.org/index.php/Preventing_SQL_Injection_in_Java
-1

this will work String del="DELETE FROM table WHERE name =('" + name + "')";

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.