HELP-Resultset method: getString w/ LONGVARCHAR
Hello Everyone. When I use the getString method of a ResultSet class on a LONGVARCHAR data type, I get unexpected results. For instance, If the column contains a long text with several words (LONGVARCHAR data type), the getString method generates a string for each word in that column! Therefore, what method do I use that will store serveral words in ONE string object?
Here's a sample code which a prints out column headers and their respective data from a Access database separated by a "~" symbol. However, when the ResultSet runs into a LONGVARCHAR data type, it generates a column for each word in the column that has a long text with several words separated by a space:
import java.sql.*;
import java.io.*;
class SimpleQuery {
public static void main(String args[]) {
String url = "jdbc:odbc:Books";
Connection con;
String query =
"select * from Publishers";
Statement stmt;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url,
"myLogin", "myPassword");
stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1) System.out.print("~ ");
String columnName = rsmd.getColumnName(i);
System.out.print(columnName);
}
System.out.println("");
while (rs.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
if (i > 1) System.out.print("~ ");
System.out.print(strValue);
String columnValue = rs.getString(i);
System.out.print(columnValue);
}
System.out.println("");
}
stmt.close();
con.close();
} catch(SQLException ex) {
System.err.print("SQLException: ");
System.err.println(ex.getMessage());
}
}
}

