{"id":1382,"date":"2015-02-19T14:19:00","date_gmt":"2015-02-19T06:19:00","guid":{"rendered":"http:\/\/www.menglanglang.cn\/?p=1382"},"modified":"2016-02-27T11:56:33","modified_gmt":"2016-02-27T03:56:33","slug":"java%e4%b8%ad%e4%bf%ae%e6%94%b9mysql%e7%9a%84%e5%88%86%e9%9a%94%e7%ac%a6","status":"publish","type":"post","link":"http:\/\/www.menglanglang.cn\/?p=1382","title":{"rendered":"Java\u4e2d\u4fee\u6539MySQL\u7684\u5206\u9694\u7b26"},"content":{"rendered":"<p>\u5728 WEB \u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u60f3\u5728 Java \u540e\u53f0\u76f4\u63a5\u4fee\u6539 MySQL \u6570\u636e\u5e93\u7684\u5206\u9694\u7b26\uff0c\u5728 MySQL \u7ec8\u7aef\uff0c\u6267\u884c\u4e0b\u9762\u7684\u547d\u4ee4\u5b8c\u5168\u6b63\u5e38\uff1a<br \/>\n<code>delimiter ||<\/code><\/p>\n<p>\u4f46\u53ea\u8981\u662f\u901a\u8fc7 Java \u4fee\u6539\uff1a<br \/>\n<code>ps.execute(\"delimiter ||\");<\/code><\/p>\n<p>\u5c31\u4f1a\u629b\u51fa\u5982\u4e0b\u9519\u8bef\uff1a<\/p>\n<p><span style=\"color: #ff0000;\">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 &#8216;delimiter ||&#8217; at line 1<\/span><\/p>\n<p>\u7f51\u4e0a\u641c\u4e86\u4e00\u5927\u5708\uff0c\u8d44\u6599\u662f\u5c11\u4e4b\u53c8\u5c11\uff0c\u6839\u672c\u627e\u4e0d\u5230\u4e00\u4e2a\u597d\u7684\u89e3\u51b3\u529e\u6cd5\u3002<\/p>\n<p>\u516b\u4e5d\u5e74\u524d\u5c31\u6709\u4eba\u78b0\u5230\u4e86\u8fd9\u79cd\u95ee\u9898\uff0c\u8be6\u7ec6\u89c1<a href=\"http:\/\/forums.mysql.com\/read.php?39,130834,130834#msg-130834\" target=\"_blank\">http:\/\/forums.mysql.com\/read.php?39,130834,130834#msg-130834<\/a><\/p>\n<p>DELIMITER is a command that is pre-parsed by a client that runs SQL scripts, e.g. mysql command-line tool or MySQL Query Browser.<\/p>\n<p>It should be unnecessary to use DELIMITER if you&#8217;re executing one statement at a time through JDBC or another programming interface. DELIMITER is necessary only when the semicolon inside a trigger or procedure body is ambiguous with the semicolon at the end of the the CREATE or ALTER statement. If you&#8217;re using an API that executes only one statement at a time, there&#8217;s no ambiguity.<\/p>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>Most SQL script implementations (not just MySQL) will have a subset of statements in the script language that should be caught by the client and handled there.<\/p>\n<p>For instance, QUIT or EXIT is frequently in SQL scripting languages, and it wouldn&#8217;t make much sense to pass that statement to the RDBMS server. It&#8217;s a command for the client to do something, not the server.<\/p>\n<p>See <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/mysql-commands.html\" rel=\"nofollow\">http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/mysql-commands.html<\/a> for a list of mysql client commands. Any of these can appear in SQL scripts for MySQL, but should be handled by the client, and not be sent to the MySQL Server to execute.<\/p>\n<p>Oh another comment about supporting DELIMITER.<\/p>\n<p>If you&#8217;re processing a SQL script, your code does need to pay attention to DELIMITER, so it knows how to parse the following statements. Statements can be multiple lines long. So instead of reading line by line, you need to read input from the script file up to the next occurrence of the characters defined by DELIMITER. The delimiter can be a single character or multiple characters:<\/p>\n<pre>DELIMITER !!\r\n\r\nSELECT foo \r\nFROM bar!!\r\n\r\nDELIMITER ;\r\n\r\nSELECT baz \r\nFROM bling;<\/pre>\n<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>You don&#8217;t need &#8220;delimiter&#8221; to create a stored procedure using Connector\/J or any other client that isn&#8217;t &#8220;mysql&#8221;. The delimiter exists *only* to tell &#8220;mysql&#8221; that you&#8217;re done typing, and that the command should be sent to the server. With other *libraries*, that&#8217;s done by the very fact that you&#8217;ve called some method to send the string representing the command to the server.<\/p>\n<p>Also, there is no good reason for client libraries to be parsing the SQL sent to them more than they absolutely need to, it will cause performance degradation for every one who uses them.<\/p>\n<p>\u4e0a\u9762\u5927\u6982\u8bf4\u7684\u610f\u601d\u662f\uff0cDELIMITER \u662f\u4e00\u4e2a\u5ba2\u6237\u7aef\u5de5\u5177\u7684\u547d\u4ee4\uff0c\u5728\u901a\u8fc7 JDBC \u9a71\u52a8\u6216\u8005\u5176\u5b83\u7a0b\u5e8f\u63a5\u53e3\u6267\u884c\u4e00\u6761\u8bed\u53e5\u7684\u65f6\u5019\uff0c\u662f\u6ca1\u6709\u5fc5\u8981\u53bb\u4fee\u6539 MySQL \u9ed8\u8ba4\u7684\u5206\u9694\u7b26\u7684\uff0c\u800c\u53ea\u6709\u5f53\u8bed\u53e5\u4e2d\u6709\u00a0trigger\uff08\u89e6\u53d1\u5668\uff09 \u6216\u8005 procedure\uff08\u5b58\u50a8\u8fc7\u7a0b\uff09 \u65f6\uff0c\u624d\u9700\u8981\u4fee\u6539\u5176\u9ed8\u8ba4\u5206\u9694\u7b26\u3002\u5982\u679c\u662f\u6bcf\u6b21\u901a\u8fc7 API \u6267\u884c\u4ec5\u4ec5\u4e00\u6761\u8bed\u53e5\uff0c\u662f\u6ca1\u6709\u5fc5\u8981\u53bb\u4fee\u6539\u5b83\u7684\u3002<\/p>\n<p>\u6700\u540e\u8bd5\u4e86\u597d\u591a\u6b21\uff0c\u624d\u53d1\u73b0\uff0c\u5728 Java \u4e2d\u521b\u5efa\u5b58\u50a8\u8fc7\u7a0b\u6216\u8005\u662f\u89e6\u53d1\u5668\uff0c\u90fd\u4e0d\u9700\u8981\u4fee\u6539\u5206\u9694\u7b26\u3002SQL \u9ed8\u8ba4\u6267\u884c\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5de5\u5177\u67e5\u770b\u5df2\u7ecf\u6210\u529f\u521b\u5efa\u4e86\u7684\u5b58\u50a8\u8fc7\u7a0b\u548c\u89e6\u53d1\u5668\uff0c\u6267\u884c\u5b8c\u5168\u6b63\u5e38\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 WEB \u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u60f3\u5728 Java \u540e\u53f0\u76f4\u63a5\u4fee\u6539 MySQL \u6570\u636e\u5e93\u7684\u5206\u9694\u7b26 &hellip; <a href=\"http:\/\/www.menglanglang.cn\/?p=1382\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ai_generated_summary":"","wpai_meta_description":"","footnotes":""},"categories":[135],"tags":[600,186,327],"class_list":["post-1382","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-mysql","tag-327"],"_links":{"self":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/1382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1382"}],"version-history":[{"count":2,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/1382\/revisions"}],"predecessor-version":[{"id":2666,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=\/wp\/v2\/posts\/1382\/revisions\/2666"}],"wp:attachment":[{"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1382"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.menglanglang.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}