gpt4 book ai didi

java - SQL JAVA JDBC deleteForCustomerID

转载 作者:行者123 更新时间:2023-11-29 21:10:22 25 4
gpt4 key购买 nike

此方法本质上应该删除 creditcard 类中的客户(它在删除时设置为级联) DB)但我认为这是不正确的。

我的sql语句是:

final static String deleteSQL = "DELETE FROM creditcard WHERE customer_id = ?;"; 

任何建议将不胜感激。我只是不明白我的做法错在哪里。

public void deleteForCustomerID(Connection connection, Long customerID) throws SQLException, DAOException 
{
if (customerID == null)
{
throw new DAOException("Trying to delete customer with NULL ID");
}

PreparedStatement ps = null;
try
{
ps = connection.prepareStatement(deleteSQL);
ps.setLong(1, customerID);
int rows = ps.executeUpdate();

}
finally {}

}

JUnit 测试的代码是:

@Test
public void testDeleteForCustomerID() throws Exception
{
DataSource ds = DataSourceManager.getDataSource();
Connection connection = ds.getConnection();
// Do not commit changes made by this test.
connection.setAutoCommit(false);

CreditCardDAO dao = new CreditCardDaoImpl();

CreditCard ccard = dao.retrieveForCustomerID(connection, customerID);
assertNotNull(ccard);
dao.deleteForCustomerID(connection, customerID);
CreditCard ccard2 = dao.retrieveForCustomerID(connection, customerID);
assertNull(ccard2);

// Do not commit changes made by this test.
connection.rollback();
connection.setAutoCommit(true);
connection.close();
}

我收到以下错误:

java.lang.AssertionError: expected null, but was: at org.junit.Assert.fail(Assert.java:88) at org.junit.Assert.failNotNull(Assert.java:755) at org.junit.Assert.assertNull(Assert.java:737) at org.junit.Assert.assertNull(Assert.java:747)

最佳答案

我认为您的deletesql字符串应该按如下方式更正。引号内不需要分号

   final static String deleteSQL = "DELETE FROM creditcard WHERE customer_id = ?";

关于java - SQL JAVA JDBC deleteForCustomerID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36424759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com