{"id":39,"date":"2021-11-04T20:28:51","date_gmt":"2021-11-04T12:28:51","guid":{"rendered":"https:\/\/www.helloalong.xyz\/?p=39"},"modified":"2021-11-09T20:18:38","modified_gmt":"2021-11-09T12:18:38","slug":"java-%e6%93%8d%e4%bd%9c-sql-server","status":"publish","type":"post","link":"https:\/\/www.alonya.cn\/?p=39","title":{"rendered":"Java \u64cd\u4f5c SQL Server"},"content":{"rendered":"<h2>\u5bfc\u5165sql server \u9a71\u52a8<\/h2>\n<p>1.\u9879\u76ee\u4e0b\u65b0\u5efalib\u6587\u4ef6\u5939<br \/>\n2.\u5fae\u8f6f\u5b98\u7f51\u4e0b\u8f7dsqljdbc_x.x.0.0_chs.zip\u5e76\u89e3\u538b<br \/>\n3.\u5c06\u5bf9\u5e94jar\u62f7\u8d1d\u5230lib\u6587\u4ef6\u5939<br \/>\n4.\u53f3\u952ejar\u6587\u4ef6\u6dfb\u52a0\u5230\u9879\u76ee\u6784\u9020\u8def\u5f84<\/p>\n<h2>Eclipse \u5feb\u901f\u751f\u6210\u6807\u51c6\u8f93\u51fa\u8bed\u53e5<\/h2>\n<pre><code class=\"language-java\">\/\/\u8f93\u5165\u4e0b\u6bb5\u77ed\u8bed\u5e76\u4f7f\u7528\u667a\u80fd\u63d0\u793a\u5feb\u6377\u952e:Alt+\/\nsysout<\/code><\/pre>\n<h2>\u4ee3\u7801<\/h2>\n<pre><code class=\"language-java\">import java.sql.*;\npublic class MyJdbc_01 {\n        \/\/\u56db\u5927\u91d1\u521a\n    static String driverClassName = &quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&quot;;\n    static String url = &quot;jdbc:sqlserver:\/\/10.130.1.11:1433;DatabaseName=test_data_yinyl&quot;;\n    static String user = &quot;sa&quot;;\n    static String password = &quot;****&quot;;\n    public static void main(String[] args) {\n        \/\/\u4e09\u5927\u5bf9\u8c61\n        Connection connection = null;\n        Statement statement = null;\n        ResultSet resultSet = null;\n        int affectRows = 0; \n        try {\n            Class.forName(driverClassName);\n        }\n        catch (ClassNotFoundException e) {\n            \/\/ TODO: handle exception\n            System.out.println(&quot;\u7c7b\u540d[&quot;+driverClassName+&quot;]\u672a\u627e\u5230&quot;);\n        }\n        try {\n            \/\/\u521b\u5efa\u4e00\u4e2a\u65b0\u6570\u636e\u5e93\u5bf9\u8c61\n            connection = DriverManager.getConnection(url, user,password);\n            statement = connection.createStatement();\n            \/\/\u65b0\u5efa\u4e00\u4e2a\u67e5\u8be2\u64cd\u4f5c\n            String sqlString = &quot;select * from t0 where id &lt; 8&quot;;\n            resultSet = statement.executeQuery(sqlString);\n            while (resultSet.next()) {\n                int tmp = resultSet.getInt(&quot;id&quot;);\n                System.out.println(tmp);    \/\/\u4f9d\u6b21\u6253\u5370\u67e5\u8be2\u7ed3\u679c\u4e2d\u7684id\u503c         \n            }\n            \/\/\u6267\u884c\u4e00\u6761\u63d2\u5165\u64cd\u4f5c\n\/\/          affectRows = statement.executeUpdate(&quot;insert into t0 values(&#039;t&#039;,&#039;t&#039;,default)&quot;);\n            \/\/\u6267\u884c\u4e00\u6761\u5220\u9664\u64cd\u4f5c\n            affectRows = statement.executeUpdate(&quot;delete from t0 where id &gt;12 and id &lt; 43&quot;);\n            \/\/\u6267\u884c\u4e00\u6761\u66f4\u65b0\u64cd\u4f5c\n\/\/          System.out.println(&quot;\u6b63\u5728\u5c1d\u8bd5\u5c06id\u4e3a3\u5904\u7684\u8bbe\u5907\u540d\u79f0\u66f4\u65b0\u4e3a\u8bbe\u59071&quot;);\n\/\/          affectRows = statement.executeUpdate(&quot;update t0 set dev = &#039;\u8bbe\u59071&#039; where id = 3&quot;);\n            \/\/\u6253\u5370\u53d7\u5f71\u54cd\u884c\u6570\n            System.out.println(affectRows+&quot;\u884c\u53d7\u5f71\u54cd&quot;);\n        } catch (SQLException e) { \n            \/\/ TODO: handle exception\n            e.printStackTrace();\n        }\n\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5bfc\u5165sql server \u9a71\u52a8 1.\u9879\u76ee\u4e0b\u65b0\u5efalib\u6587\u4ef6\u5939 2.\u5fae\u8f6f\u5b98\u7f51\u4e0b\u8f7dsqljdbc_x.x.0.0_c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,10],"tags":[],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-java","category-development"],"_links":{"self":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":4,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":46,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions\/46"}],"wp:attachment":[{"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.alonya.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}