8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Apps ScriptからJDBCでAzue SQL Databaeにつないでみた

Posted at

大体これ通りやるとできます。一瞬詰まったので書いておく。
https://developers.google.com/apps-script/guides/jdbc

Azure SQL DatabaeへのアクセスはApps Script's IP addessesから行われるのでIP制限を外す。

64.18.0.0 - 64.18.15.255
64.233.160.0 - 64.233.191.255
66.102.0.0 - 66.102.15.255
66.249.80.0 - 66.249.95.255
72.14.192.0 - 72.14.255.255
74.125.0.0 - 74.125.255.255
173.194.0.0 - 173.194.255.255
207.126.144.0 - 207.126.159.255
209.85.128.0 - 209.85.255.255
216.239.32.0 - 216.239.63.255

次に接続文字列をJDBCコピーする。
無題.png

jdbc:sqlserver://SERVER_NAME.database.windows.net:1433;database=DATABASE_NAME;

このオプションは使えないので消す。
user
encrypt
trustServerCertificate
hostNameInCertificate
loginTimeout

ズバリこう

jdbcToSqldatabae.gs
// Replace the variables in this block with real values.
var address = 'SERVER_NAME.database.windows.net:1433;databaseName=DATABASE_NAME;';
var user = 'USER_NAME@SERVER_NAME'; // User
var userPwd = 'PASSWORD';

var dbUrl = 'jdbc:sqlserver://' + address;

// Read up to 1000 rows of data from the table and log them.
function readFromTable() {
  var conn = Jdbc.getConnection(dbUrl, user, userPwd);

  var start = new Date();
  var stmt = conn.createStatement();
  stmt.setMaxRows(1000);
  var results = stmt.executeQuery('SELECT * FROM [SalesLT].[Address]');
  var numCols = results.getMetaData().getColumnCount();

  while (results.next()) {
    var rowString = '';
    for (var col = 0; col < numCols; col++) {
      rowString += results.getString(col + 1) + '\t';
    }
    Logger.log(rowString)
  }

  results.close();
  stmt.close();

  var end = new Date();
  Logger.log('Time elapsed: %sms', end - start);
}

でもこれだとGoogle Apps Scriptからだと入り放題なんだよねー

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?