无法使用Java编程连接SQL Server

时间:2019-06-07 17:59:38

标签: java sql-server

我正在尝试使用JAVA编程连接SQL数据库。但是会弹出如下错误。

“与主机SERVER的TCP / IP连接,端口1433失败。错误:连接超时。请验证连接属性,并检查主机上是否正在运行SQL Server实例并接受TCP / IP端口连接,并且没有防火墙阻止与该端口的TCP连接。”

我还启用了TCP / IP,并在IPALL 1433中添加了端口号并重新启动了SQL Server。但是仍然无法使用Java编程进行连接,它显示出与上述相同的错误。

连接SQL Server数据库的Java编码如下:

public class NewMain {


public static void main(String[] args) throws ClassNotFoundException {

    String connectionUrl = "jdbc:sqlserver://SERVER:1433;username=Mselvakumars;password=Welcome1345;databaseName=Loan;";

    try {
         Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection con = DriverManager.getConnection(connectionUrl);

        System.out.println(con);

    } 
    catch (SQLException e) {
        e.printStackTrace();
    }
}

}

我无法找到错误仍然存​​在的根本原因,服务器数据库凭据正确。我不知道代码中是否缺少某些内容。还尝试过针对此问题的堆栈溢出建议。

1 个答案:

答案 0 :(得分:0)

尝试这个为我工作

logger.info(“加载驱动程序类”);             Class.forName(“ com.microsoft.sqlserver.jdbc.SQLServerDriver”);

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
            while page == 0:
                if event.type == pygame.MOUSEBUTTONDOWN:
                    main_menu()
                if event.type == pygame.KEYDOWN:
                    if event.key == ord('b'):
                        page += 1
                    if event.key == ord('1'):
                        page += 2
                    if event.key == ord('k'):
                        k += 1
                        print(k, i, d, z)
                    if event.key == ord('i'):
                        i += 1
                        print(k, i, d, z)
                    if event.key == ord('d'):
                        d += 1
                        print(k, i, d, z)
                    if event.key == ord('z'):
                        z += 1
                        print(k, i, d, z)
                    if event.key == pygame.K_BACKSPACE:
                        main_menu()
                    while k == 1 and i == 1 and d == 1 and z == 1:
                        screen.fill(BLACK)
                        kidz = pygame.image.load("download.png").convert()
                        screen.blit(kidz, [0, 0])
                        pygame.display.flip()
#battle royal
            while page == 1:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        done = True
                    fortnite = pygame.image.load("battle_royal.jpg").convert()
                    screen.blit(fortnite, [0, 0])
                    laod()
                    pygame.display.flip()
#one player
            while page == 2:
                p1 = pygame.image.load("p1.jpg").convert()
                screen.blit(p1, [0, 0])
                pygame.display.flip()
                player_position = pygame.mouse.get_pos()
                x = player_position[0]
                print(player_position)
                print(x)
                char = pygame.image.load("wingu.png").convert()
                char.set_colorkey(WHITE)
                screen.blit(char, [x, 650])
                pygame.display.flip()