Jacobvdb
Jacobvdb

Reputation: 1448

Error: Resultset does not have a column 1

Using the JDBC services to connect to a Google Cloud SQL and
following the example of the overview everything works as expected.

When I try to do some other SQL statement like select database();
I get the error : Resultset does not have a column 1

This is the code I am using.

var sqlStatement = "select database();";
var pstmt = conn.prepareStatement(sqlStatement);
var rs = pstmt.executeQuery(sqlStatement);  
app.add(app.createLabel(rs.getString(1)));

I also tried with getObject() but it does get any better .

So any Idea, tutorials or examples how to deal with sqlStatements
different than the tipical select like use database or
select database()?

Upvotes: 1

Views: 489

Answers (2)

Arun Nagarajan
Arun Nagarajan

Reputation: 5645

You are missing the statement to start the iterator - this is the .next() call on the record set. Here is an function that works for me. I am just logging the DB name here using Logger, but same should work for UiApp as well.

function simpleRead() {
  var conn = Jdbc.getCloudSqlConnection("jdbc:google:rdbms://arunnagarajan:...");
  var stmt = conn.createStatement();
  var rs = stmt.executeQuery("select database()");
  rs.next(); //if you expect multiple rows, then do this in while(rs.next()) loop
  Logger.log(rs.getString(1));
  rs.close();
  stmt.close();
  conn.close();
}

Upvotes: 1

Thunder Rabbit
Thunder Rabbit

Reputation: 5449

On the last line, try

app.add(app.createLabel(rs.getString(0)));

Upvotes: 0

Related Questions