2016-08-21 42 views
0

我不知道爲什麼我有這樣的錯誤......請,如果有人能告訴我什麼是錯在此:你在你的SQL語法錯誤com.mysql.jdbc.exception你的sql語法有錯誤;

com.mysql.jdbc.exception;檢查對應於你的MySQL服務器版本的手冊正確的語法使用

mat_pay="maybe"; 
Class.forName("com.mysql.jdbc.Driver"); 
connec = DriverManager.getConnection("jdbc:mysql://localhost/babe","root",""); 
stmt = connec.prepareStatement("INSERT INTO ? VALUES (, ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?) "); 
       stmt.setString(1,mat_pay); 
       stmt.setInt(2,septembre.var_m_p_g1); //septembre.var_m_p_g1 has a value 'integer' 
       stmt.setInt(3,id_g2); //septembre.var_m_p_g1 has a value 'integer' 
       stmt.setInt(4,0); 
       stmt.setInt(5,0); 
       stmt.setInt(6,0); 
       stmt.setInt(7,0); 
       stmt.setInt(8,0); 
       stmt.setInt(9,0); 
       stmt.setInt(10,0); 
       stmt.setInt(11,0); 
       stmt.setInt(12,0); 
       stmt.setInt(13,0); 
       stmt.executeUpdate(); 
+0

可能有更多的問題,但很難說,因爲你沒有打算格式化你的代碼。你要求人們來幫助你,所以你最好有興趣讓他們輕鬆地做到這一點。 – MarsAtomic

+0

發佈(使用[edit]添加到您的問題)您期望執行的確切SQL。 –

回答

1

你不能用一個?佔位符代替表名。該聲明必須明確指定該表。如果你真的必須(SQL注入安全漏洞)做到這一點,你可以構建語句動態地使用字符串格式化

// assuming mat_pay is the name of a variable containing the table name 
String query = String.format("INSERT INTO %s VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", mat_pay); 

stmt = connec.prepareStatement(query); 
stmt.setInt(1,septembre.var_m_p_g1); //septembre.var_m_p_g1 has a value 'integer' 
stmt.setInt(2,id_g2); //septembre.var_m_p_g1 has a value 'integer' 
stmt.setInt(3,0); 
stmt.setInt(4,0); 
stmt.setInt(5,0); 
stmt.setInt(6,0); 
stmt.setInt(7,0); 
stmt.setInt(8,0); 
stmt.setInt(9,0); 
stmt.setInt(10,0); 
stmt.setInt(11,0); 
stmt.setInt(12,0); 
stmt.executeUpdate(); 

mat_pay的價值不應該被一些用戶/客戶端輸入,但完全在你的控制,不受外部操縱。否則,您將面臨SQL注入攻擊。

請注意,您在值列表的開頭還有一個額外的逗號。