Guest User

Untitled

a guest
Jul 29th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. Export MYSQL rows into several txt files
  2. try {
  3. Class.forName("com.mysql.jdbc.Driver");
  4. System.out.println("Opening connection");
  5. Connection con = DriverManager.getConnection(
  6. "jdbc:mysql://localhost/articles", "username", "password");
  7.  
  8. String query = "Select title,articletext from articles";
  9.  
  10. Statement stmt = con.createStatement();
  11. ResultSet rs = stmt.executeQuery(query);
  12.  
  13. while (rs.next()) {
  14. String title = rs.getString(1);
  15. String text = rs.getString(2);
  16.  
  17. try {
  18. FileWriter fstream = new FileWriter(title + ".txt");
  19.  
  20. BufferedWriter out = new BufferedWriter(fstream);
  21. out.write(text);
  22. out.close();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26.  
  27. }
  28.  
  29. System.out.println("Closing connection");
  30. con.close();
  31.  
  32. } catch (ClassNotFoundException e) {
  33. // TODO Auto-generated catch block
  34. e.printStackTrace();
  35. } catch (SQLException e) {
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }
Add Comment
Please, Sign In to add comment