qq_18848743
2015-01-05 08:28
采纳率: 100%
浏览 10.0k
已采纳

Java连接数据库,成功加载SQL驱动程序,但数据库连接失败

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class T2
{
public static void main(String[] args)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("成功加载SQL驱动程序");
} catch (Exception e)
{
System.out.println("找不到SQL驱动程序");
}
try
{
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433:DatabanseName=SQltest", "sa", "xcvbnm");
Statement stmt = con.createStatement();
System.out.println("数据库连接成功");
} catch (Exception e)
{
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}

结果:
成功加载SQL驱动程序
com.microsoft.sqlserver.jdbc.SQLServerException: 端口号 1433:DatabanseName=SQltest 无效。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:691)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at test.T2.main(T2.java:21)
数据库连接失败

5条回答 默认 最新

相关推荐 更多相似问题