English 中文(简体)
如何知道在java是否执行过l语?
原标题:How to know if an sql statement executed in java?
  • 时间:2011-03-16 13:38:36
  •  标签:
  • java
  • mysql

我想知道,删除这一声明是否实际上删除了某些内容。 下面的法典总是执行。 是否删除了部分内容。 这样做的恰当方式是什么?

public Deleter(String pname, String pword) {

        try {
            PreparedStatement createPlayer = conn.prepareStatement("DELETE FROM players WHERE P_Name= "+ pname +"  AND P_Word= " + pword + " ");
            createPlayer.execute();

            if(createPlayer.execute()==true){

            JOptionPane.showMessageDialog(null, "Player successfully deleted!");

            }else{

                 JOptionPane.showMessageDialog(null, "Player does not exist!", "notdeleted", JOptionPane.ERROR_MESSAGE);
            }

        } catch (Exception e) {
        }
    }
最佳回答

你实际上两次执行删除声明,因为你两次打电话.execute()。 在大多数情况下,如果你几乎立即第二次掌握,你就不太可能有可以通过说明删除的数据。

相反,使用executeUpdate(meth。 退还给您的行文数目

int rowsAffected = createPlayer.executeUpdate();

if(rowsAffected > 0) {
   JOptionPane.showMessageDialog(null, "Player successfully deleted!");
}
else{
    JOptionPane.showMessageDialog(null, "Player does not exist!", "notdeleted", JOptionPane.ERROR_MESSAGE);
}
问题回答

Use executeUpdate. It returns an int parameter telling you the number of rows affected. Also, be careful, in your code you are executing the statement twice.

您应取代Player.execute(Player.execute);对于一名创造者来说,当他们返回时,就意味着受到影响的浏览次数。 你们需要做的是,如果是零,就不必这样做。

使用最新()来计算增长额。

还有一些重大项目需要更正在您的法典中:

  • You are not using PreparedStatement correctly. Use parameters instead.
  • Don t swallow exceptions. At the least print a stack trace but showing a message to the user would be nice.
  • You are running the statement twice. Just do it once in your if statement.




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签