{"id":1288,"date":"2018-04-22T15:15:47","date_gmt":"2018-04-22T06:15:47","guid":{"rendered":"http:\/\/www.code-magagine.com\/?p=1288"},"modified":"2018-04-23T00:46:27","modified_gmt":"2018-04-22T15:46:27","slug":"%e3%80%90java%e3%80%91pre","status":"publish","type":"post","link":"http:\/\/www.code-magagine.com\/?p=1288","title":{"rendered":"\u3010Java\u3011PreparedStatement\u306b\u3088\u308bINSERT\u6587\u306e\u5b9f\u884c"},"content":{"rendered":"<p>\u300cPreparedStatement\u300d\u306f\u3001\u300cStatement\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u300d\u306e\u30b5\u30d6\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306b\u306a\u308a\u3001\u6587\u5b57\u901a\u308a<strong>\u300c\u6e96\u5099\u3055\u308c\u305fSQL\u3092\u767a\u884c\u3059\u308b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u300d<\/strong>\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u6e96\u5099\u3055\u308c\u305fSQL\u3068\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u30d7\u30ea\u30b3\u30f3\u30d1\u30a4\u30eb\uff08\u65e2\u306b\u30b3\u30f3\u30d1\u30a4\u30eb\u6e08\u307f\uff09\u306eSQL\u306e\u3053\u3068<\/strong><\/span>\u3092\u6307\u3057\u307e\u3059\u3002<\/p>\n<h2>Statement\u3068\u306e\u9055\u3044\u306f\uff1f<\/h2>\n<h3>\u30e1\u30ea\u30c3\u30c8<\/h3>\n<h4>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u5411\u4e0a\u304c\u671f\u5f85\u3067\u304d\u308b\u3002<\/h4>\n<p><span style=\"color: #ff0000;\"><span style=\"color: #000000;\">\u4e0b\u8a18\u306e\u6027\u8cea\u306b\u3088\u308a\u3001<\/span><strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306e\u5411\u4e0a\u304c\u671f\u5f85<\/strong><\/span>\u3067\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u00a0\u65e2\u306b\u30b3\u30f3\u30d1\u30a4\u30eb\u3055\u308c\u305f\uff33\uff31\uff2c\u3092\u767a\u884c\u3059\u308b\u3053\u3068\u306b\u306a\u308b\u3002<\/li>\n<li>\u524d\u56de\u5229\u7528\u3057\u305f\u969b\u306e\u30ad\u30e3\u30c3\u30b7\u30e5\u3092\u518d\u5229\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u51e6\u7406\u30b9\u30d4\u30fc\u30c9\u304c\u4e0a\u304c\u308b\u3002<\/li>\n<\/ul>\n<p>\u305f\u3060\u3057\u3001Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5834\u5408\u306f\u3001<span style=\"color: #ff0000;\"><strong>\u30ea\u30af\u30a8\u30b9\u30c8\u306e\u5ea6\u306bConnection\u3092\u9589\u3058\u3066\u3057\u307e\u3044\u3001PreparedStatement\u3082\u7834\u68c4\u3057\u3066\u3057\u307e\u3044\u307e\u3059\u306e\u3067\u3001\u30ad\u30e3\u30c3\u30b7\u30e5\u304c\u30af\u30ea\u30a2\u3055\u308c\u3066\u3057\u307e\u3044\u3042\u307e\u308a\u5927\u304d\u306a\u6069\u6075\u3092\u53d7\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u305b\u3093\u3002<\/strong><span style=\"color: #000000;\">\u305f\u3060\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u30b5\u30fc\u30d0\u30fc\u306b\u3088\u3063\u3066\u306f\u3001Connection\u3092\u9589\u3058\u3066\u3082\u3001PreparedStatement\u3092\u9589\u3058\u306a\u3044\u3082\u306e\u3082\u3042\u308a\u307e\u3059\u3002<\/span><\/span><\/p>\n<h4>SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u306b\u5f37\u304f\u3001\u697d\u3002<\/h4>\n<p>SQL\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u57cb\u3081\u8fbc\u3080\u969b\u306b\u3001\u81ea\u52d5\u7684\u306b\u30a8\u30b9\u30b1\u30fc\u30d7\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u3001\u30d7\u30ed\u30b0\u30e9\u30de\u3067\u30a8\u30b9\u30b1\u30fc\u30d7\u51e6\u7406\u3092\u66f8\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<h3>\u30c7\u30e1\u30ea\u30c3\u30c8<\/h3>\n<h4>\u30e1\u30f3\u30c6\u304c\u3057\u305a\u3089\u3044\u3002<\/h4>\n<p>\u6570\u5024\u3092\u6307\u5b9a\u3057\u3066\u3001\u52d5\u7684\u306b\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u57cb\u3081\u8fbc\u3080\u306e\u3067\u3001\u52d5\u7684\u306b\uff33\uff31\uff2c\u3092\u4f5c\u6210\u3059\u308b\u5834\u5408\u7b49\u306f\u3001\u5f8c\u3067\u30e1\u30f3\u30c6\u304c\u3057\u305a\u3089\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n<h2>PreparedStatement\u3067INSERT\u6587\u3092\u767a\u884c\u3059\u308b\u30b5\u30f3\u30d7\u30eb<\/h2>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npackage oracletest;\r\n\r\nimport java.sql.Connection;\r\nimport java.sql.DriverManager;\r\nimport java.sql.PreparedStatement;\r\nimport java.sql.ResultSet;\r\n\r\npublic class OracleInsert {\r\n\r\n\tpublic static void main(String[] args) throws Exception {\r\n\r\n\t\tConnection con = null;\r\n\t\tPreparedStatement presmt = null;\r\n\t\tResultSet rs = null;\r\n\r\n\t\t\/\/\u30db\u30b9\u30c8\u540d\r\n\t\tString hostname = &quot;localhost&quot;;\r\n\t\t\/\/SID\r\n\t\tString sid = &quot;orcl&quot;;\r\n\t\t\/\/\u30e6\u30fc\u30b6\u30fc\u540d\r\n\t\tString username = &quot;orauser&quot;;\r\n\t\t\/\/\u30d1\u30b9\u30ef\u30fc\u30c9\r\n\t\tString passwd = &quot;orapass&quot;;\r\n\t\t\/\/SQL\r\n\t\tString sql = &quot;INSERT INTO USER_MASTER VALUES (?,?,?)&quot;;\r\n\r\n\r\n\t\ttry {\r\n\t\t    Class.forName(&quot;oracle.jdbc.driver.OracleDriver&quot;);\r\n\t\t    con = DriverManager.getConnection(\r\n\t\t    \t\t&quot;jdbc:oracle:thin:@&quot; + hostname + &quot;:1521:&quot; + sid,username,passwd);\r\n\r\n\t\t\tpresmt = con.prepareStatement(sql);\r\n\r\n\t\t\tpresmt.setString(1,&quot;0023&quot;);\r\n\t\t\tpresmt.setString(2,&quot;1002&quot;);\r\n\t\t\tpresmt.setString(3,&quot;\u7530\u4e2d\u592a\u90ce&quot;);\r\n\r\n\t\t\tpresmt.executeUpdate();\r\n\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\r\n\t\tpresmt.close();\r\n        con.close();\r\n    }\r\n\r\n}\r\n<\/pre>\n<h3>\u30dd\u30a4\u30f3\u30c8<\/h3>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nString sql = &quot;INSERT INTO USER_MASTER VALUES (?,?,?)&quot;;\r\npresmt = con.prepareStatement(sql);\r\npresmt.setString(1,&quot;0023&quot;);\r\npresmt.setString(2,&quot;1002&quot;);\r\npresmt.setString(3,&quot;\u7530\u4e2d\u592a\u90ce&quot;);\r\n<\/pre>\n<p>\u4e0a\u8a18\u306e\u300c?\u300d\u3068\u3044\u3046\u8a18\u8ff0\u306f\u3001<strong>\u300c\u30d1\u30e9\u30e1\u30fc\u30bf\u5909\u6570\u300d<\/strong>\u3068\u547c\u3073\u307e\u3059\u3002\u300c\u30d1\u30e9\u30e1\u30fc\u30bf\u5909\u6570\u300d\u306b\u5bfe\u3057\u3066\u3001?\u306e\u756a\u5730\u306b\u5024\u3092\u4ee3\u5165\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u306e\u969b\u306b\u81ea\u52d5\u306b\u5909\u6570\u306e\u30a8\u30b9\u30b1\u30fc\u30d7\u3082\u3057\u3066\u304f\u308c\u308b\u306e\u3067\u3001<span style=\"color: #ff0000;\"><strong>SQL\u30a4\u30f3\u30b8\u30a7\u30af\u30b7\u30e7\u30f3\u306b\u3082\u5f37\u304f\u306a\u308a\u307e\u3059\u3002<\/strong><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"\u300cPreparedStatement\u300d\u306f\u3001\u300cStatement\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u300d\u306e\u30b5\u30d6\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306b\u306a\u308a\u3001\u6587\u5b57\u901a\u308a\u300c\u6e96\u5099\u3055\u308c\u305fSQL\u3092\u767a\u884c\u3059\u308b\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u300d\u306b\u306a\u308a\u307e\u3059\u3002 \u6e96\u5099\u3055\u308c\u305fSQL\u3068\u306f\u3001\u30d7\u30ea\u30b3\u30f3\u30d1\u30a4\u30eb\uff08\u65e2\u306b [&hellip;]","protected":false},"author":1,"featured_media":1378,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,20],"tags":[],"_links":{"self":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/1288"}],"collection":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1288"}],"version-history":[{"count":6,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/1288\/revisions"}],"predecessor-version":[{"id":1294,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/posts\/1288\/revisions\/1294"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=\/wp\/v2\/media\/1378"}],"wp:attachment":[{"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1288"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.code-magagine.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}