如何使用java jdbc连接到本地SQL数据库

时间:2022-08-10 01:44:42

When I try something like this:
Class.forName("com.mysql.jdbc.Driver").newInstance(); DriverManager.getConnection("jdbc:mysql://192.168.2.116:3306/SocialFamilyTree");

当我尝试这样的事情时:Class.forName(“com.mysql.jdbc.Driver”)。newInstance();的DriverManager.getConnection( “JDBC:MySQL的://192.168.2.116:3306 / SocialFamilyTree”);

I get an error:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

我收到一个错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

Tried:

try{
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
    DriverManager.getConnection("jdbc:sqlserver://localhost:1433");

}catch(Exception e){
    System.out.println("Couldn't get database connection.");
    e.printStackTrace();
}

and got:

Couldn't get database connection.
Oct 06, 2012 11:15:37 PM com.microsoft.sqlserver.jdbc.SQLServerConnection <init>

1 个解决方案

#1


0  

if you are trying to connect to sql server your code is wrong , as in that you are trying to connect to mysql .

如果你试图连接到sql server你的代码是错误的,因为你正在尝试连接到mysql。

Use jtds or sql server driver to connect Microsoft SQL Server 2005 JDBC Driver

使用jtds或sql server驱动程序连接Microsoft SQL Server 2005 JDBC驱动程序

DRIVER CLASS: com.microsoft.sqlserver.jdbc.SQLServerDriver 

DRIVER LOCATION: Specify the location on your machine of the Microsoft SQL Server driver. See your Microsoft SQL Server driver documentation for more details. Certain versions of the Microsoft SQL Server driver require more than one jar file for the driver location. In this case, simply separate each file location by a semi-colon.

驱动程序位置:指定计算机上Microsoft SQL Server驱动程序的位置。有关更多详细信息,请参阅Microsoft SQL Server驱动程序文档。某些版本的Microsoft SQL Server驱动程序需要多个jar文件作为驱动程序位置。在这种情况下,只需用分号分隔每个文件位置。

JDBC URL FORMAT: jdbc:sqlserver://<server_name>:<port> 

The default port for Microsoft SQL Server is 1433. Usually, if the default port is being used by the database server, the : value of the JDBC url can be omitted.

Microsoft SQL Server的默认端口是1433.通常,如果数据库服务器正在使用默认端口,则可以省略JDBC URL的:value。

Examples:

jdbc:sqlserver://neptune.acme.com:1433 

jdbc:sqlserver://127.0.0.1:1433 

#1


0  

if you are trying to connect to sql server your code is wrong , as in that you are trying to connect to mysql .

如果你试图连接到sql server你的代码是错误的,因为你正在尝试连接到mysql。

Use jtds or sql server driver to connect Microsoft SQL Server 2005 JDBC Driver

使用jtds或sql server驱动程序连接Microsoft SQL Server 2005 JDBC驱动程序

DRIVER CLASS: com.microsoft.sqlserver.jdbc.SQLServerDriver 

DRIVER LOCATION: Specify the location on your machine of the Microsoft SQL Server driver. See your Microsoft SQL Server driver documentation for more details. Certain versions of the Microsoft SQL Server driver require more than one jar file for the driver location. In this case, simply separate each file location by a semi-colon.

驱动程序位置:指定计算机上Microsoft SQL Server驱动程序的位置。有关更多详细信息,请参阅Microsoft SQL Server驱动程序文档。某些版本的Microsoft SQL Server驱动程序需要多个jar文件作为驱动程序位置。在这种情况下,只需用分号分隔每个文件位置。

JDBC URL FORMAT: jdbc:sqlserver://<server_name>:<port> 

The default port for Microsoft SQL Server is 1433. Usually, if the default port is being used by the database server, the : value of the JDBC url can be omitted.

Microsoft SQL Server的默认端口是1433.通常,如果数据库服务器正在使用默认端口,则可以省略JDBC URL的:value。

Examples:

jdbc:sqlserver://neptune.acme.com:1433 

jdbc:sqlserver://127.0.0.1:1433