-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestConnection.java
More file actions
53 lines (44 loc) · 2.25 KB
/
TestConnection.java
File metadata and controls
53 lines (44 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.example.azure_sql_demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class TestConnection {
public static void main(String[] args) {
String server = "java-azure-cloud-sql-new.database.windows.net";
String database = "app-db-clean";
String username = "SEU_USUARIO"; // SUBSTITUA AQUI
String password = "SUA_SENHA"; // SUBSTITUA AQUI
// Diferentes URLs para testar
String[] connectionStrings = {
// URL 1 - Padrão com SSL
String.format("jdbc:sqlserver://%s:1433;database=%s;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;",
server, database),
// URL 2 - SSL flexível
String.format("jdbc:sqlserver://%s:1433;database=%s;encrypt=true;trustServerCertificate=true;loginTimeout=30;",
server, database),
// URL 3 - Sem SSL (não recomendado para produção)
String.format("jdbc:sqlserver://%s:1433;database=%s;encrypt=false;loginTimeout=30;",
server, database)
};
for (int i = 0; i < connectionStrings.length; i++) {
System.out.println("\n=== Teste " + (i + 1) + " ===");
System.out.println("URL: " + connectionStrings[i]);
try (Connection connection = DriverManager.getConnection(connectionStrings[i], username, password)) {
System.out.println("✅ SUCESSO! Conexão estabelecida.");
// Teste simples de query
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT @@VERSION");
if (rs.next()) {
System.out.println("Versão do SQL Server: " + rs.getString(1));
}
break; // Se chegou aqui, a conexão funciona
} catch (Exception e) {
System.err.println("❌ ERRO: " + e.getMessage());
if (e.getCause() != null) {
System.err.println("Causa: " + e.getCause().getMessage());
}
}
}
}
}