import java.io.*;
import java.sql.*;
import au.com.bytecode.opencsv.*;
Boolean ok = false;
String zeile;
String separator;
int count = 0;
try
{
BufferedReader CSVFile = new BufferedReader(new FileReader("C:\\Documents and Settings\\Paul\\Desktop\\Script\\test.csv"));
zeile = CSVFile.readLine();
if(zeile != null)
{
System.out.println("......... Loading file ok!");
if(zeile.contains(";"))
{
System.out.println("......... CSV-File seperated by using \";\"");
ok = true;
separator = ";";
}
else if(zeile.contains(","))
{
System.out.println("......... CSV-File seperated by using \",\"");
ok = true;
separator = ",";
}
else
{
System.out.println("......... Can't find separate entry, maybe this is no CSV-File or wrong CSV-File-Format!");
}
}
if(ok == true)
{
CSVReader Datei = new CSVReader(new FileReader("C:\\Documents and Settings\\Paul\\Desktop\\Script\\test.csv"));
String [] daten;
while ((daten = Datei.readNext()) != null)
{
count++;
//SHOWS THE CSV CONTENT
System.out.println(
daten[0] + "\t" +
daten[1] + "\t" +
daten[2] + "\t" +
daten[3] + "\t" +
daten[4] + "\t" +
daten[5] + "\t"
);
//SHOWS THE CSV CONTENT
//SQL CONNECTION
Connection conn = null;
String treiber = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/csv_test";
String userName = "root";
String password = "root";
//SQL CONNECTION
//SQL STATEMENTS
String SqlUpdate = "Update `stundenplan` SET Stunde = '"+daten[0]+"', Montag = '"+daten[1]+"', Dienstag = '"+daten[2]+"', Mittwoch = '"+daten[3]+"', Donnerstag = '"+daten[4]+"', Freitag = '"+daten[5]+"' WHERE Stunde = '"+daten[0]+"' ";
String SqlSelect = "SELECT Stunde FROM `stundenplan` WHERE Stunde = '"+daten[0]+"' ";
String SqlInsert = "INSERT INTO `stundenplan` (Stunde, Montag, Dienstag, Mittwoch, Donnerstag, Freitag) VALUES ('"+daten[0]+"', '"+daten[1]+"', '"+daten[2]+"', '"+daten[3]+"', '"+daten[4]+"', '"+daten[5]+"') ";
//SQL STATEMENTS
try
{
Class.forName ( treiber ).newInstance ();
conn = DriverManager.getConnection (url, userName, password);
}
catch (Exception e)
{
System.err.println ("Error connecting to server! "+e);
}
try
{
java.sql.Statement s = conn.createStatement();
ResultSet result = s.executeQuery (SqlSelect);
if(result.next())
{
if(result.getString("Stunde") == daten[0])
{
java.sql.Statement u = conn.createStatement();
int r = u.executeUpdate (SqlUpdate);
}
}
else
{
i = conn.createStatement();
int r2 = i.executeUpdate (SqlInsert);
}
s.close();
}
catch (Exception e)
{
return ("Error writing to Database! "+e);
}
conn.close();
//SQL CONNECTION
}
System.out.println("......... " + count + " entries proceded!");
return (SqlUpdate);
return (SqlInsert);
CSVFile.close();
Datei.close();
}
}
catch (IOException e)
{
System.out.println("Error reading from file! \n" +
"Please check the filename (test.csv) and the path.");
//System.out.println(e.toString());
}