\u4e09\u3001\u901a\u8fc7\u4e00\u4e2a\u6848\u4f8b\u4e86\u89e3Statement\u63a5\u53e3\u67e5\u8be2\u6570\u636e\u7684\u7528\u6cd5<\/strong><\/div>\n\u5728\u4e0a\u9762\u4ecb\u7ecd\u4e86Statement\u63a5\u53e3\u67e5\u8be2\u6570\u636e\u7684\u5177\u4f53\u6b65\u9aa4\uff0c\u63a5\u4e0b\u6765\uff0c\u5c0f\u7f16\u5e26\u7740\u5927\u5bb6\u4e00\u8d77\u6765\u4e86\u89e3Statement\u63a5\u53e3\u67e5\u8be2\u6570\u636e\u7684\u7528\u6cd5\uff0cstudent\u8868\u4e2d\u7684\u6570\u636e\u548c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
student\u8868\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n
<\/p>\n
\u4ee3\u7801\uff1a<\/p>\n
import java.sql.Connection; \r\nimport java.sql.DriverManager; \r\nimport java.sql.ResultSet; \r\nimport java.sql.SQLException; \r\nimport java.sql.Statement; \r\nimport java.util.Scanner; \r\n \r\npublic class Example30 { \r\n \r\n public static void main(String[] args) { \r\n System.out.println(\"\u8bf7\u8f93\u5165\u4f60\u8981\u67e5\u8be2\u7684ID:\"); \r\n Scanner sc=new Scanner(System.in); \r\n String input=sc.next(); \r\n String driver=\"com.mysql.jdbc.Driver\"; \r\n try { \r\n \/\/\u52a0\u8f7d\u9a71\u52a8 \r\n Class.forName(driver); \r\n \/\/\u6570\u636e\u5e93\u5730\u5740\uff0c\u672c\u673a\u3001\u7aef\u53e3\u53f73306\u3001\u6570\u636e\u5e93\u540d\u4e3atest \r\n String url=\"jdbc:mysql:\/\/localhost:3306\/test\"; \r\n \/\/\u7528\u6237\u540d \r\n String user=\"root\"; \r\n \/\/\u5bc6\u7801 \r\n String pwd=\"168168\"; \r\n \/\/\u8fde\u63a5\u6570\u636e\u5e93 \r\n Connection conn=DriverManager.getConnection(url,user,pwd); \r\n \/\/\u521b\u5efaStatement\u5bf9\u8c61 \r\n Statement stmt=conn.createStatement(); \r\n String sql=\"select * from student where id='\"+input+\"'\"; \r\n \/\/\u6267\u884cSQL\u8bed\u53e5 \r\n ResultSet rs=stmt.executeQuery(sql); \r\n \/\/\u6839\u636e\u7528\u6237\u8f93\u5165\u7684ID\u503c\u83b7\u53d6\u6570\u636e \r\n if(rs.next()){ \r\n System.out.println(\"id:\"+rs.getString(\"id\")+\" name:\"+rs.getString(\"name\")+\" age:\"+rs.getInt(\"age\")); \r\n }else{ \r\n System.out.println(\"\u4f60\u8f93\u5165\u7684ID\u4e0d\u5b58\u5728\uff01\"); \r\n } \r\n } catch (ClassNotFoundException e) { \r\n \/\/ TODO Auto-generated catch block \r\n e.printStackTrace(); \r\n } catch (SQLException e) { \r\n \/\/ TODO Auto-generated catch block \r\n e.printStackTrace(); \r\n } \r\n } \r\n} <\/pre>\n\u6548\u679c\u56fe\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
<\/p>\n
\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u662f\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\uff0c\u4e4b\u540e\u4f7f\u7528getConnection()\u65b9\u6cd5\u6765\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u521b\u5efaStatement\u5bf9\u8c61\uff0c\u8c03\u7528Connection\u5bf9\u8c61\u7684createStatement()\u65b9\u6cd5\u521b\u5efa\u8fd9\u4e2aMySQL\u8bed\u53e5\u5bf9\u8c61\uff0c\u5728\u8fd9\u4e2a\u5bf9\u8c61\u8c03\u7528executeQuery()\u65b9\u6cd5\u6765\u5904\u7406\u67e5\u8be2\u7684\u7ed3\u679c\u3002<\/p>\n
\u56db\u3001Statement\u63a5\u53e3\u6dfb\u52a0\u6570\u636e\u5b9e\u73b0\u6b65\u9aa4<\/strong><\/div>\n\u9996\u5148\u5bfc\u5165\u62d3\u5c55\u5305\u201cmysql-connector-java-5.1.7-bin.jar\u201d\uff0c\u5728Ecilpse\u7f16\u8f91\u8f6f\u4ef6\u7684\u5f53\u524d\u9879\u76ee\u53f3\u952e\u9009\u62e9\u201cBulid Path\u201d\uff0c\u518d\u9009\u62e9\u201cConfigure Build Path...\u201d\uff0c\u9009\u62e9Libraies\uff0c\u5728\u53f3\u8fb9\u6709\u4e2a\u201cAdd External JARs...\u201d\u6309\u94ae\u628a\u8fd9\u4e2a\u62d3\u5c55\u5305\u52a0\u8fdb\u6765\uff0c\u7136\u540e\u70b9\u51fb\u201cOK\u201d\u3002\u5177\u4f53\u64cd\u4f5c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/ol>\n<\/p>\n
<\/p>\n
\u4f7f\u7528Class.forName()\u65b9\u6cd5\u6765\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u3002<\/ol>\n\u6210\u529f\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\u540e\uff0cClass.forName()\u65b9\u6cd5\u5411DriverManager\u6ce8\u518c\u81ea\u5df1\uff0c\u63a5\u7740\u4f7f\u7528getConnection()\u65b9\u6cd5\u548c\u6570\u636e\u5e93\u8fdb\u884c\u8fde\u63a5\uff0c\u8fd4\u56de\u4e00\u4e2aConnection\u5bf9\u8c61\u3002<\/ol>\n\u4f7f\u7528Connection\u5bf9\u8c61\u7684createStatement()\u65b9\u6cd5\u521b\u5efa\u4e00\u4e2aStatement\u5bf9\u8c61\u3002<\/ol>\n\u4f7f\u7528Statement\u5bf9\u8c61\u8c03\u7528executeUpdate()\u65b9\u6cd5\u67e5\u8be2\u6570\u636e\u5e93\u8868\uff0c\u628a\u67e5\u8be2\u7684\u7ed3\u679c\u5b58\u50a8\u5728\u4e00\u4e2aResultSet\u5bf9\u8c61\u3002<\/ol>\n\u4f7f\u7528ResultSet\u5bf9\u8c61\u7684next()\u65b9\u6cd5\uff0c\u83b7\u53d6\u8868\u4e2d\u7684\u6570\u636e\u3002<\/ol>\n\u4e94\u3001\u901a\u8fc7\u4e00\u4e2a\u6848\u4f8b\u4e86\u89e3Statement\u63a5\u53e3\u6dfb\u52a0\u6570\u636e\u7684\u7528\u6cd5<\/strong><\/div>\n\u5728\u4e0a\u9762\u4ecb\u7ecd\u4e86Statement\u63a5\u53e3\u6dfb\u52a0\u6570\u636e\u7684\u5b9e\u73b0\u6b65\u9aa4\uff0c\u63a5\u4e0b\u6765\uff0c\u5c0f\u7f16\u5e26\u7740\u5927\u5bb6\u4e00\u8d77\u6765\u4e86\u89e3Statement\u63a5\u53e3\u6dfb\u52a0\u6570\u636e\u7684\u7528\u6cd5\uff0c\u4ee3\u7801\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
import java.sql.Connection; \r\nimport java.sql.DriverManager; \r\nimport java.sql.ResultSet; \r\nimport java.sql.SQLException; \r\nimport java.sql.Statement; \r\nimport java.util.Scanner; \r\n \r\npublic class Example31 { \r\n \r\n public static void main(String[] args) { \r\n Scanner sc=new Scanner(System.in); \r\n System.out.println(\"\u8bf7\u8f93\u5165\u4f60\u8981\u6dfb\u52a0\u7684ID:\"); \r\n String id=sc.next(); \r\n System.out.println(\"\u8bf7\u8f93\u5165\u4f60\u8981\u6dfb\u52a0\u7684Name:\"); \r\n String name=sc.next(); \r\n System.out.println(\"\u8bf7\u8f93\u5165\u4f60\u8981\u6dfb\u52a0\u7684Age:\"); \r\n int age=sc.nextInt(); \r\n String driver=\"com.mysql.jdbc.Driver\"; \r\n try { \r\n \/\/\u52a0\u8f7d\u9a71\u52a8 \r\n Class.forName(driver); \r\n \/\/\u6570\u636e\u5e93\u5730\u5740\uff0c\u672c\u673a\u3001\u7aef\u53e3\u53f73306\u3001\u6570\u636e\u5e93\u540d\u4e3atest \r\n String url=\"jdbc:mysql:\/\/localhost:3306\/test\"; \r\n \/\/\u7528\u6237\u540d \r\n String user=\"root\"; \r\n \/\/\u5bc6\u7801 \r\n String pwd=\"168168\"; \r\n \/\/\u8fde\u63a5\u6570\u636e\u5e93 \r\n Connection conn=DriverManager.getConnection(url,user,pwd); \r\n \/\/\u521b\u5efaStatement\u5bf9\u8c61 \r\n Statement stmt=conn.createStatement(); \r\n String sql=\"insert into student values('\"+id+\"','\"+name+\"',\"+age+\")\"; \r\n \/\/\u6267\u884cSQL\u8bed\u53e5 \r\n stmt.executeUpdate(sql); \r\n sql=\"select * from student where id='\"+id+\"'\"; \r\n \/\/\u6267\u884cSQL\u8bed\u53e5 \r\n ResultSet rs=stmt.executeQuery(sql); \r\n \/\/\u6839\u636eID\u503c\u83b7\u53d6\u6570\u636e \r\n if(rs.next()){ \r\n System.out.println(\"id:\"+rs.getString(\"id\")+\" name:\"+rs.getString(\"name\")+\" age:\"+rs.getInt(\"age\")); \r\n } \r\n } catch (ClassNotFoundException e) { \r\n \/\/ TODO Auto-generated catch block \r\n e.printStackTrace(); \r\n } catch (SQLException e) { \r\n \/\/ TODO Auto-generated catch block \r\n e.printStackTrace(); \r\n } \r\n } \r\n} <\/pre>\n\u6548\u679c\u56fe\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n
<\/p>\n
\u8868\u4e2d\u7684\u6570\u636e\uff1a<\/p>\n
<\/p>\n
\u5728\u4e0a\u9762\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u662f\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\uff0c\u4e4b\u540e\u4f7f\u7528getConnection()\u65b9\u6cd5\u6765\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u521b\u5efaStatement\u5bf9\u8c61\uff0c\u8c03\u7528Connection\u5bf9\u8c61\u7684createStatement()\u65b9\u6cd5\u521b\u5efa\u8fd9\u4e2aMySQL\u8bed\u53e5\u5bf9\u8c61\uff0c\u5728\u8fd9\u4e2a\u5bf9\u8c61\u8c03\u7528executeUpdate\u65b9\u6cd5\u6765\u5904\u7406\u3002<\/p>\n
\u516d\u3001\u603b\u7ed3<\/strong><\/div>\n\u672c\u6587\u4ecb\u7ecd\u4e86Statement\u63a5\u53e3\u5b9e\u73b0\u67e5\u8be2\u6570\u636e\u3001\u6dfb\u52a0\u6570\u636e\u3002<\/p>\n
\u5728JDBC\u7684\u57fa\u672c\u5e94\u7528\u4e2d\uff0c\u4ecb\u7ecd\u4e86\u4f7f\u7528Statement\u63a5\u53e3\u67e5\u8be2\u548c\u6dfb\u52a0\u6570\u636e\u7684\u6b65\u9aa4\u3002\u91cd\u70b9\u5728\u4e8e\u4f7f\u7528getConnection()\u65b9\u6cd5\u6765\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u521b\u5efaStatement\u5bf9\u8c61\uff0c\u8c03\u7528Connection\u5bf9\u8c61\u7684createStatement()\u65b9\u6cd5\u521b\u5efa\u8fd9\u4e2aMySQL\u8bed\u53e5\u5bf9\u8c61\u3002<\/p>\n
\u9488\u5bf9\u63a5\u53e3\u67e5\u8be2\u6570\u636e\uff0c\u5728\u8fd9\u4e2a\u5bf9\u8c61\u8c03\u7528executeQuery()\u65b9\u6cd5\u6765\u5904\u7406\u67e5\u8be2\u7684\u7ed3\u679c;\u9488\u5bf9\u63a5\u53e3\u6dfb\u52a0\u6570\u636e\uff0c\u5728\u8fd9\u4e2a\u5bf9\u8c61\u8c03\u7528executeUpdate\u65b9\u6cd5\u6765\u5904\u7406\u3002\u5e76\u901a\u8fc7\u4e00\u4e2a\u5177\u4f53\u7684\u6848\u4f8b\u6765\u5e2e\u52a9\u5927\u5bb6\u4e86\u89e3\u5b83\u7684\u7528\u6cd5\u3002<\/p>\n
\u5e0c\u671b\u5927\u5bb6\u901a\u8fc7\u672c\u6587\u7684\u5b66\u4e60\uff0c\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9!<\/p>\n","protected":false},"excerpt":{"rendered":"
\u5728JDBC\u6280\u672f\u4e2d\uff0c\u4e0d\u540c\u7684\u6570\u636e\u5e93\u9700\u8981\u4e0d\u540c\u7684\u9a71\u52a8\u7a0b\u5e8f\uff0c\u5148\u52a0\u8f7d\u9a71\u52a8\u7a0b\u5e8f\uff0c\u63a5\u7740\u6570\u636e\u5e93\u7684\u8fde\u63a5\u540e\uff0c\u518d\u4f7f\u7528SQL\u8bed\u53e5\u6765\u6267\u884c\u6570 […]<\/p>\n","protected":false},"author":1920,"featured_media":121883,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-218978","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218978","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=218978"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218978\/revisions"}],"predecessor-version":[{"id":219192,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/218978\/revisions\/219192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/121883"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=218978"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=218978"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=218978"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}