我正在编写一个程序,我想从jtable中删除所选行,这必须通过使用java的存储过程来完成。 Ι创建以下存储过程(mssql):
USE [Lab]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_member_delete]
-- Add the parameters for the stored procedure here
@members_id int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
delete from members where Members_id=@members_id
END
调用程序的java代码
private void cmdDeleteActionPerformed(java.awt.event.ActionEvent evt) {
if(!jTable2.getSelectionModel().isSelectionEmpty()){
int click=JOptionPane.showConfirmDialog(this,"Are you sure","Confirm",JOptionPane.YES_NO_OPTION);
if(click==JOptionPane.YES_OPTION)
try {
Connect c=new Connect();
Connection con=c.start();
//Statement ps=con.createStatement();
CallableStatement ps=con.prepareCall ("{call usp_member_delete(?,?)}");
int index []=jTable2.getSelectedRows();
for (int i=0;i<index.length;i++)
{String id=""+jTable2.getValueAt(index[i], 0);
//ps.execute("delete from members where member_id="+id);
ps.executeUpdate(id);
c.stop();
}
DefaultTableModel mod=(DefaultTableModel)jTable2.getModel();
for (int i=0;i<index.length;i++)
mod.removeRow(index [i]-i);
}catch (Exception e){JOptionPane.showMessageDialog(this, e);}
}
}
我得到错误的方法executequery()不能在preparestatement或callablestatement上获取参数。请问建议吗? 提前感谢您的回答 Elpiniki
答案 0 :(得分:0)
我已阅读它并且我已经为其他目的创建了连接,例如insert.T(作为代码后面的示例)private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
String name=jTextField2.getText();
String email =jTextField3.getText();
String site=jTextField4.getText();
String cv=jTextArea2.getText();
String category =jComboBox2.getSelectedItem().toString();
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
try (Connection con = DriverManager.getConnection("jdbc:sqlserver://127.0.0.1\\ELPINIKI-TOSH\\SQLEXPRESS:1433;databaseName=Lab","sa","fir3w@ll")) {
CallableStatement ps=con.prepareCall ("{call usp_member_insert(?,?,?,?,?,?,?)}");
ps.registerOutParameter("Members_id", java.sql.Types.INTEGER);
ps.setString(2,name);
ps.setString(3,email);
ps.setString(4,site);
ps.setString(5,cv);
ps.setString(6,category);
ps.registerOutParameter("MemberCategory_id",java.sql.Types.INTEGER);
int x=0; x=ps.executeUpdate();
if (x!=0);
JOptionPane.showMessageDialog(this, "Data inserted");
jTextField2.setText("");
jTextField3.setText("");
jTextField4.setText("");
jComboBox2.setSelectedIndex(0);
jTextArea2.setText("");
con.close();
}
}catch(ClassNotFoundException | SQLException e)
{
System.out.println(e);
}
}
问题在于我附加的上一个代码。我使用行选择,我想使用该过程删除该行。 谢谢