Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. public static void insert(ArrayList<Double> list, String tableName, int id)
  2. throws FileNotFoundException, SQLException {
  3. // *********update
  4. // test if the given id is already in the table or not
  5. String retrievalSql = "SELECT `object` FROM `" + tableName
  6. + "` where `id` = " + id;
  7.  
  8. pst = conn.prepareStatement(retrievalSql);
  9.  
  10. ResultSet result = (ResultSet) pst.executeQuery();
  11. // the result set is not empty
  12. if (result.next() == true) {
  13. // return and don't try to insert
  14. System.out.println("Already exists");
  15. return;
  16. }
  17.  
  18. // ******************
  19.  
  20. String fileName = "list.bin";
  21. save2(list, fileName);
  22. String sql = "INSERT INTO `" + tableName
  23. + "` (`id`, `object`) VALUES (" + id + ",?)";
  24.  
  25. // System.out.println(sql);
  26.  
  27. // same the arrayList to the file
  28. save2(list, fileName);
  29.  
  30. // read the object from the file
  31. File file = new File(fileName);
  32. FileInputStream fin = new FileInputStream(file);
  33.  
  34. // STEP 4: Execute a query
  35. // System.out.println("Creating statement...");
  36. pst = conn.prepareStatement(sql);
  37. pst.setBinaryStream(1, fin, (int) file.length());
  38. pst.executeUpdate();
  39. // System.out.println("Insertion Done!");
  40. }
  41.  
  42.  
  43. public void dropTable(String tableName) throws SQLException {
  44. String query = "DROP table `" + tableName + "`";
  45. pst = conn.prepareStatement(query);
  46. System.out.println(query);
  47. if (pst.execute())
  48. System.out.println("Table droped successfully");
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement