Load a big CSV file into a MySQL DB
Submitted by rac on 2 January, 2008 - 13:16.
Used Libraries
- mysql-connector-java-5.1.5-bin.jar
CSV2SQLImporter.java
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.Statement;
-
import java.sql.ResultSet;
-
import java.sql.SQLException;
-
-
class CSV2SQLImporter
-
{
-
Connection conn = null;
-
public CSV2SQLImporter()
-
{
-
}
-
-
{
-
Connection conntmp;
-
try
-
{
-
Class.forName(
-
"com.mysql.jdbc.Driver").newInstance();
-
-
db_userid, db_password);
-
-
}
-
{
-
System.out.println("SQL Error Code: "+es.getErrorCode()+"\nSQL state: "+es.getSQLState()+"\nException Message: "+es.getMessage());
-
es.printStackTrace();
-
conntmp = null;
-
return false;
-
}
-
{
-
e.printStackTrace();
-
conntmp = null;
-
return false;
-
}
-
-
this.conn = conntmp;
-
return true;
-
}
-
-
public void importData(String filename, String table)
-
{
-
Statement stmt;
-
String query;
-
-
try
-
{
-
stmt = this.conn.createStatement(
-
ResultSet.TYPE_SCROLL_SENSITIVE,
-
-
query = "LOAD DATA LOCAL " +
-
"INFILE '" + filename + "'" +
-
" REPLACE" +
-
" INTO TABLE " + table +
-
" FIELDS" +
-
" TERMINATED BY ';';";
-
stmt.executeUpdate(query);
-
-
}
-
{
-
e.printStackTrace();
-
stmt = null;
-
}
-
}
-
};
sampleGUIimport.java
-
import javax.swing.*;
-
-
public class sampleGUIimport extends JFrame
-
{
-
JTextArea response;
-
/**
-
* @param importfile.csv table
-
*/
-
{
-
sampleGUIimport gui = new sampleGUIimport();
-
String server = "remote.server.net";
-
String port = "3306";
-
String mysqldb = "remote_db";
-
String user = "dbuser";
-
String password = "dbpassword";
-
-
//String csvfile = "C:/temp/sample.txt";
-
String csvfile1 = "//source.server.net/windowsshare/sample1.txt";
-
String table1 = "tbl_sample1";
-
String csvfile2 = "//source.server.net/windowsshare/sample2.txt";
-
String table2 = "tbl_sample2";
-
-
CSV2SQLImporter db = new CSV2SQLImporter();
-
boolean connect = db.connect("jdbc:mysql://"+server+":"+port+"/"+mysqldb,user,password);
-
if(connect){
-
gui.addResponse("Importing sample1 Data...");
-
db.importData(csvfile1, table1);
-
gui.addResponse("Importing sample2 Data...");
-
db.importData(csvfile2, table2);
-
gui.addResponse("Data import done");
-
} else {
-
gui.addResponse("Couldn't connect to mysql db");
-
}
-
}
-
-
public sampleGUIimport() {
-
getContentPane().setLayout(null);
-
-
response.setLocation(,);
-
response.setSize(546,528);
-
response.setRows(5);
-
response.setColumns(5);
-
response.setToolTipText("Here you see what I'm doing");
-
response.setEditable(false);
-
getContentPane().add(response);
-
-
setTitle("multi sample file Import Utility");
-
setSize(546,528);
-
setVisible(true);
-
setResizable(false);
-
}
-
-
this.response.setText(this.response.getText()+newresponse+"\n");
-
}
-
-
}
Sample ANT script to build JAR
-
<?xml version="1.0" encoding="UTF-8"?>
-
<project default="jar">
-
<!-- Ordnerstruktur -->
-
<property name="project.root" location="../../../../../../" />
-
<property name="src.dir" location="${project.root}/src/" />
-
<property name="srctmp.dir" location="${project.root}/srctmp/" />
-
<property name="bin.dir" location="${project.root}/bin/" />
-
<property name="lib.dir" location="${project.root}/lib/" />
-
<property name="tmp.dir" location="${project.root}/tmp/" />
-
-
<!-- wichtige Files -->
-
<property name="mysql-connector-java-5.1.5-bin.jar" location="${lib.dir}/mysql-connector-java-5.1.5-bin.jar" />
-
<property name="SAMPLEimporter.jar" location="${bin.dir}/SAMPLEimporter.jar" />
-
<property name="SAMPLEimporterWOLIB.jar" location="${bin.dir}/SAMPLEimporterWOLIB.jar" />
-
<property name="main.class" value="org.test.sample.csv2sql.SAMPLEimport" />
-
-
<!-- Alle libs die verwendet werden -->
-
<fileset id="lib.jars" dir="${lib.dir}">
-
<include name="mysql-connector-java-5.1.5-bin.jar"/>
-
</fileset>
-
-
<path id="libs">
-
<fileset refid="lib.jars"/>
-
</path>
-
-
<!-- Compiliert das Ganze Projekt -->
-
<target name="compile">
-
<mkdir dir="${bin.dir}" />
-
<javac srcdir="${src.dir}" destdir="${bin.dir}" classpathref="libs" />
-
</target>
-
-
<!-- Erstellt ein Projekt jar File -->
-
<target name="jar" depends="compile">
-
<jar destfile="${SAMPLEimporterWOLIB.jar}" basedir="${bin.dir}" includes="org/test/sample/csv2sql/*.class">
-
<manifest>
-
<attribute name="Main-Class" value="${main.class}" />
-
</manifest>
-
</jar>
-
</target>
-
-
<!-- Erstellt ein Jar welches alle Librarys enthaellt -->
-
<target name="oneJar" depends="jar">
-
<mkdir dir="${tmp.dir}" />
-
<unjar dest="${tmp.dir}" src="${SAMPLEimporterWOLIB.jar}" />
-
<unjar dest="${tmp.dir}">
-
<fileset refid="lib.jars" />
-
</unjar>
-
<delete dir="${tmp.dir}/META-INF"/>
-
<jar destfile="${SAMPLEimporter.jar}" basedir="${tmp.dir}" includes="**/*">
-
<manifest>
-
<attribute name="Main-Class" value="${main.class}" />
-
</manifest>
-
</jar>
-
<delete dir="${tmp.dir}" />
-
</target>
-
-
<!-- Fuehrt SAMPLEimporter aus -->
-
<target name="run" depends="oneJar">
-
<java classname="${main.class}" classpath="${SAMPLEimporter.jar}" fork="true" />
-
</target>
-
-
-
<!-- Loescht alle erstellten Files -->
-
<target name="clean">
-
<delete dir="${bin.dir}" />
-
-
</target>
-
</project>
Trackback URL for this post:
http://www.2030.tk/trackback/23
»
- Download PDF
- Printer-friendly version
- 1356 reads

Post new comment