2014-09-20 208 views
-1

我在執行此代碼時出現問題, 我調用了void函數(insertfaq()),該函數引用另一個方法。 成功調用insertfaq()函數,但其​​他函數(實際上在insertfaq()函數中存在錯誤,但無關緊要)。另一種方法不能調用,而其他方法調用

該程序應該運行良好。當調用insertfaq()函數時,下一個pisahkeyword()應該運行良好,它也包含checkkeyword()函數。但checkkeyword()從未運行,並且沒有錯誤顯示。

下面的代碼

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package controller; 

import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.StringTokenizer; 

/** 
* 
* @author Pcs.Aulia.Ardy 
*/ 
public class userfungsi { 

    //getting basic id 
    public String idUser; 
    String tanggal = "01/01/2014"; 
    public String judul; 
    public String isi; 
    private final String status = "1"; 

    private ResultSet rs_getIdFaq; 
    private Statement stmt_get_getIdFaq; 
    private String idFaq; 

    private Statement stmt_insertFaq; 

    public String keyword; 
    private String tempKeyword; 

    private ResultSet rs_getCheckKeyword; 
    private Statement stmt_getCheckKeyword; 
    private String CheckKeyword; 

    private Statement stmt_updateOrInsertKeyword; 

    private ResultSet rs_getId_keywordCount; 
    private Statement stmt_getId_keywordCount; 
    private String id_keyword_count; 

    private Statement stmt_insertNewKeywordCount; 

    public userfungsi(String iduser1, String judul1, String isi1, String keyword1) { 
     this.idUser = iduser1; 
     this.judul = judul1; 
     this.isi = isi1; 
     this.keyword = keyword1; 
    } 

    public void mainFunction() { 
     get_id(); 
     getId_keywordCount(); 
     pisahKeyword(); 
     insertFaq(); 
    } 

    public void get_id() { 
     try { 
      koneksi k_getIDfaq = new koneksi(stmt_get_getIdFaq); 
      k_getIDfaq.setSt(stmt_get_getIdFaq); 
      k_getIDfaq.hubung(); 
      String sql_get_id = "select nextval('seq_tb_faq')"; 
      rs_getIdFaq = k_getIDfaq.select(sql_get_id); 
      while (rs_getIdFaq.next()) { 
       idFaq = rs_getIdFaq.getString("nextval"); 
      } 
      System.out.println("idFaq berhasil didapatkan: " + idFaq); 
      k_getIDfaq.putus(); 

     } catch (SQLException | ClassNotFoundException e) { 
      e.printStackTrace(); 
      System.out.println("idFaq gagal didapatkan"); 
     } 
    } 

