If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Loading huge data Files using DB2 loader from java

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-11-06, 02:53
sriha sriha is offline
Registered User
 
Join Date: Sep 2006
Posts: 1
Post Loading huge data Files using DB2 loader from java

Hi All,


I am new to DB2 Data base. I am using DB2 8.2 Version. I want to load huge data files using DB2 loader from java. Can I call the Load command from the java.

Like

Runtime.getRuntime.execute("Load Command");

User/Admin uploads large file from the browser. The file is fixed length formate file with .txt extension.

Can any boyd please help me, how and what is the command i need to use? It will be helpful if you give me the sample code for it.

Is it possible calling the DB2 loader from java.

Alternate is, calling the DB2 loader from store procedure. Which is the better idea.

Even i dont know what is the command need to give and its parameters.

Can Anybody please help me? I really appreciate for your help.

Thanks in advance,
Sriha
Reply With Quote
  #2 (permalink)  
Old 09-11-06, 09:27
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
To use LOAD from a SP, you would have to write the SP in a language like C++. To do it from Java, it would look like this:

// This is code to connect to DB, then a loop to do successive loads, then disconnect

// do import work
System.out.println("Connect");
runTime = Runtime.getRuntime();
db2Command = new StringBuffer("DB2 CONNECT TO ");
db2Command.append(getDbName());
db2Command.append(" USER ");
db2Command.append(dbLogin.getLoggedInUsername());
db2Command.append(" USING ");
db2Command.append(dbLogin.getLoggedInPassword());
child = runTime.exec(db2Command.toString());

inStream = new java.io.InputStreamReader(child.getInputStream());
buffReader = new java.io.BufferedReader(inStream);
inLine = null;
while ((inLine = buffReader.readLine()) != null) {
System.out.println(inLine);
}
exitVal = child.waitFor();

// import data

tigerPath = new File(dataDirectory);

for (i=0; i< rts.length; ++i)
{
importRT(tigerPath,rts[i],cl[i],nl[i],col[i]);
}

System.out.println("Disconnect");
runTime = Runtime.getRuntime();
child = runTime.exec("DB2 DISCONNECT ALL");

inStream = new java.io.InputStreamReader(child.getInputStream());
buffReader = new java.io.BufferedReader(inStream);
inLine = null;
while ((inLine = buffReader.readLine()) != null) {
System.out.println(inLine);
}
exitVal = child.waitFor();


// this is the function to load

public static void importRT(File dir, String recordType, String columnLocations, String nullIndicators, String columns){
int i;
StringBuffer importCommand;
java.io.InputStreamReader inStream;
java.io.BufferedReader buffReader;
String inLine;
int exitVal;
String retString;

try {
final String fileType = new String("." + recordType);
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.toUpperCase().endsWith(fileType);
}
};

plString = new String(recordType);

String files[] = dir.list(filter);

progMax = files.length;
progCurrent = 0;
display.asyncExec(importTiger);
for (i=0; i< files.length; ++i)
{
importCommand = new StringBuffer("DB2 LOAD CLIENT FROM ");
importCommand.append(dir.getPath());
importCommand.append("\\");
importCommand.append(files[i]);
importCommand.append(" OF ASC METHOD L ");
importCommand.append(columnLocations);
importCommand.append(" NULL INDICATORS ");
importCommand.append(nullIndicators);
importCommand.append(" MESSAGES ");
importCommand.append(dir.getPath());
importCommand.append("\\");
importCommand.append(files[i]);
importCommand.append(".ldm INSERT INTO ANDY.TIGER_");
importCommand.append(recordType);
importCommand.append(" ");
importCommand.append(columns);
importCommand.append(" nonrecoverable without prompting ");

System.out.println(importCommand.toString());
//cStmt.setString(1,importCommand.toString());
//cStmt.execute();
//rs = cStmt.getResultSet();

// get node for database name
child = runTime.exec(importCommand.toString());

inStream = new java.io.InputStreamReader(child.getInputStream());
buffReader = new java.io.BufferedReader(inStream);
inLine = null;
while ((inLine = buffReader.readLine()) != null) {
System.out.print(inLine+"\n");
}
exitVal = child.waitFor();
progCurrent = i+1;
display.asyncExec(importTiger);
}

} catch (Exception e) {
e.printStackTrace(System.out);
}
}


HTH

Andy
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On