首页上一页 1 下一页尾页 1 条记录 1/1页
java项目开发案例全程实录(第二版)----酒店管理系统
发表在Java图书答疑
2014-03-11
是否精华
是
否
版块置顶:
是
否
win7,64位,开发软件:Myeclipse10.0 数据库:SQL Server2005(是从附加数据库导入项目数据库) Myeclipse10.0开发的项目中 导入的是SQL Server 2000驱动包。代码如下:
import java.sql.*;
public class JDBC {
private static final String DRIVERCLASS = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_DrinkeryManage";
private static final String USERNAME = "sa";
private static final String PASSWORD = "";
private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
static {// 通过静态方法加载数据库驱动
try {
Class.forName(DRIVERCLASS).newInstance();// 加载数据库驱动
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {// 创建数据库连接的方法
Connection conn = threadLocal.get();// 从线程中获得数据库连接
if (conn == null) {// 没有可用的数据库连接
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);// 创建新的数据库连接
threadLocal.set(conn);// 将数据库连接保存到线程中
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
public static boolean closeConnection() {// 关闭数据库连接的方法
boolean isClosed = true;
Connection conn = threadLocal.get();// 从线程中获得数据库连接
threadLocal.set(null);// 清空线程中的数据库连接
if (conn != null) {// 数据库连接可用
try {
conn.close();// 关闭数据库连接
} catch (SQLException e) {
isClosed = false;
e.printStackTrace();
}
}
return isClosed;
}
}
问题:找不到数据库。(通过本论坛找到一些解决问题的方法,也按照步骤操作,但事与愿违,没成功,还是报错)
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用户 'sa' 登录失败。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSLoginRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(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.mwq.dao.JDBC.getConnection(JDBC.java:29)
at com.mwq.dao.BaseDao.selectSomeValue(BaseDao.java:62)
at com.mwq.dao.Dao.sUserNameOfNotFreeze(Dao.java:23)
at com.mwq.frame.LandFrame.<init>(LandFrame.java:162)
at com.mwq.DrinkeryManage.<init>(DrinkeryManage.java:16)
at com.mwq.DrinkeryManage$1.run(DrinkeryManage.java:38)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mwq.dao.BaseDao.selectSomeValue(BaseDao.java:64)
at com.mwq.dao.Dao.sUserNameOfNotFreeze(Dao.java:23)
at com.mwq.frame.LandFrame.<init>(LandFrame.java:162)
at com.mwq.DrinkeryManage.<init>(DrinkeryManage.java:16)
at com.mwq.DrinkeryManage$1.run(DrinkeryManage.java:38)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
import java.sql.*;
public class JDBC {
private static final String DRIVERCLASS = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static final String URL = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_DrinkeryManage";
private static final String USERNAME = "sa";
private static final String PASSWORD = "";
private static final ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
static {// 通过静态方法加载数据库驱动
try {
Class.forName(DRIVERCLASS).newInstance();// 加载数据库驱动
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {// 创建数据库连接的方法
Connection conn = threadLocal.get();// 从线程中获得数据库连接
if (conn == null) {// 没有可用的数据库连接
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);// 创建新的数据库连接
threadLocal.set(conn);// 将数据库连接保存到线程中
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
public static boolean closeConnection() {// 关闭数据库连接的方法
boolean isClosed = true;
Connection conn = threadLocal.get();// 从线程中获得数据库连接
threadLocal.set(null);// 清空线程中的数据库连接
if (conn != null) {// 数据库连接可用
try {
conn.close();// 关闭数据库连接
} catch (SQLException e) {
isClosed = false;
e.printStackTrace();
}
}
return isClosed;
}
}
问题:找不到数据库。(通过本论坛找到一些解决问题的方法,也按照步骤操作,但事与愿违,没成功,还是报错)
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用户 'sa' 登录失败。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSLoginRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(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.mwq.dao.JDBC.getConnection(JDBC.java:29)
at com.mwq.dao.BaseDao.selectSomeValue(BaseDao.java:62)
at com.mwq.dao.Dao.sUserNameOfNotFreeze(Dao.java:23)
at com.mwq.frame.LandFrame.<init>(LandFrame.java:162)
at com.mwq.DrinkeryManage.<init>(DrinkeryManage.java:16)
at com.mwq.DrinkeryManage$1.run(DrinkeryManage.java:38)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.mwq.dao.BaseDao.selectSomeValue(BaseDao.java:64)
at com.mwq.dao.Dao.sUserNameOfNotFreeze(Dao.java:23)
at com.mwq.frame.LandFrame.<init>(LandFrame.java:162)
at com.mwq.DrinkeryManage.<init>(DrinkeryManage.java:16)
at com.mwq.DrinkeryManage$1.run(DrinkeryManage.java:38)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)