2011-11-02 93 views
0

大家好, 这是我的代码,Update语句

public static boolean updateDB() { 
    Connection cn = null; 
    PreparedStatement pstmt = null; 
    ResultSet rs = null; 
    boolean result = false; 
    int number; 
    String insetSQL ="update quan set ten_quan='lin' where id=1"; 
    try { 
    cn = LocalDatabasePooling.getInstance().getConnection(); 
    pstmt = cn.prepareStatement(insetSQL); 
    pstmt.executeUpdate(); 
    result = true; 
    } catch (SQLException e) { 
    e.printStackTrace(); 
    } finally { 
    if (rs != null) { 
     try { 
     rs.close(); 
     } catch (SQLException sqle) { } 
    } 
    if (pstmt != null) { 
     try { 
     pstmt.close(); 
     } 
     catch (SQLException sqle) { } 
    } 
    if (cn1 != null) { 
     try { 
     cn1.close(); 
     } 
     catch (SQLException sqle) { } 
    } 
    } 
    return false; 
} 

上面是正确的代码?但是当我调试时,我看到线

number = pstmt.executeUpdate(); 

没有被执行,它下面的代码也没有执行太多。

当我添加看

pstmt.executeUpdate(); 

我看到这个信息

错误(评估

什么是错的过程?

+0

你为什么要改变/修复的问题的代码不留在我的答案反馈?现在我的回答对没有读过原始问题的人来说是没有意义的。 – BalusC

回答

2

显然例外是被扔这完全被你的代码忽略了,例如:

try { 
    // Your code was here. 
} catch (SQLException e) { 
    // But you didn't put anything here. 
} 

你至少应该记录/打印堆栈跟踪

try { 
    // Your code was here. 
} catch (SQLException e) { 
    e.printStackTrace(); 
} 

,或者甚至更好,只是把它。

public SomeObject someMethod() throws SQLException { 
    try { 
     // Your code was here. 
    } finally { 
     // Surely close resources here. 
    } 
} 

例外包含了丰富的有关问题原因的信息,他们不应该被忽视,除非你真的知道自己在做什么。

0

pstmt.executeUpdate();没有执行 - 没关系。但是你得到了什么?命令行中的异常跟踪?尝试添加如上所述的BalusC的try catch块并再次运行该问题。你的sql可能有问题,比如表不存在,列名不正确或者你的datasouce根本没有返回连接。你可能会得到一个空指针异常。

0

你需要放?在语句中,然后将这些值插入到preparedStatement中。

如果您要执行一个简单的“直” SQL,就没有必要使用PreparedStatement