【发布时间】:2015-05-18 10:52:23
【问题描述】:
我的问题是,我在 mysql 中创建了一个存储过程。我试图通过我的 java 代码调用这个过程。
我没有得到任何异常,但数据库不受影响。如果我做 通过preparedstatement的相同声明一切正常。 如果我在工作台中手动调用该过程,它也可以工作。
存储过程
CREATE DEFINER=`root`@`localhost`PROCEDURE `delete`(IN id INT(11))
BEGIN SET SQL_SAFE_UPDATES=0; Delete from sender where id = id;
END
调用代码
public void deleteSender(int id){
CallableStatement cStmt = null;
PreparedStatement pStmt = null;
PreparedStatement pStmt_2 = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/info", "root","");
String call = "{call delete(?)}";
cStmt = connection.prepareCall(call);
cStmt.setInt(1, id);
boolean b = cStmt.execute();
} catch (Exception e) {
}
}
As prepared Statement:
public void deleteSender(int id){
CallableStatement cStmt = null;
PreparedStatement pStmt = null;
PreparedStatement pStmt_2 = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/info", "root","");
pStmt = connection.prepareStatement("Delete from sender where id="+id);
pStmt_2 = connection.prepareStatement("Delete from table2 where sender_id="+id);
boolean b = pStmt.execute();
boolean b_2 = pStmt_2.execute();
if(b == false || b_2 == false){
JOptionPane.showMessageDialog(null, "Data deleted");
}
} catch (Exception e) {
}
有什么想法吗??
【问题讨论】:
-
1.您在第一部分中没有遇到异常因为您似乎没有处理它们(第二部分也是如此)。 2.您的第二部分不是准备好的语句,您仍在将
id值连接到字符串。 -
感谢您的回答,但即使我用 syso 或其他东西捕捉到异常,它甚至无法到达那里,但在调试模式下,一切都被正确调用了。如果我将值设置为真正的准备好的语句,则第二个代码的工作方式相同,因此无论哪种方式它都可以工作......只是可调用对象没有
标签: java mysql stored-procedures