2013-01-04 71 views
2

編輯:我有一封信,它現在工作。SQL更新語句不是從Java工作,但手動工作?

我有下面的SQL語句

UPDATE names set sent_count = sent_count + 1 where user_name = 'name' AND category = 'test' AND service = 'test' 

這種工作方式是每個I運行從PGAdmin3的sql時間增加sent_count,但我有在Java程序中使用PostgreSQL JDBC下面的方法,它不拋出任何例外,但它不增加sent_count這裏是低於

public void increaseUsernameResponseCount(String userName, String category, String service, String country) throws SQLException 
{ 
    Connection conn = null; 
    PreparedStatement pst = null; 
    // ResultSet rs = null; 

    try 
    { 
     Class.forName("org.postgresql.Driver"); 
     conn = DriverManager.getConnection(url, props); 
     pst = conn.prepareStatement("UPDATE names set sent_count = sent_count + 1 where user_name = ? AND category = ? AND service = ?"); 
     pst.setString(1, userName); 
     pst.setString(2, category); 
     pst.setString(3, service); 

     int count = pst.executeUpdate(); 
     System.out.println(count); 

    } 
    catch (Exception e) 
    { 
     System.out.println(e); 
    } 
    finally 
    { 
     pst.close(); 
     conn.close(); 
    } 
} 

計數櫃檯打印0的方法時,這個方法是跑了。任何想法爲什麼它不能從該方法工作,但它在我執行sql查詢時起作用?

+3

我認爲你已經確定'userName','category'和'service'與你的pgadmin會話完全一樣嗎? – NPE

+0

使用'System.out.println'打印用戶名,類別,服務的值。 –

+2

當然,確保你連接到同一個數據庫,而不是某個UAT副本或其他東西(已經存在:)) – NPE

回答

2

檢查您是否有兩個包含相同表的數據庫。如果您在兩個不同位置工作並在從轉儲中導入數據庫名稱時不斷更改數據庫名稱,通常會發生這種情況

同時檢查「url」和「props」字符串是否對數據庫正確。