我在执行此代码时遇到问题, 我调用了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 :(得分:0)
id_Faq
的相同值被插入两次。在您的表属性中,id_Faq具有唯一约束。因此id_Faq
的每个值都应该是唯一的。
答案 1 :(得分:0)
stmt_insertFaq.executeUpdate(sql_insertFaq);
抛出错误,之后你正在调用putus()方法。如果你想执行 即使在异常的情况下调用putus()方法,然后在try catch块中包含上面的调用并吃掉异常。理想情况下,你应该纠正异常,然后一切都落到实处