    public void getId_keywordCount() { 
     try { 
      koneksi k_getId_keywordCount = new koneksi(stmt_getId_keywordCount); 
      k_getId_keywordCount.setSt(stmt_getId_keywordCount); 
      k_getId_keywordCount.hubung(); 
      String sql_getId_keywordCount = "select nextval('seq_tb_keyword_count')"; 
      rs_getId_keywordCount = k_getId_keywordCount.select(sql_getId_keywordCount); 
      while (rs_getId_keywordCount.next()) { 
       id_keyword_count = rs_getId_keywordCount.getString(1); 
       System.out.println("id keyword count berhasil didapatkan: " + id_keyword_count); 
      } 
      k_getId_keywordCount.putus(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("id keywordcount gagal didapatkan"); 
     } 

    } 

    public void insertFaq() { 
     try { 

      koneksi k_insertFaq = new koneksi(stmt_insertFaq); 
      k_insertFaq.setSt(stmt_insertFaq); 
      k_insertFaq.hubung(); 
      String sql_insertFaq = "INSERT INTO tb_faq(\n" 
        + "   id_faq, id_user, tanggal_input, judul, isi, status)\n" 
        + " VALUES ('" + idFaq + "', '" + idUser + "', '" + tanggal + "', '" + judul + "', '" + isi + "', true)"; 
      stmt_insertFaq = k_insertFaq.statement(sql_insertFaq); 
      stmt_insertFaq.executeUpdate(sql_insertFaq); 
      k_insertFaq.putus(); 
      System.out.println("insert faq berhasil dilakukan"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("insertFaq gagal dijalankan"); 
     } 
    } 

    public void insertNewKeywordCount() { 
     try { 

      koneksi k_insertNewKeywordCount = new koneksi(stmt_insertNewKeywordCount); 
      k_insertNewKeywordCount.setSt(stmt_insertNewKeywordCount); 
      k_insertNewKeywordCount.hubung(); 
      String sql_insertNewKeywordCount = "INSERT INTO tb_keyword_count(\n" 
        + "   id_keyword_count, id_keyword, id_faq, count)\n" 
        + " VALUES ('" + id_keyword_count + "', '" + tempKeyword + "', '" + idFaq + "', 1);"; 
      stmt_insertNewKeywordCount = k_insertNewKeywordCount.statement(sql_insertNewKeywordCount); 
      stmt_insertNewKeywordCount.executeUpdate(sql_insertNewKeywordCount); 
      k_insertNewKeywordCount.putus(); 
      System.out.println("insert new keyword count berhasil dilakukan"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("insert new keyword gagal dilakukan"); 
     } 
    } 

    public void pisahKeyword() { 
     try { 
      StringTokenizer sktx = new StringTokenizer(keyword, ","); 
      while (sktx.hasMoreElements()) { 
       tempKeyword = (String) sktx.nextElement(); 
       System.out.println("daftar keyword: " + tempKeyword); 
       System.out.println("pisah keyword berhasil dijalankan"); 
       checkKeyword(); 
       System.out.println("method checkKeyword berhasil dipanggil"); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("pisah keyword gagal dijalankan"); 
     } 

    } 

    public void checkKeyword() { 
     try { 
      koneksi k_checkKeyword = new koneksi(stmt_getCheckKeyword); 
      k_checkKeyword.setSt(stmt_getCheckKeyword); 
      k_checkKeyword.hubung(); 
      String sql_checkKeyword = "select id_keyword from public.tb_keyword where id_keyword = '" + tempKeyword + "'"; 
      rs_getCheckKeyword = k_checkKeyword.select(sql_checkKeyword); 
      while (rs_getCheckKeyword.next()) { 
       CheckKeyword = rs_getCheckKeyword.getString("id_keyword"); 
       updateOrInsertKeyword(); 
       System.out.println("check keyword berhasil dijalankan"); 
      } 
      k_checkKeyword.putus(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("check keyword gagal dijalankan"); 

     } 
    } 

    public void updateOrInsertKeyword() { 
     try { 
      //check if exist 
      if (tempKeyword.equals(CheckKeyword)) { 
       koneksi k_updateOrInsertKeyword = new koneksi(stmt_updateOrInsertKeyword); 
       k_updateOrInsertKeyword.setSt(stmt_updateOrInsertKeyword); 
       k_updateOrInsertKeyword.hubung(); 
       String sql_updateOrInsertKeyword = "UPDATE tb_keyword_count\n" 
         + " SET count=count + 1\n" 
         + "where\n" 
         + "id_keyword = '" + tempKeyword + "'"; 
       stmt_updateOrInsertKeyword = k_updateOrInsertKeyword.statement(sql_updateOrInsertKeyword); 
       stmt_updateOrInsertKeyword.executeUpdate(sql_updateOrInsertKeyword); 
       k_updateOrInsertKeyword.putus(); 
       System.out.println("update keyword berhasil dijalankan"); 
      } else { 
       koneksi k_updateOrInsertKeyword = new koneksi(stmt_updateOrInsertKeyword); 
       k_updateOrInsertKeyword.setSt(stmt_updateOrInsertKeyword); 
       k_updateOrInsertKeyword.hubung(); 
       String sql_updateOrInsertKeyword_notExist = "INSERT INTO tb_keyword(\n" 
         + "   id_keyword, \"desc\")\n" 
         + " VALUES ('" + tempKeyword + "', 'test1');"; 
       stmt_updateOrInsertKeyword = k_updateOrInsertKeyword.statement(sql_updateOrInsertKeyword_notExist); 
       stmt_updateOrInsertKeyword.executeUpdate(sql_updateOrInsertKeyword_notExist); 
       k_updateOrInsertKeyword.putus(); 
       insertNewKeywordCount(); 
       System.out.println("method insertNewKeywordCount() berhasil dipanggil"); 
       System.out.println("insert keyword baru berhasil dijalankan"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

} 

,這是錯誤顯示:

idFaq berhasil didapatkan: 135 
id keyword count berhasil didapatkan: 54 
daftar keyword: belong1 
pisah keyword berhasil dijalankan 
method checkKeyword berhasil dipanggil 
daftar keyword: belong2 
pisah keyword berhasil dijalankan 
method checkKeyword berhasil dipanggil 
daftar keyword: belong3 
pisah keyword berhasil dijalankan 
method checkKeyword berhasil dipanggil 
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "pk_tb_faq" 
    Detail: Key (id_faq)=(135) already exists. 
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157) 
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886) 
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:331) 

謝謝你的幫忙。 乾杯:D

+0

如果你可以把你的代碼翻譯成英文,那將會很棒。否則很難快速理解。 – 2014-09-20 12:08:21

+0

代碼風格...不是很好,特別是變量名稱中的下劃線。無論如何,你已經有你的例外的答案,你試圖插入已經在你的數據庫中的值。調試你的代碼來找到這個地方爲什麼你插入兩次。 – 2014-09-20 12:09:54

回答

0

插入兩次相同的值id_Faq。在你的表格屬性中,id_Faq具有唯一的約束。所以id_Faq的每個值都應該是唯一的。

0

stmt_insertFaq.executeUpdate(sql_insertFaq);

拋出錯誤,之後您正在調用putus()方法。如果你想執行 putus()方法調用即使在異常的情況下,然後在try catch塊中包裝上面的調用並且吃掉異常。理想情況下,你應該糾正異常,然後所有東西都落在地方

相關問題