1

我面临在 java 编程中编写更新 sql/oracle 命令的问题。我的数据库是甲骨文。我曾尝试过这种方法,但它不起作用。没有更新发生。有人请帮我吗?

conn = ds.getConnection();

String updateQ = "update ANI_999 set First_Name='d.getName()',HouseNo='d.getAddr1()',StreetName='d.getAddr2()',AppartmentSuite='d.getAddr3()',Town_City='d.getTownCity()',State='d.getState()',Postal_Code='d.getPostalCd()',Country='d.getCountry()' where CALLER_ID = 'msisdn'";

stmt = conn.prepareStatement(updateQ);
int result = stmt.executeUpdate(updateQ);

conn.commit();
conn.close();
4

3 回答 3

4
update ANI_999 set First_Name='d.getName()',HouseNo='d.getAddr1()',StreetName='d.getAddr2()',AppartmentSuite='d.getAddr3()',Town_City='d.getTownCity()',State='d.getState()',Postal_Code='d.getPostalCd()',Country='d.getCountry()' where CALLER_ID = 'msisdn'

First_Name='d.getName()'将按原样执行,不会用您的方法中的值替换d.getName(),对于所有其他字段也是如此。

而是试试这个:

String query = "update ANI_999 set first_name = ? where caller_id = 'msisdn'";

PreparedStatement pstmt = conn.prepareStatement(query);
pstmt.setString(1, d.getName());

int result = pstmt.executeUpdate();
于 2012-05-21T03:08:04.503 回答
0

只是为了完成 mprabhat 答案,你应该使用类似的东西

String query = "update ANI_999 set first_name = ? where caller_id = ?"; 

PreparedStatement pstmt = conn.prepareStatement(query); 
pstmt.setString(1, d.getName()); 
pstmt.setString(2, msisdn); 

int result = pstmt.executeUpdate(); 
conn.commit() // in case no autocommit is set.
于 2012-05-21T10:16:41.743 回答
0

尝试这个,

conn.setAutoCommit(true);

和,

Statement st = conn.createStatement();

和查询最终,

String updateQ = "update ANI_999 set First_Name='d.getName()',HouseNo='d.getAddr1()',StreetName='d.getAddr2()',AppartmentSuite='d.getAddr3()',Town_City='d.getTownCity()',State='d.getState()',Postal_Code='d.getPostalCd()',Country='d.getCountry()' where CALLER_ID = 'msisdn'";

if(st.execute(updateQ)) {
    System.out.println("UPDATE! SUCCESS");
}

我希望能帮助你。

于 2012-05-21T03:03:35.103 回答