MySQL server version for the right syntax to use near '' at line 1 問題描述: 線上日志在執行數據庫表insert操作時,出現如下異常: ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:? You have an error
MySQL server version for the right syntax to use near '' at line 1問題描述:
線上日志在執行數據庫表insert操作時,出現如下異常:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:?
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
問題原因分析:
一般數據庫SQL 異常會將sql拼寫錯誤的文本解析錯誤地點放在near"" 中間引號中,而恰恰這次near語句出現出錯地點沒有標示,但是可以確定一點就是這個錯誤一定是sql語句錯誤造成的。
原因查找:
原因分析唯一可以確定的就是這個sql 拼寫一定是錯誤的,那么是一定要查看源碼拼寫是否正確。
代碼如下:
public String batchSave(Mapparams) { Collection collection = (Collection )params.get("collection"); StringBuffer buffer = new StringBuffer("insert into sem_creative(id, platCreativeId, platPlanId, platAdgroupId, planId, adgroupId, " + "title, desc1, desc2, pcDestinationUrl, pcDisplayUrl, mobileDestinationUrl, mobileDisplayUrl," + "pause, status, valid, accountId, platform, opstatus, creator, createTime) values"); for(Creative c : collection) { buffer.append("(").append(c.getId()).append(",") .append(StringUtil.emptyToNULL(c.getPlatCreativeId())).append(",") .append(StringUtil.emptyToNULL(c.getPlatPlanId())).append(",") .append(StringUtil.emptyToNULL(c.getPlatAdgroupId())).append(",") .append(c.getPlanId()).append(",") .append(c.getAdgroupId()).append(",") .append(StringUtil.emptyToNULL(c.getTitle())).append(",") .append(StringUtil.emptyToNULL(c.getDesc1())).append(",") .append(StringUtil.emptyToNULL(c.getDesc2())).append(",") .append(StringUtil.emptyToNULL(c.getPcDestinationUrl())).append(",") .append(StringUtil.emptyToNULL(c.getPcDisplayUrl())).append(",") .append(StringUtil.emptyToNULL(c.getMobileDestinationUrl())).append(",") .append(StringUtil.emptyToNULL(c.getMobileDisplayUrl())).append(",") .append(c.getPause()).append(",") .append(c.getStatus()).append(",") .append(c.getValid()).append(",") .append(c.getAccountId()).append(",") .append(c.getPlatform()).append(",") .append(c.getOpstatus()).append(",") .append(StringUtil.emptyToNULL(c.getCreator())).append(",") .append(StringUtil.emptyToNULL(c.getCreateTime().toString())).append("),"); } buffer.deleteCharAt(buffer.length()-1); return buffer.toString(); }
?這里可以看到SQL語句,拼寫是正確的,其中每個value值,對于空值的處理也正確。那為什么會報錯呢?
等等,一定有哪里出錯了!
for(Creative c : collection) {
?再看看這行代碼,如果這個的集合類為空,會怎么樣呢?這個一下錯誤就找到了,在collection為空的情況下,返回的SQL語句就只有前半句,執行就會報上述所描述錯誤。
問題追蹤:
這個地方在一般情況下沒有報錯,也很少出現;那么為什么這次會出現呢?
這個地方還是得從源碼入手
public final void syncUpload(String filePath, int segment) throws Exception{ if(segment > MAX_SEGMENT) { throw new IllegalArgumentException("分段上傳數據量過大!"); } if(segment <= 0) { this.segment = DEFAULT_SEGMENT; } CSVReadercsvFileReader = getCSVReader(filePath); Iterator it = csvFileReader.iterator(); List segmentList = new ArrayList(); while(true) { try { while(it.hasNext()) { if(segmentList.size() == this.segment) { try { syncBatchSave(segmentList); }catch(Exception e) { logger.error("批量添加異常: ", e); } segmentList = new ArrayList(); } E e = it.next(); segmentList.add(parse(e)); } }catch(Exception e) { logger.error("CSV文件解析異常: ", e); continue; } break; } syncBatchSave(segmentList); }
?這里傳入的segmentList看不會為空,但是parse(e)拋出異常時,則會在插入傳入的集合不為null,但是集合的 size為0,所以在如上拼寫SQL語句時,就會出現更新異常。
總結:
1.在接口的傳入參數,一定要做邊界校驗,比如判空或者最大最小判斷(如果集合大小超過20W也會出現異常);
2.團隊代碼規范中,要整理出代碼規范要點,并每周定時review,對于不符合基本要求規范的懲罰5元團隊建設基金;
1 樓 Engberber 22 小時前 今日決定將以前遇到的問題以及處理方式做一個分享聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com