Create_CSV.java
package com.chillyfacts.com; import java.io.File; import java.io.PrintWriter; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Create_CSV { public static void main(String[] args) { try { PrintWriter pw= new PrintWriter(new File("C:\\Users\\MIRITPC\\Desktop\\csv\\books_table1.csv")); StringBuilder sb=new StringBuilder(); Connection connection=null; DB_Connection obj_DB_Connection=new DB_Connection(); connection=obj_DB_Connection.getConnection(); ResultSet rs=null; String query="select * from books"; PreparedStatement ps=connection.prepareStatement(query); rs=ps.executeQuery(); while(rs.next()){ sb.append(rs.getString("book_sl_no")); sb.append(","); sb.append(rs.getString("book_title")); sb.append(","); sb.append(rs.getString("category_name")); sb.append(","); sb.append(rs.getString("author_name")); sb.append(","); sb.append(rs.getString("publisher_name")); sb.append("\r\n"); } pw.write(sb.toString()); pw.close(); System.out.println("finished"); } catch (Exception e) { // TODO: handle exception } } }
'. MySql database connection class
DB_Connection.java
package com.chillyfacts.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB_Connection {
public Connection getConnection(){
Connection connection=null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/books_db","root", "root");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
No comments:
Post a Comment