Advertisement
angeliski

Conexão SQL Server JDBC - Java com Autenticação do SQL

Nov 6th, 2011
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class Main {
  4.   public static void main(String[] args) {
  5.     // string de conexão...usando Windows Authentication
  6.     String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
  7.       "databaseName=estudos;";
  8.  
  9.     try {
  10.       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
  11.       Connection conn = DriverManager.getConnection(connectionUrl,"sa","1234");
  12.       System.out.println("Conexão obtida com sucesso.");
  13.     }
  14.     catch (SQLException ex) {
  15.       System.out.println("SQLException: " + ex.getMessage());
  16.       System.out.println("SQLState: " + ex.getSQLState());
  17.       System.out.println("VendorError: " + ex.getErrorCode());
  18.     }
  19.     catch (Exception e) {
  20.        
  21.       System.out.println("Problemas ao tentar conectar com o banco de dados: " + e);
  22.     }
  23.   }
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement