Code for getDbTable()
is:
public String[][] getDbTable(String vs_name)
{
int i = 0;
String a[][] = new String[3600][16];
System.out.println("datetime is" +d);
System.out.println("datetime is" +currentDate);
try
{
con = getConnection();
String sql = "exec vcs_gauge @gauge_name=?,@first_rec_time=?,@last_rec_time=?";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("date is "+df.format(currentDate));
clstmt = con.prepareCall(sql);
clstmt.setString(1,"vs1_bag");
clstmt.setString(2, "2014-09-01 10:00:00");
clstmt.setString(3, "2014-09-01 11:00:00");
clstmt.execute();
rs = clstmt.getResultSet();
while (rs.next())
{
for(int j=0; j<16; j++)
{
a[i][j] = rs.getString(j+1);
}
i++;
}
}
catch( Exception e )
{
System.out.println("\nException in Display Bean in getDbTable(String code):"+e);
}
finally
{
//closeConnection();
}
return a;
}
How to get a single parameter from ResultSet
into String
type of array. I want to retrieve "logtime" which is my first column in the table and store it into String Array. I tried the following code:
public String[] getcahrttime() throws SQLException
{
getConnection();
getDbTable(vs_name);
String[] timeStr = null;
while(rs.next()){
char[] time= rs.getString("logtime").toCharArray();
timeStr = new String[time.length];
for (int i=0,len=time.length; i<len; i++){
timeStr[i] = String.valueOf(time[i]);
}
}
System.out.println("time is"+timeStr);
return timeStr;
}
where getConnection
establishes the connection and getDbTable(vs_name)
returns the table with all columns in it.
When I run the above code then timestr
is getting null
value. How to fix the problem.
OK - your first problem is that for each row, you're resetting the timeStr
variable and throwing away everything you read from earlier rows. That obviously can't work. Instead, we need to initialise the array once, and then only add to it (exactly one value for each row found).
The problem with this, is that arrays need to be initialised with a specific size - and we don't know ahead of time how many rows there will be. One way of dealing with this would be to run a count(*)
query first, and then initialise your array to that size. Or, specific ResultSet
implementations might be able to tell you the size of the set, though this is not guaranteed.
But the easiest way is simply to use a List
instead of an array, as these can be resized dynamically. I find them much easier to work with, so would pass it around as a List
right up to the point it needs to be converted - but here I'll convert it to an array for the sake of demonstration.
Asides from that it's easy - there's no need to mess with character arrays:
public String[] getChartTime() throws SQLException
{
// ...
// Earlier lines as before, let's start from the variable
final List<String> timeStr = new ArrayList<>();
while(rs.next()) {
// Just get the value of the column, and add it to the list
timeStr.add(rs.getString("logtime"));
}
// I would return the list here, but let's convert it to an array
return timeStr.toArray(new String[timeStr.size()]);
}