首页上一页 1 下一页尾页 1 条记录 1/1页
《JAVA从入门到精通》第20章数据库连接问题
发表在Java图书答疑
2009-03-04
是否精华
是
否
版块置顶:
是
否
package com.sl20;
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","");
System.out.println("数据库连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return con; //按方法要求返回个Connection对象
}
public static void main(String[] args) { //主方法
Conn c = new Conn(); //创建本类对象
c.getConnection(); //调用连接数据库方法
}
}
数据库驱动加载成功
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 com.sl20.Conn.getConnection(Conn.java:14)
at com.sl20.Conn.main(Conn.java:23)
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","");
System.out.println("数据库连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
return con; //按方法要求返回个Connection对象
}
public static void main(String[] args) { //主方法
Conn c = new Conn(); //创建本类对象
c.getConnection(); //调用连接数据库方法
}
}
数据库驱动加载成功
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 com.sl20.Conn.getConnection(Conn.java:14)
at com.sl20.Conn.main(Conn.java:23)