{"id":4027,"date":"2021-07-30T10:59:00","date_gmt":"2021-07-30T10:59:00","guid":{"rendered":"https:\/\/www.bearloga.space\/?page_id=4027"},"modified":"2021-07-30T10:59:00","modified_gmt":"2021-07-30T10:59:00","slug":"jdbc-potokovaya-peredacha-ascii-i-dvoichnyh-dannyh","status":"publish","type":"page","link":"https:\/\/www.bearloga.space\/en\/jdbc-potokovaya-peredacha-ascii-i-dvoichnyh-dannyh\/","title":{"rendered":"JDBC \u2014 \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u0430\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 ASCII \u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u041e\u0431\u044a\u0435\u043a\u0442 PreparedStatement \u0438\u043c\u0435\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432.&nbsp;\u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0446\u0435\u043b\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 CLOB \u0438 BLOB.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u043e\u0442\u043e\u043a\u043e\u0432\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u0434\u0430\u043d\u043d\u044b\u0445:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>setAsciiStream ():<\/strong>&nbsp;\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 ASCII.<\/li><li><strong>setCharacterStream ():<\/strong>&nbsp;\u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 UNICODE.<\/li><li><strong>setBinaryStream ():<\/strong>&nbsp;\u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>setAsciiStream ():<\/strong>&nbsp;\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 ASCII.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>setCharacterStream ():<\/strong>&nbsp;\u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 UNICODE.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>setBinaryStream ():<\/strong>\u00a0\u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u041c\u0435\u0442\u043e\u0434 setXXXStream () \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430, \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0444\u0430\u0439\u043b\u0430, \u043f\u043e\u043c\u0438\u043c\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430-\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044f.&nbsp;\u042d\u0442\u043e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0443, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043e \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u043e\u0442\u043e\u043a\u0430.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u043f\u0440\u0438\u043c\u0435\u0440<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c XML-\u0444\u0430\u0439\u043b XML_Data.xml \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.&nbsp;\u0412\u043e\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 XML \u2014<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?xml version=\"1.0\"?&gt;\n&lt;Employee&gt;\n&lt;id&gt;100&lt;\/id&gt;\n&lt;first&gt;Zara&lt;\/first&gt;\n&lt;last&gt;Ali&lt;\/last&gt;\n&lt;Salary&gt;10000&lt;\/Salary&gt;\n&lt;Dob&gt;18-08-1978&lt;\/Dob&gt;\n&lt;Employee&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0425\u0440\u0430\u043d\u0438\u0442\u0435 \u044d\u0442\u043e\u0442 XML-\u0444\u0430\u0439\u043b \u0432 \u0442\u043e\u043c \u0436\u0435 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435, \u0433\u0434\u0435 \u0432\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0435\u0441\u044c \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 XML_Data, \u0430 \u0437\u0430\u0442\u0435\u043c \u0444\u0430\u0439\u043b XML_Data.xml \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d \u0432 \u044d\u0442\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u0421\u043a\u043e\u043f\u0438\u0440\u0443\u0439\u0442\u0435 \u0438 \u0432\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0432 JDBCExample.java, \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0439\u0442\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ Import required packages\nimport java.sql.*;\nimport java.io.*;\nimport java.util.*;\n\npublic class JDBCExample {\n   \/\/ JDBC driver name and database URL\n   static final String JDBC_DRIVER = \"com.mysql.jdbc.Driver\";  \n   static final String DB_URL = \"jdbc:mysql:\/\/localhost\/EMP\";\n\n   \/\/  Database credentials\n   static final String USER = \"username\";\n   static final String PASS = \"password\";\n   \n   public static void main(String[] args) {\n   Connection conn = null;\n   PreparedStatement pstmt = null;\n   Statement stmt = null;\n   ResultSet rs = null;\n   try{\n      \/\/ Register JDBC driver\n      Class.forName(\"com.mysql.jdbc.Driver\");\n\n      \/\/ Open a connection\n      System.out.println(\"Connecting to database...\");\n      conn = DriverManager.getConnection(DB_URL,USER,PASS);\n\n      \/\/Create a Statement object and build table\n      stmt = conn.createStatement();\n      createXMLTable(stmt);\n\n      \/\/Open a FileInputStream\n      File f = new File(\"XML_Data.xml\");\n      long fileLength = f.length();\n      FileInputStream fis = new FileInputStream(f);\n\n      \/\/Create PreparedStatement and stream data\n      String SQL = \"INSERT INTO XML_Data VALUES (?,?)\";\n      pstmt = conn.prepareStatement(SQL);\n      pstmt.setInt(1,100);\n      pstmt.setAsciiStream(2,fis,(int)fileLength);\n      pstmt.execute();\n\n      \/\/Close input stream\n      fis.close();\n\n      \/\/ Do a query to get the row\n      SQL = \"SELECT Data FROM XML_Data WHERE id=100\";\n      rs = stmt.executeQuery (SQL);\n      \/\/ Get the first row\n      if (rs.next ()){\n         \/\/Retrieve data from input stream\n         InputStream xmlInputStream = rs.getAsciiStream (1);\n         int c;\n         ByteArrayOutputStream bos = new ByteArrayOutputStream();\n         while (( c = xmlInputStream.read ()) != -1)\n            bos.write(c);\n         \/\/Print results\n         System.out.println(bos.toString());\n      }\n      \/\/ Clean-up environment\n      rs.close();\n      stmt.close();\n      pstmt.close();\n      conn.close();\n   }catch(SQLException se){\n      \/\/Handle errors for JDBC\n      se.printStackTrace();\n   }catch(Exception e){\n      \/\/Handle errors for Class.forName\n      e.printStackTrace();\n   }finally{\n      \/\/finally block used to close resources\n      try{\n         if(stmt!=null)\n            stmt.close();\n      }catch(SQLException se2){\n      }\/\/ nothing we can do\n      try{\n         if(pstmt!=null)\n            pstmt.close();\n      }catch(SQLException se2){\n      }\/\/ nothing we can do\n      try{\n         if(conn!=null)\n            conn.close();\n      }catch(SQLException se){\n         se.printStackTrace();\n      }\/\/end finally try\n   }\/\/end try\n   System.out.println(\"Goodbye!\");\n}\/\/end main\n\npublic static void createXMLTable(Statement stmt) \n   throws SQLException{\n   System.out.println(\"Creating XML_Data table...\" );\n   \/\/Create SQL Statement\n   String streamingDataSql = \"CREATE TABLE XML_Data \" +\n                             \"(id INTEGER, Data LONG)\";\n   \/\/Drop table first if it exists.\n   try{\n      stmt.executeUpdate(\"DROP TABLE XML_Data\");\n   }catch(SQLException se){\n   }\/\/ do nothing\n   \/\/Build table.\n   stmt.executeUpdate(streamingDataSql);\n}\/\/end createXMLTable\n}\/\/end JDBCExample<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">C:\\&gt;javac JDBCExample.java\nC:\\&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u041e\u0431\u044a\u0435\u043a\u0442 PreparedStatement \u0438\u043c\u0435\u0435\u0442 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432.&nbsp;\u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0446\u0435\u043b\u044b\u0435 \u0444\u0430\u0439\u043b\u044b \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0431\u0430\u0437\u044b [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-4027","page","type-page","status-publish","hentry","category-jdbc"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages\/4027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/comments?post=4027"}],"version-history":[{"count":0,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/pages\/4027\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/media?parent=4027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/categories?post=4027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bearloga.space\/en\/wp-json\/wp\/v2\/tags?post=4027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}