首页上一页 1 下一页尾页 5 条记录 1/1页
《JAVA从入门到精通》问题
发表在Java图书答疑
2009-06-19
是否精华
是
否
版块置顶:
是
否
问题是关于第20章数据库课后第一个联系练习题de .
我运行的是书里带的标准答案可是还报错!
下面是代码:
import java.sql.*;
public class UpdateStu {
static Connection con;
static Statement sql;
static ResultSet res;
public Connection getConnection() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;"
+ "DatabaseName=db_jdbc", "sa", "");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
UpdateStu c = new UpdateStu();
con = c.getConnection();
try {
sql = con.createStatement();
res = sql.executeQuery("select * from tb_emp where dapt = '销售部'");
while (res.next()) {
String id = res.getString(1);
String name = res.getString("name");
String sex = res.getString("sex");
String brithday = res.getString("brithday");
System.out.print("编号:" + id);
System.out.print(" 姓名:" + name);
System.out.print(" 性别:" + sex);
System.out.println(" 生日:" + brithday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面是报错信息:
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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at UpdateStu.getConnection(UpdateStu.java:12)
at UpdateStu.main(UpdateStu.java:22)
java.lang.NullPointerException
at UpdateStu.main(UpdateStu.java:24)
我运行的是书里带的标准答案可是还报错!
下面是代码:
import java.sql.*;
public class UpdateStu {
static Connection con;
static Statement sql;
static ResultSet res;
public Connection getConnection() {
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;"
+ "DatabaseName=db_jdbc", "sa", "");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public static void main(String[] args) {
UpdateStu c = new UpdateStu();
con = c.getConnection();
try {
sql = con.createStatement();
res = sql.executeQuery("select * from tb_emp where dapt = '销售部'");
while (res.next()) {
String id = res.getString(1);
String name = res.getString("name");
String sex = res.getString("sex");
String brithday = res.getString("brithday");
System.out.print("编号:" + id);
System.out.print(" 姓名:" + name);
System.out.print(" 性别:" + sex);
System.out.println(" 生日:" + brithday);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
下面是报错信息:
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(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at UpdateStu.getConnection(UpdateStu.java:12)
at UpdateStu.main(UpdateStu.java:22)
java.lang.NullPointerException
at UpdateStu.main(UpdateStu.java:24)