Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!

Varmeer Kumar

Greenhorn
+ Follow
since Nov 16, 2014
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Varmeer Kumar

Sama, that's a great way to do it! You can also read the file with a scanner and import the information in that way!
Here is the code that I've used:

package Example_Import;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.io.*;
import java.lang.String;
import java.util.Scanner;

public class Example {

//import mysql-connector-java-5.1.32.jar
//http://www.tutorialspoint.com/jdbc/jdbc-create-tables.htm
private static String JDBC_URL = "jdbc:mysql://localhost:3306/sakila";
private static String USER = "root";
private static String PASSWORD = "2004";

public static void main(String[] args) throws IOException {

Connection myConnection = null;
Statement statement = null;
PreparedStatement preparedStatement = null;

try{
//CONNECTING TO THE DATABASE
myConnection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);


System.out.println("Connected to database"); //CONNECTED

//CREATING TABLE IN THE DATABASE
System.out.println("Creating table in given database...");
statement = myConnection.createStatement();

String sql = "CREATE TABLE REGISTRATION " +
"(id INTEGER not NULL, " +
" last VARCHAR(25), " +
" first VARCHAR(25), " +
" balance FLOAT, " +
" Credit FLOAT," +
" Date DATE," +
" Rating CHAR," +
" PRIMARY KEY ( id ))";

statement.executeUpdate(sql);
System.out.println("Created table in given database...");
statement.close(); //closing statement


//CREATING A PREPARED STATEMENT
String insertTableSQL = "INSERT INTO REGISTRATION"
+ "(id, last, first, balance, Credit, Date, Rating) VALUES"
+ "(?,?,?,?, ?, ?, ?)";


preparedStatement = myConnection.prepareStatement(insertTableSQL);

//RETRIEVING INFORMATION FROM CSV FILE

//opening a file input stream
BufferedReader reader = new BufferedReader(new FileReader("SAMPLE.csv"));

String line = null; //line read from csv
Scanner scanner = null; //scanned line

SimpleDateFormat date = new SimpleDateFormat("mm/DD/yyyy");

reader.readLine(); //omits the first line

//READING FILE LINE BY LINE AND UPLOADING INFORMATION TO DATABASE
while((line = reader.readLine()) != null){
scanner = new Scanner(line);
scanner.useDelimiter(",");
while(scanner.hasNext()){
preparedStatement.setInt(1,Integer.parseInt(scanner.next()));
preparedStatement.setString(2, scanner.next());
preparedStatement.setString(3, scanner.next());
preparedStatement.setFloat(4, Float.parseFloat(scanner.next()));
preparedStatement.setFloat(5, Float.parseFloat(scanner.next()));
try {
java.util.Date d;
d = date.parse(scanner.next());
preparedStatement.setDate(6, new java.sql.Date(d.getTime()));
} catch (ParseException e) {
e.printStackTrace();
}
preparedStatement.setString(7, scanner.next());
}
preparedStatement.executeUpdate();
}

preparedStatement.close();
System.out.println("Data imported");

reader.close(); //closing CSV reader

} catch (SQLException e){
e.printStackTrace();
}finally{//CLOSING CONNECTION
try{
if(statement!=null)
myConnection.close();
}catch(SQLException se){
}// do nothing
try{
if(myConnection!=null)
myConnection.close();
}catch(SQLException se){
se.printStackTrace();
}
System.out.println("Connection closed");
}
}
}