问题
I have the following code used to connect to a mySQL server from a Google App Script. It was working prior to moving the server. Now, even though I have entered the new information(address, names, pasword), it no longer works. The error I am getting it "failed to establish a database connection. Check connection string, username and password." I am not familiar with servers and need to figure out how to fix this. I have Putty, but am unfamiliar with it - I tried, but it still would not connect. I have included my sanitized connection code below.
var address = //'address:port';
var user = //'username';
var userPwd = //'password';
var db = //'databasename';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
function testsql() {
var conn=Jdbc.getConnection(dbUrl,user,userPwd);
var stmt = conn.prepareStatement("show databases");
var dbList = stmt.executeQuery();
dbList.next(); var row = 0;
Logger.log("Start of Log:");
while(dbList.next()){
Logger.log(dbList.getString(1)); row++;
}
}
If the code is formatted correctly, is there another way to test the connection string?
I did find this question, but I am not 100% that is the problem. Google Apps Script JDBC connection problem
Thanks for any help!
回答1:
Another Possibility
Your code looks okay to me. But if you want to try something different, this one also creates a dialog to display all of the data. You welcome to it. I can tell you that I have a database on my website that I never could access but I moved over to a friends site and went right in. I think the first site was probably blocking ip addresses from google to keep all of the web scrapers out.
function returnAQuery(q)
{
var s = '';
var address = //'address:port';
var user = //'username';
var userPwd = //'password';
var db = //'databasename';
var dbUrl = 'jdbc:mysql://' + address + '/' + db;
s += dbUrl + ', ' + user + ', ' + userPwd + '<br />';
s += q + '<br />';
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var stmt = conn.createStatement();
var results = stmt.executeQuery(q);
if(results)
{
var numCols = results.getMetaData().getColumnCount();
for(var col = 0;col < numCols;col++)
{
if(col>0)s+=' , ';
s += results.getMetaData().getColumnName(col + 1);
}
s += '<br />';
while (results.next())
{
var rowString = '';
for (var col = 0; col < numCols; col++)
{
if(col>0)s+= ' , ';
s += results.getString(col + 1);
}
s += '<br />';
}
var ui=HtmlService.createHtmlOutput(s).setWidth(1200).setHeight(450);
SpreadsheetApp.getUi().showModelessDialog(ui, 'JDBC');
}
else
{
s += 'No results returned from query';
}
}
来源:https://stackoverflow.com/questions/46004986/jdbc-mysql-connection-string-issues-from-google-app-script