Advertisement
Guest User

Dec14JDBC01.java

a guest
Dec 14th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.CharArrayWriter;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.io.OutputStreamWriter;
  10. import java.io.Reader;
  11. import java.sql.CallableStatement;
  12. import java.sql.Clob;
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.PreparedStatement;
  16. import java.sql.ResultSet;
  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19.  
  20. import javax.sql.rowset.serial.SerialBlob;
  21. import javax.sql.rowset.serial.SerialClob;
  22. import javax.sql.rowset.serial.SerialException;
  23.  
  24. public class JDBC01 {
  25.  
  26. public static void main(String[] args) {
  27. String url = "jdbc:mysql://127.0.0.1:3306/jdbcDB" + "?user=root&password=password"
  28. + "&useSSL=true&useUnicode=yes" + "&characterEncoding=UTF-8";
  29. String sql2 = "INSERT INTO Company "
  30. + " (id, name, capital, amount, createDate, image, text)"
  31. + " values(null, ?, ?, ?, ?, ?, ?)"; // placeholder, 佔位符號
  32.  
  33. String sql3 = "SELECT id, image, text FROM Company";
  34.  
  35. try {
  36. Class.forName("com.mysql.jdbc.Driver").newInstance();
  37. } catch (Exception e1) {
  38. e1.printStackTrace();
  39. }
  40. try (
  41.  
  42. Connection con = DriverManager.getConnection(url);
  43. // Connection con = dataSource.getConnection();
  44. Statement stmt = con.createStatement();
  45. PreparedStatement pstmt = con.prepareStatement(sql2);
  46. PreparedStatement pstmt3 = con.prepareStatement(sql3);
  47. CallableStatement pstmt4 = con.prepareCall(sql3);) {
  48. // Class.forName("com.mysql.jdbc.Driver");
  49. String sql = "drop table if exists Company ";
  50. int n = stmt.executeUpdate(sql);
  51. System.out.println("n(0)=" + n);
  52. sql = " Create Table Company (" +
  53. " id int auto_increment primary key," +
  54. " name varchar(50), " +
  55. " capital int(30), " +
  56. " amount numeric(15, 2), " +
  57. " image longblob, " +
  58. " text longtext, " +
  59. " createDate date, " +
  60. " setupDateTime datetime, " +
  61. " obj blob " + ")";
  62.  
  63. n = stmt.executeUpdate(sql);
  64. String insertSQL = "INSERT INTO Company " + " (id, name, capital, amount, createDate)"
  65. + " values(null, '快樂谷有限公司', 2147483647, " + " 215.95, '2016-12-10') ";
  66. // System.out.println(insertSQL);
  67. n = stmt.executeUpdate(insertSQL);
  68.  
  69.  
  70. // 使用PreparedStatement物件
  71. pstmt.setString(1, "逍遙遊有限公司");
  72. pstmt.setInt(2, 2147483646);
  73. pstmt.setDouble(3, 315.95);
  74. pstmt.setDate(4, java.sql.Date.valueOf("1950-12-20"));
  75. File f = new File("D:\\Java004_Shared\\_MySQL\\penelope_cruz_64.jpg");
  76. int length = (int) f.length();
  77. FileInputStream fis = new FileInputStream(f);
  78. pstmt.setBinaryStream(5, fis, length);
  79. Clob clob = new SerialClob("測試資料".toCharArray());
  80. pstmt.setClob(6, clob);
  81. n = pstmt.executeUpdate();
  82.  
  83. pstmt.setString(1, "相逢假期有限公司");
  84. pstmt.setInt(2, -2147483646);
  85. pstmt.setDouble(3, 123.95);
  86. pstmt.setDate(4, java.sql.Date.valueOf("1980-11-20"));
  87. File f1 = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg");
  88. int length1 = (int) f1.length();
  89. byte[] ba = new byte[length1];
  90. FileInputStream fis1 = new FileInputStream(f1);
  91. fis1.read(ba);
  92. SerialBlob sb = new SerialBlob(ba);
  93. pstmt.setBlob(5, sb);
  94. Clob clob2 = getClobFromFile("D:\\java004\\JDBC\\ClobData1.txt");
  95. pstmt.setClob(6, clob2);
  96.  
  97. n = pstmt.executeUpdate();
  98.  
  99. pstmt.setString(1, "綠大地有限公司");
  100. pstmt.setInt(2, 1000646);
  101. pstmt.setDouble(3, 2255.95);
  102. pstmt.setDate(4, java.sql.Date.valueOf("1980-11-20"));
  103. File f2 = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Jellyfish.jpg");
  104. int length2 = (int) f2.length();
  105. byte[] ba2 = new byte[length2];
  106. FileInputStream fis2 = new FileInputStream(f2);
  107. fis2.read(ba2);
  108. pstmt.setBytes(5, ba2);
  109. Clob clob3 = getClobFromBigInteger();
  110.  
  111. pstmt.setClob(6, clob3);
  112. n = pstmt.executeUpdate();
  113.  
  114. // pstmt3.setInt(1, 2);
  115. ResultSet rs = pstmt3.executeQuery();
  116. int count = 0;
  117. while (rs.next()) {
  118. int id = rs.getInt(1);
  119. // --------------- Case1 -----------------
  120. // try (
  121. // InputStream is = rs.getBinaryStream(2);
  122. //
  123. // FileOutputStream fos = new FileOutputStream
  124. // ("d:\\images" + count + ".jpg");
  125. // ) {
  126. // if (is == null) {
  127. // continue;
  128. // }
  129. // byte[] b = new byte[8192];
  130. // int len = 0 ;
  131. // while ((len=is.read(b))!= -1){
  132. // fos.write(b, 0, len); // AOL
  133. // }
  134. // }
  135. // --------------- Case2 -----------------
  136. byte[] b2 = rs.getBytes(2);
  137. if (b2 == null) {
  138. continue;
  139. }
  140. try (
  141. FileOutputStream fos = new FileOutputStream
  142. ("d:\\imagesA" + count + ".jpg");
  143. )
  144. {
  145. fos.write(b2);
  146. }
  147. // // --------------- Case3 -----------------
  148. // Blob bb = rs.getBlob(2);
  149. // if (bb == null) {
  150. // continue;
  151. // }
  152. // try (
  153. // InputStream is = bb.getBinaryStream();
  154. // FileOutputStream fos = new FileOutputStream("d:\\imagesB" + count + ".jpg");) {
  155. // byte[] b3 = new byte[8192];
  156. // int len = 0 ;
  157. // while ((len=is.read(b3))!= -1){
  158. // fos.write(b3, 0, len);
  159. // }
  160. // }
  161. // 取出Clob資料
  162. Clob clob9 = rs.getClob(3);
  163. System.out.println("id=" + id + " " + clob9.length());
  164. Reader reader = clob9.getCharacterStream();
  165. String filename = "data" + id + ".txt";
  166. try (
  167. FileOutputStream out =
  168. new FileOutputStream("D:\\Java004\\jdbc\\"+ filename);
  169. OutputStreamWriter osw =
  170. new OutputStreamWriter(out, "BIG5");
  171. ) {
  172. char[] ca = new char[8192];
  173. int len = 0;
  174. while ((len = reader.read(ca)) != -1) {
  175. osw.write(ca, 0, len);
  176. }
  177. }
  178.  
  179. count++;
  180. }
  181.  
  182. System.out.println("n(2)=" + n);
  183. } catch (SQLException e) {
  184. e.printStackTrace();
  185. } catch (IOException e) {
  186. e.printStackTrace();
  187. }
  188.  
  189. }
  190.  
  191. private static Clob getClobFromBigInteger() {
  192. try (
  193. FileInputStream fis = new FileInputStream("D:\\factorial.txt");
  194. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  195. ) {
  196. byte[] b3 = new byte[8192];
  197. int len = 0;
  198. while ((len = fis.read(b3)) != -1) {
  199. baos.write(b3, 0, len);
  200. }
  201. byte[] ba = baos.toByteArray();
  202. String s = new String(ba, "UTF-8");
  203. Clob clob = new SerialClob(s.toCharArray());
  204. return clob;
  205. } catch (Exception ex) {
  206. ex.printStackTrace();
  207. }
  208. return null;
  209. }
  210.  
  211. private static Clob getClobFromFile(String filename) {
  212. try (
  213. FileReader fr = new FileReader(filename);
  214. BufferedReader br = new BufferedReader(fr);
  215. CharArrayWriter caw = new CharArrayWriter();
  216. ) {
  217. String line = "";
  218. while ((line=br.readLine())!= null){
  219. caw.write(line.toCharArray());
  220. caw.write('\r');
  221. caw.write('\n');
  222. }
  223. char[] ca = caw.toCharArray();
  224. SerialClob clob = new SerialClob(ca);
  225. return clob;
  226. } catch(IOException ex){
  227. ex.printStackTrace();
  228. } catch (SerialException e) {
  229. e.printStackTrace();
  230. } catch (SQLException e) {
  231. e.printStackTrace();
  232. }
  233. return null;
  234. }
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement