2014-01-30 62 views
0

我正在修改之前由其他人編寫的使用JDBC的Java Web應用程序。數據庫是使用PHPMyAdmin創建的。在此過程中,我需要重命名其中一個表格。我在代碼中用正確的名稱替換了舊名稱,但現在應用程序似乎在嘗試插入新記錄時正在搜索舊錶格。更改表名後ExecuteUpdate()異常

這是例外:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.reclamation' doesn't exist 

reclamation是表的舊名。

這是我的代碼:

public class ServletUserEnquiryForm extends HttpServlet{ 
    public void init(ServletConfig config) throws ServletException{ 

    super.init(config); 
    } 

    /**Process the HTTP Get request*/ 
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
          throws ServletException, IOException{ 

    request.setCharacterEncoding("UTF-8"); 

    String shop = request.getParameter("shop"); 
    System.out.println(shop); 
    String name = request.getParameter("names"); 
    System.out.println(name); 
    String tell = request.getParameter("tell"); 
    String stoke = request.getParameter("stoke"); 
    String model = request.getParameter("model"); 
    String barcod = request.getParameter("barkod"); 
    String bon = request.getParameter("bon"); 
    String fack = request.getParameter("fack"); 
    if(fack.equals("")){ 
    fack = "0"; 
    } 
    String comentar = request.getParameter("comentar"); 
    String reasonable = request.getParameter("reasonable"); 
    if(reasonable == "yes") 
    { 
    reasonable = "yes"; 
    } 
    else 
    { 
    reasonable = "no"; 
    } 

    String in_store = request.getParameter("in_store"); 
    if(in_store == "yes") 
    { 
    in_store = "yes"; 
    } 
    else 
    { 
    in_store = "no"; 
    } 

    String satisfaction = request.getParameter("satisfaction"); 
    if(satisfaction == "zamqna") 
    { 
    satisfaction = "Замяна"; 
    } 
    else 
    if(satisfaction == "remont") 
    { 
     satisfaction = "Ремонт"; 
    } 
    else 
     if(satisfaction == "namalenie") 
     { 
      satisfaction = "Намаление"; 
     } 
     else 
      if(satisfaction == "return") 
      { 
       satisfaction = "Връщане на сума"; 
      } 
    //String okomplektovka = request.getParameter("okomplektovka"); 

    String type_return = request.getParameter("type_return"); 
    if(type_return == "sum") 
    { 
    type_return = "sum"; 
    } 
    else 
    if(type_return == "bank") 
    { 
     type_return = "bank"; 
    } 
    else 
     type_return = ""; 

    String sum = request.getParameter("sum"); 
    if(sum == "") 
    { 
    sum = "0.00"; 
    } 

    String iban = request.getParameter("iban"); 
    if(iban =="") 
    { 
    iban = "N/A"; 
    } 
    String servis = request.getParameter("servis"); 

    if(reasonable != null && in_store != null && satisfaction != null && type_return != null) 
    { 
    try { 
     Connection con = dbSQL.getConnection(); 
     String tableName = "shop_"+shop; 
     String sql = "INSERT INTO " + tableName + " (name,tel,product_type,model,genkod,receipts,invoice,description,reasonable,in_store,satisfaction,type_return,sum,iban,shop,data_in,servis)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate(), ?)"; 
     PreparedStatement ps = con.prepareStatement(sql); 
     ResultSet rs = null; 
     ps.setString(1, name); 
     ps.setString(2, tell); 
     ps.setString(3, stoke); 
     ps.setString(4, model); 
     ps.setString(5, barcod); 
     ps.setString(6, bon); 
     ps.setString(7, fack); 
     ps.setString(8, comentar); 
     ps.setString(9, reasonable); 
     ps.setString(10, in_store); 
     ps.setString(11, satisfaction); 
     ps.setString(12, type_return); 
     ps.setString(13, sum); 
     ps.setString(14, iban); 
     ps.setString(15, shop); 
     ps.setString(16, servis); 
     ps.executeUpdate(); 
     ps.close(); 

    } 
    catch(Exception e){ 

    } 
    finally { 


     } 

    } 
    } 

} 

正常工作,直到它到達ps.executeUpdate(),我看不到的地方似乎是問題。

這是dbSQL定義:

public class dbSQL { 

private static final String DB_HOST ="192.168.15.26"; 
private static final String DB_USER = "root"; 
private static final String DB_PASS = "passs"; 
private static final String DB_NAME = "test?characterEncoding=UTF-8"; 

private static final String useDB = "mysql"; 


public static Connection getConnection() throws ClassNotFoundException { 

Connection conn = null; 
    try { 

      Class.forName("com.mysql.jdbc.Driver"); 
      conn = DriverManager.getConnection("jdbc:mysql://" + DB_HOST + ":3306/" + DB_NAME, DB_USER, DB_PASS); 

    } catch (SQLException e) { 
     e.printStackTrace(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    return conn; 
    } 

} 
+0

OT:真的這個比較if(合理==「是」)適合你嗎? – eltabo

+0

有沒有在你的數據庫中引用舊的表名,這樣的觸發器? – eltabo

+0

SHOW TRIGGERS找不到任何內容。 – Iv4etooo

回答

0

哪裏是 「表名」 定義的? 您的SQLExceptions claimes,該tabel不存在,因此請檢查「tableName」的定義並將其設置爲表的正確名稱。

+0

我想用幾個相同的表(但包含不同的信息)相同的代碼,這就是爲什麼我有 String tableName =「shop _」+ shop; String sql =「INSERT INTO」+ tableName +「(name,tel ...」; 調試時,我發現查詢看起來不錯,它指向右表。 – Iv4etooo