• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to read/write text file to db2 by using java code

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,

Can any one tell me a way to read and write the text file data or big data to db2 database using java code.

Thanks in advance...
V.shan
 
Shan Accent
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai every,

Please give me some idea, to do it. I created the clob field indb2 database. I don't know how to write / read the clob data.

Thanks,
V.Shan
 
author & internet detective
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shan,
You can read the CLOB as an AsciiStream.
 
Shan Accent
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai

I created the table to insert a clob data as follows
db2 => create table ctest(datas clob(65536))
DB20000I The SQL command completed successfully.
db2 => describe table ctest

Column Type Type
name schema name Length Scale Nulls

------------------------------ --------- ------------------ -------- ----- -----
-
DATAS SYSIBM CLOB 65536 0 Yes


1 record(s) selected.

////////////////////////////////////////////

I tried the following java code to insert a clob data.

public static synchronized Connection getConnection() throws Exception {

Connection m_connectionDatabase = null;
try {
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");

// Get the connection to the database.
m_connectionDatabase = DriverManager.getConnection(
"jdbc b2 ps","","");
}catch (Exception e) {
throw new Exception("Failed to connect to the database. ", e);
}

return m_connectionDatabase;
}

public static void main(String a[]) {

File file = new File("d:\\a.txt");
InputStream fis = new FileInputStream(file);
Connection con = getConnection();
PreparedStatement pstmt = con.prepareStatement("insert into ctest values(?)");

//try 1
pstmt.setAsciiStream(1,fis,(int)file.length());

// try 2
pstmt.setCharacterStream(1,fis,(int)(file.length()));

// try 3
pstmt.setBinaryStream(1,fis,(int)(file.length()));

// try 4
byte[] b = new byte[fis.available()];
fis.read(b);
pstmt.setBytes(1, b);

// try 5
byte[] b = new byte[fis.available()];
fis.read(b);
pstmt.setString(1, new String(b));


pstmt.executeUpdate();
pstmt.close();
fis.close();
}

///////////////
Result:
////////////////

If the file size is less than 32kb, it inserted successfully.

but if more than 32kb, it throws follwing exception
COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0352N An unsupported SQLTYPE was encountered in position "1" of the input list (SQLDA). SQLSTATE=56084

at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source)
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.loadParameters(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(Unknown Source)
at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(Unknown Source)


//////////////////////
Can any body please help out to insert clob value.

Thanks in advance
V.Shan
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic