Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- public class JDBCExample {
- public static void main(String[] args) {
- String filename = System.getProperty("user.home") + "/Desktop/filename.csv";
- try {
- FileWriter fw = new FileWriter(filename);
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/zurich", "root", "Sweth9!a");
- Statement myStmt = myConn.createStatement();
- ResultSet myRs = myStmt.executeQuery("select * from persondata ");
- while (myRs.next()) {
- fw.append(myRs.getString("name"));
- fw.append(',');
- fw.append(myRs.getString("lastname"));
- fw.append(',');
- fw.append(myRs.getString("productnumber"));
- fw.append('\n');
- }
- fw.flush();
- fw.close();
- myConn.close();
- System.out.println("CSV File is created successfully.");
- }
- catch (Exception exc) {
- exc.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement