首页上一页 1 下一页尾页 1 条记录 1/1页
连接数据库出问题
发表在JavaWeb图书答疑
2009-12-29
是否精华
是
否
版块置顶:
是
否
书籍是JAVA从入门到精通 20.4.1连接数据库 346页
代码:
package number;
import java.sql.*; //导入java.sql包
public class Conn { //创建类Conn
Connection con; //声明Connection对象
public Connection getConnection(){ //建立返回值为Connection的方法
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("数据库驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;" +
"DatabaseName=db_jdbc","sa","123");
System.out.println("数据库连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return con; //按方法要求返回个Connection对象
}
public static void main(String[] args) { //主方法
Conn c = new Conn(); //创建本类对象
c.getConnection(); //调用连接数据库方法
}
}
我本身的sa密码是123
sqlserver 2000 的JDBC三个包我已经导进去了
报错如下:
数据库驱动加载成功
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at number.Conn.getConnection(Conn.java:14)
at number.Conn.main(Conn.java:23)
sql server已经装好,数据库也有了。端口1433未被占用。系统是XP SP3
在线等回复
代码:
package number;
import java.sql.*; //导入java.sql包
public class Conn { //创建类Conn
Connection con; //声明Connection对象
public Connection getConnection(){ //建立返回值为Connection的方法
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("数据库驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;" +
"DatabaseName=db_jdbc","sa","123");
System.out.println("数据库连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return con; //按方法要求返回个Connection对象
}
public static void main(String[] args) { //主方法
Conn c = new Conn(); //创建本类对象
c.getConnection(); //调用连接数据库方法
}
}
我本身的sa密码是123
sqlserver 2000 的JDBC三个包我已经导进去了
报错如下:
数据库驱动加载成功
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at number.Conn.getConnection(Conn.java:14)
at number.Conn.main(Conn.java:23)
sql server已经装好,数据库也有了。端口1433未被占用。系统是XP SP3
在线等回复