
{"id":2366,"date":"2020-10-14T13:23:51","date_gmt":"2020-10-14T05:23:51","guid":{"rendered":"http:\/\/admin.evshou.com\/?p=2366"},"modified":"2020-10-15T15:41:21","modified_gmt":"2020-10-15T07:41:21","slug":"2020javaweb%e6%95%99%e7%a8%8b%e4%b9%8bjdbc%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2366","title":{"rendered":"2020Javaweb\u6559\u7a0b\u4e4bJDBC\u603b\u7ed3"},"content":{"rendered":"<h3>2020Javaweb\u6559\u7a0b\u4e4bJDBC\u603b\u7ed3<\/h3>\n<p><strong><span style=\"color: #ff0000;\">statement\u6267\u884cDDL<\/span><\/strong><\/p>\n<p><!--more--><\/p>\n<pre>public class TestJDBC {\r\n    public static void main(String[] args) {\r\n        Connection connection=null;\r\n        Statement statement=null;\r\n        try{\r\n            \/\/1.\u6ce8\u518c\u9a71\u52a8\r\n            Class.forName(\"com.mysql.jdbc.Driver\");\r\n            \/\/2.\u521b\u5efaConnection\u5bf9\u8c61\r\n            connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/demos\", \"root\", \"root\");\r\n            \/\/3.\u521b\u5efa\u6267\u884cSQL\u8bed\u53e5\u7684\u5bf9\u8c61\r\n            statement = connection.createStatement();\r\n            \/\/4.sql\u8bed\u53e5\r\n            boolean execute = statement.execute(\"create table stu(id int auto_increment primary key,sname varchar(20),sex varchar(2))\");\r\n            System.out.println(execute);\r\n        }catch (Exception e){}finally {\r\n            try{\r\n                if (statement != null) {\r\n                    statement.close();\r\n                }\r\n                if (connection != null) {\r\n                    connection.close();\r\n                }\r\n            }catch (Exception e){}\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong><span style=\"color: #ff0000;\">statement\u6267\u884cDML<\/span><\/strong><\/p>\n<pre>public class TestJDBC {\r\n    public static void main(String[] args) {\r\n        Connection connection=null;\r\n        Statement statement=null;\r\n        try{\r\n            \/\/1.\u6ce8\u518c\u9a71\u52a8\r\n            Class.forName(\"com.mysql.jdbc.Driver\");\r\n            \/\/2.\u521b\u5efaConnection\u5bf9\u8c61\r\n            connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/demos\", \"root\", \"root\");\r\n            \/\/3.\u521b\u5efa\u6267\u884cSQL\u8bed\u53e5\u7684\u5bf9\u8c61\r\n            statement = connection.createStatement();\r\n            \/\/4.sql\u8bed\u53e5\r\n            \/\/\u65b0\u589e\u6570\u636e\r\n            \/\/int i = statement.executeUpdate(\"insert into gamer(gname)values ('admin')\");\r\n            \/\/\u4fee\u6539\u6570\u636e\r\n            \/\/int i = statement.executeUpdate(\"update gamer set gname='admin888' where id=2\");\r\n            \/\/\u5220\u9664\u6570\u636e\r\n            int i = statement.executeUpdate(\"delete from gamer where id=2\");\r\n            if (i &gt; 0) {\r\n                System.out.println(\"OK\");\r\n            }else{ System.out.println(\"ERR\");}\r\n        }catch (Exception e){}finally {\r\n            try{\r\n                if (statement != null) {\r\n                    statement.close();\r\n                }\r\n                if (connection != null) {\r\n                    connection.close();\r\n                }\r\n            }catch (Exception e){}\r\n        }\r\n    }\r\n}<\/pre>\n<p><strong><span style=\"color: #ff0000;\">statement\u6267\u884cDQL<\/span><\/strong><\/p>\n<pre>public class TestJDBC {\r\n    public static void main(String[] args) {\r\n        Connection connection=null;\r\n        Statement statement=null;\r\n        ResultSet resultSet=null;\r\n        try{\r\n            \/\/1.\u6ce8\u518c\u9a71\u52a8\r\n            Class.forName(\"com.mysql.jdbc.Driver\");\r\n            \/\/2.\u521b\u5efaConnection\u5bf9\u8c61\r\n            connection = DriverManager.getConnection(\"jdbc:mysql:\/\/localhost:3306\/demos\", \"root\", \"root\");\r\n            \/\/3.\u521b\u5efa\u6267\u884cSQL\u8bed\u53e5\u7684\u5bf9\u8c61\r\n            statement = connection.createStatement();\r\n            \/\/4.sql\u8bed\u53e5\r\n            String sql=\"select id,sname,sex from stu\";\r\n            \/\/5.\u6267\u884cSQL\u8bed\u53e5\r\n            resultSet = statement.executeQuery(sql);\r\n            \/\/6.\u904d\u5386\r\n            while (resultSet.next()) {\r\n                int id = resultSet.getInt(1);\r\n                String sname = resultSet.getString(\"sname\");\r\n                String sex = resultSet.getString(\"sex\");\r\n                System.out.println(id+\":\"+sname+\":\"+sex);\r\n            }\r\n        }catch (Exception e){}finally {\r\n            try{\r\n                if (resultSet != null) {\r\n                    resultSet.close();\r\n                }\r\n                if (statement != null) {\r\n                    statement.close();\r\n                }\r\n                if (connection != null) {\r\n                    connection.close();\r\n                }\r\n            }catch (Exception e){}\r\n        }\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>2020Javaweb\u6559\u7a0b\u4e4bJDBC\u603b\u7ed3 statement\u6267\u884cDDL<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2366","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2366"}],"version-history":[{"count":1,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2366\/revisions"}],"predecessor-version":[{"id":2367,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2366\/revisions\/2367"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}