Connect Google Sheets scripts to MySQL

367 Views Asked by At

I'm just learning to use Google Sheets scripts, and trying to log into my MySQL database, below is my code

function readData() {
  var conn = Jdbc.getConnection('jdbc:mysql://hostname(or using IP Address):3306/database_name',username,password);
  var stmt = conn.createStatement();
  var results = stmt.executeQuery('SELECT * FROM tablename');
  var metaData=results.getMetaData();
  var numCols = metaData.getColumnCount();
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName('Sheet1');
  sheet.clearContents();
  var arr=[];

  for (var col = 0; col < numCols; col++) {
    arr.push(metaData.getColumnName(col + 1));
  }

  sheet.appendRow(arr);

while (results.next()) {
  arr=[];
  for (var col = 0; col < numCols; col++) {
    arr.push(results.getString(col + 1));
  }
  sheet.appendRow(arr);
}

results.close();
stmt.close();
sheet.autoResizeColumns(1, numCols+1);
}

and also I've already Additional MySQL Access Hosts (WHM) from cPanel-Databases-Remote MySQL

64.18.0-15.%
64.233.160-191.%
66.102.0-15.%
66.249.80-95.%
72.14.192-255.%
74.125.%
173.194.%
207.126.144-159.%
209.85.128-255.%
216.239.32-63.%

So I try to run it, but I got error message

Exception: Failed to establish a database connection. Check connection string, username and password. (line 9, file "Code")

Is there anything missing or not set up yet? Would please help me how to solve this?

0

There are 0 best solutions below