I have a datebase lyrics with a table lyrics1, and with the code below I want to insert a row into lyrics1.
我有一个带有table lyrics1的datebase歌词,下面的代码我想在lyrics1中插入一行。
But when I go back to the mysql client and do describe lyrics1
, it hasn't updated.
但是当我回到mysql客户端并描述lyrics1时,它还没有更新。
I get no error, it connects to the database fine. At least I don't get error saying it was unable to.
我没有错误,它连接到数据库很好。至少我没有出错说它不能。
connectToDB();
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String query = "INSERT INTO lyrics1(name, artist) values(?, ?)";
PreparedStatement statement = connection.prepareStatement(query);
statement.setString(1, nameOfSong.getText()); // set input parameter 2
statement.setString(2, artist.getText());
ResultSet rs = statement.executeQuery("SELECT * FROM lyrics1");
while (rs.next()){
System.out.println(rs.getString(1));
}
rs.close();
statement.close();
connection.close();
} catch (SQLException insertException) {
displaySQLError(insertException);
}
internalFrame.dispose();
}
});
1 个解决方案
#1
4
you are never actual executing the INSERT
statement, you just create it and never actual execute it. You are executing a SELECT
but nothing actually sends the INSERT
statement to the server.
您永远不会实际执行INSERT语句,您只是创建它,而不会实际执行它。您正在执行一个SELECT,但实际上并没有向服务器发送INSERT语句。
You need to first call statement.executeUpdate()
it will return the number of rows affected, most of the time you want to check that this is equal to 1 for INSERT and more than ZERO for UPDATE and DELETE.
您需要首先调用statement.executeUpdate(),它将返回受影响的行数,大多数情况下,您希望检查这个值是否等于1,而对于UPDATE和DELETE,则要检查这个值是否大于零。
#1
4
you are never actual executing the INSERT
statement, you just create it and never actual execute it. You are executing a SELECT
but nothing actually sends the INSERT
statement to the server.
您永远不会实际执行INSERT语句,您只是创建它,而不会实际执行它。您正在执行一个SELECT,但实际上并没有向服务器发送INSERT语句。
You need to first call statement.executeUpdate()
it will return the number of rows affected, most of the time you want to check that this is equal to 1 for INSERT and more than ZERO for UPDATE and DELETE.
您需要首先调用statement.executeUpdate(),它将返回受影响的行数,大多数情况下,您希望检查这个值是否等于1,而对于UPDATE和DELETE,则要检查这个值是否大于零。