I try to connect to Azure SQL with MS JDBC driver:
import java.sql.*;
public class ExampleSQLJDBC {
public static void main(String[] args) {
/
This code works well: to connect Azure database and grab data from table
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Get Data', functionName: 'readData'}
];
spreadsheet.addMenu('Report', menuItems);
}
// Replace the variables in this block with your values.
var hostName = 'SERVER.database.windows.net:1433;'
var db = 'DBNAME;';
var user = 'USER@SERVER';
var userPwd = 'PASSWORD';
var dbUrl = 'jdbc:sqlserver://'+hostName + 'databaseName='+db;
function readData() {
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
var stmt = conn.createStatement();
// Place your query below
var results = stmt.executeQuery('SELECT TOP (10) * FROM [dbo].[NAME]');
var metaData=results.getMetaData();
var numCols = metaData.getColumnCount();
var sheet = SpreadsheetApp.getActiveSheet();
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);
}
Per my experience, I think the issue was caused by the connection string which is the variable connectionUrl
of your code.
Please see the completed connection string below.
jdbc:sqlserver://<hostname>.database.windows.net:1433;database=<database-name>;user=<username>@<hostname>;password={your_password_here};encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;
You can find it at the link Show connection strings
of the right bar of the tab DASHBOARD
on Azure old portal (see Fig 1), or see it at the link Show database connection strings
on Azure new portal (see Fig 2).
Fig 1. Show connection string on Azure old portal
Fig 2. Show database connection strings on Azure new portal
Hope it helps. Any concern, please feel free to let me know.
Update
Here is my code for connecting SQL Azure.
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String hostName = "xxxxx";
String dbName = "petersqldb";
String user = "peter@xxxx";
String password = "xxxxxxxx";
String url = String.format("jdbc:sqlserver://%s.database.windows.net:1433;database=%s;user=%s;password=%s;encrypt=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;", hostName, dbName, user, password);
Connection conn = DriverManager.getConnection(url, user, password);
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("select 1+1 as sum");
while(rs.next()) {
System.out.println(rs.getInt("sum"));
}
rs.close();
stat.close();
conn.close();
}
The problem was solved with these steps:
scutil --get HostName
(case sensitive!) sudo scutil --set HostName "newname"
Source: https://youtrack.jetbrains.com/issue/JRE-221#comment=27-1826316