java 与sql sever 2005 连接不上 (急!)
程序:importjava.sql.*;publicclassabc{/***@paramargs*///TODO自动生成方法存根publicstaticvoidmain...
程序:import java.sql.*;
public class abc
{
/**
* @param args
*/
// TODO 自动生成方法存根
public static void main(String[] args)
{
// TODO 自动生成方法存根
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Northwind"; //连接服务器和数据库sample
String userName = "gx"; //默认用户名
String userPwd = "123456"; //密码
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}
错误:com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'gx' 登录失败。该用户与可信 SQL Server 连接无关联。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2532)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at abc.main(abc.java:22) 展开
public class abc
{
/**
* @param args
*/
// TODO 自动生成方法存根
public static void main(String[] args)
{
// TODO 自动生成方法存根
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=Northwind"; //连接服务器和数据库sample
String userName = "gx"; //默认用户名
String userPwd = "123456"; //密码
Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}
错误:com.microsoft.sqlserver.jdbc.SQLServerException: 用户 'gx' 登录失败。该用户与可信 SQL Server 连接无关联。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2532)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at abc.main(abc.java:22) 展开
6个回答
展开全部
你的代码是这一行出错了吧,
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
我估计是你的dbURL写错了··
我附送一个sqlserver2005连接数据库的帮助类给你··
像这种连接 做成单例模式要比普通模式好很多···
import java.sql.Connection;
import java.sql.DriverManager;
public class DBHelp {
private final static String JDBCURL="jdbc:sqlserver://localhost:1433;databasename=test";
private final static String USER="sa";
private final static String PWD="";
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getCon()throws Exception{
Connection con=DriverManager.getConnection(JDBCURL,USER,PWD);
return con;
}
}
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
我估计是你的dbURL写错了··
我附送一个sqlserver2005连接数据库的帮助类给你··
像这种连接 做成单例模式要比普通模式好很多···
import java.sql.Connection;
import java.sql.DriverManager;
public class DBHelp {
private final static String JDBCURL="jdbc:sqlserver://localhost:1433;databasename=test";
private final static String USER="sa";
private final static String PWD="";
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getCon()throws Exception{
Connection con=DriverManager.getConnection(JDBCURL,USER,PWD);
return con;
}
}
展开全部
用户名或者密码错误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你有导入jar包吗?如果导入了,jar文件的位置是否改变了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2010-04-27
展开全部
用命令窗口输入 netstat -a 检查监听端口 是否开启 具体详情可访问
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
加下QQ方便点解决 留下你的QQ吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
TCP/IP有没启动?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询