Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.FileWriter;
  6.  
  7. public class JDBCExample {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. String filename = System.getProperty("user.home") + "/Desktop/filename.csv";
  12.  
  13. try {
  14.  
  15. FileWriter fw = new FileWriter(filename);
  16. Class.forName("com.mysql.jdbc.Driver").newInstance();
  17. Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zurich", "root", "Sweth9!a");
  18.  
  19. Statement myStmt = myConn.createStatement();
  20.  
  21. ResultSet myRs = myStmt.executeQuery("select * from persondata ");
  22.  
  23. while (myRs.next()) {
  24.  
  25. fw.append(myRs.getString("name"));
  26. fw.append(',');
  27. fw.append(myRs.getString("lastname"));
  28. fw.append(',');
  29. fw.append(myRs.getString("productnumber"));
  30. fw.append('\n');
  31.  
  32. }
  33.  
  34. fw.flush();
  35. fw.close();
  36. myConn.close();
  37. System.out.println("CSV File is created successfully.");
  38. }
  39.  
  40. catch (Exception exc) {
  41. exc.printStackTrace();
  42.  
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement