2010-12-15 60 views
2

Disons que j'ai une table appelée ABC avec 2 colonnes id (nombre), contenu (xml) dans DB2.Comment lire la colonne xml de db2 en utilisant jdbc

String q="select * from ABC where id=121"; 
Connection conn = getConnection(dbUrl,schemaName,userName,password); 
Statement stmt = conn.createStatement(); 
ResultSet rs = stmt.executeQuery(q); 
while(rs.next()) 
{ 
    //HERE HOW CAN I GET CONTENT COLUMN VALUE IN STRING FORMAT 
} 

J'ai essayé rs.getObject (i), rs.getString (i) et rs.getSQLXML (i) .getString(), mais pas de chance ... Et je besoin seule solution db2

Je fixe mon auto:

String q="select * from ABC where id=121"; 
Connection conn = getConnection(dbUrl,schemaName,userName,password); 
Statement stmt = conn.createStatement(); 
ResultSet rs = stmt.executeQuery(q); 
ResultSetMetaData rsmd = rs.getMetaData(); 
while(rs.next()) 
{ 
    if(rsmd.getColumnTypeName(i).equalsIgnoreCase("XML")) 
    { 
      convertInStreamToString(rs.getBinaryStream(i)); 

    } 
    else 
    { 
     rs.getObject(i); 
    } 
} 

private String convertInStreamToString(InputStream data) throws Exception 
{ 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     byte[] buf = new byte[1024]; 
     int n = 0; 
     while ((n=data.read(buf))>=0) 
     { 
      baos.write(buf, 0, n); 
     } 

     data.close(); 
     byte[] bytes = baos.toByteArray(); 
     return new String(bytes); 
} 

Hope this helps ...

+0

Je me suis fixé – sanumala

Répondre

1

lignes XML sont mises en œuvre de grands objets. Essayez rs.getClob(i), puis appelez le getSubstring pour récupérer le fichier XML. Here's an example.