Java sql 删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21542825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:16:35  来源:igfitidea点击:

Java sql delete row

javasqldatabasesql-delete

提问by user3268379

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

采纳答案by SpringLearner

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

我猜 name 是 DB 中的 varchar 类型,所以这样做

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

此外,这不是您使用的方式,也不是使用 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);
    }
}

回答by Sudharsun

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

希望这可以帮助

回答by jeremyjjbrown

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

永远不要用字符串连接在 Java 中创建 SQL 语句,它容易受到 sql 注入的攻击。请这样做。

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

回答by wrecklez

try this bro. use Statement

试试这个兄弟。使用声明

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

回答by Asif alam

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

这将起作用 String del="DELETE FROM table WHERE name =('" + name + "')";

:)

:)

回答by Manash Ranjan Dakua

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