Forum Controls
Spotlight Features

The Rich Engineering Heritage Behind Dependency Injection

Andrew McVeigh takes us on a tour of the rich heritage behind dependency injection, what it represents, and tells us why its here to stay.

NetBeans 6: Matisse Updates

NetBeans 6 delivers great updates to the Matisse GUI builder. Spend a few minutes with Roman Strobl and get an expert briefing on what's new and what has changed.

Introduction to Groovy Part 3

In this, the third and final installation of Andres' Introduction to Groovy series, you learn about how Groovy handles variable numbers of arguments, named parameters, currying, and more about Groovy operators. Including, some new operators.

Easier Custom Components with Swing Fuse

Swing Fuse (actually just Fuse), is a framework designed to make it easier to create your own custom desktop components. In this article, Daniel Spiewak shows you how to get started and provides sample source code you can download.

Benchmark Analysis: Guice vs Spring

Willam Louth shows how he uses JXInsight Probes to investigate probable performance issues with code bases that he is not familiar with. He also highlights possible pitfalls in creating a benchmark, as well as in the analysis of results.
Replies: 3 - Pages: 1  
Threads: [ Previous | Next ]
  Click to reply to this thread Reply

Import Data from Txt or CSV files into MYSQL database tables

At 9:41 AM on Nov 17, 2005, Java-Tips Team wrote:

Mysql-connector-java-3.1.10 is a JDBC connector for MYSQL database. MYSQL provides LOAD DATA INFILE utility to import data from files like csv, txt or xls into database tables.

The example below imports data from .txt file into table.

temp.txt file is a tab separated file:

"1 string"      100
"2 string"      102
"3 string"      104
"4 string"      106

testtable structure
CREATE TABLE testtable
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
text varchar(45) NOT NULL,
price integer not null);
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class automateImport
{
    public static void main(String[] args) 
    {
        DBase db = new DBase();
        Connection conn = db.connect(
    "jdbc:mysql://localhost:3306/test","root","caspian");
        db.importData(conn,args[0]);
    }
 
}
 
class DBase
{
    public DBase()
    {
    }
 
    public Connection connect(String db_connect_str, 
  String db_userid, String db_password)
    {
        Connection conn;
        try 
        {
            Class.forName(  
    "com.mysql.jdbc.Driver").newInstance();
 
            conn = DriverManager.getConnection(db_connect_str, 
    db_userid, db_password);
        
        }
        catch(Exception e)
        {
            e.printStackTrace();
            conn = null;
        }
 
        return conn;    
    }
    
    public void importData(Connection conn,String filename)
    {
        Statement stmt;
        String query;
 
        try
        {
            stmt = conn.createStatement(
    ResultSet.TYPE_SCROLL_SENSITIVE,
    ResultSet.CONCUR_UPDATABLE);
 
            query = "LOAD DATA INFILE '"+filename+
    "' INTO TABLE testtable (text,price);";
 
            stmt.executeUpdate(query);
                
        }
        catch(Exception e)
        {
            e.printStackTrace();
            stmt = null;
        }
    }
};

If you want to import a CSV file, you can use the following query:

query = "LOAD DATA INFILE '"+filename+"' INTO TABLE testtable  FIELDS
 TERMINATED BY ',' (text,price)";
Java-Tips Team
http://www.java-tips.org/
1 . At 10:54 AM on Nov 17, 2005, Mark Matthews wrote:
  Click to reply to this thread Reply

Re: Import Data from Txt or CSV files into MYSQL database tables

Even more useful, one can add "LOCAL" to that query, and the file will be read by the client, and sent to the server:

LOAD DATA LOCAL INFILE  '...'


If you set the configuration property "allowUrlInLocalInfile=true", the filename can actually be a URL, and the driver will connect to that URL and stream the data to the server.

In MySQL-5.0, you can even do basic transformations on the data before it is stored in the table, so you have the basics of an ETL (extract, transform, load) utility from your JDBC driver:

http://dev.mysql.com/doc/refman/5.0/en/load-data.html

(look for information on the SET clause).

The "LOAD DATA [LOCAL] INFILE" feature is actually quite a powerful and underused tool in my opinion!
2 . At 5:15 PM on Jan 5, 2006, ilango gurusamy wrote:
  Click to reply to this thread Reply

Re: Import Data from Txt or CSV files into MYSQL database tables

Hi
I tried out the program and get a bunch of errors as follows:

Exception in thread "main" java.lang.NoClassDefFoundError: AutomateImport (wrong
name: us/ilango/AutomateImport)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)


-------

Any help is deeply appreciated. Thanks a lot.
3 . At 6:38 PM on Jan 5, 2006, ilango gurusamy wrote:
  Click to reply to this thread Reply

Re: Import Data from Txt or CSV files into MYSQL database tables

I tried to run the program again and the errors are as follows:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password
: YES)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:771)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3649)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1176)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2558)
at com.mysql.jdbc.Connection. (Connection.java:1485)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java
:266)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at DBase.connect(AutomateImport.java:35)
at AutomateImport.main(AutomateImport.java:13)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at AutomateImport.main(AutomateImport.java:15)



I am not sure why this is happening.Any help is greatly appreciated

thanks
ilango

thread.rss_message