代码显示.executeUpdate语句中的错误。我试图通过Java执行sql语句。 错误显示在
行中rs = stmt.executeUpdate(upquery);
错误是: 不兼容的类型 发现:int 必需:java.sql.ResultSet;
在开始编码之前,我在全球范围内导入了某些内容:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
行动代码:
try{
int srl = Integer.parseInt(srlTF.getText());
String date = dateTF.getText();
String part = paTF.getText();
float amt = Float.parseFloat(amtTF.getText());
float exp = Float.parseFloat(expTF.getText());
float pro = Float.parseFloat(proTF.getText());
Class.forName("com.mysql.jdbc.Driver");
String usr = "root";
String pwd = "root";
String DB_URL = "jdbc:mysql://localhost/accounts?user="+usr+"&password="+pwd;
con = DriverManager.getConnection(DB_URL);
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CLOSE_CURSORS_AT_COMMIT);
String upquery = "insert into hostmargin_acc values(" + srl +","+ "'"+ date +","+ part +","+ amt +","+ exp +","+ pro + ");" ;
rs = stmt.executeUpdate(upquery);
msgLB.setText("BrO! Pwolichu! Save Chietu Kallan");
}
catch (Exception ex){
System.out.println(ex.getMessage());
}
答案 0 :(得分:1)
executeUpdate
返回受影响的行数。尝试
String upquery = "insert into hostmargin_acc values(?, ?, ?, ?, ?, ?)";
PreparedStatement statement =
connection.prepareStatement(upquery,
ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CLOSE_CURSORS_AT_COMMIT);
statement.setInt(1, srl);
// set more params...
int rows = statement.executeUpdate();
答案 1 :(得分:0)
executeUpdate()返回一个整数,以指示操作是否成功。您正在ResultSet类型的变量中收集返回的int变量,因此错误。
类似的问题你可以看看这个链接。 [Exception generated when updating table - java