求助:这种错误如何解决?

zhjxzhj 2005-06-14 11:09:53

tomcat工程测试的时候出现下面错误怎么解决?


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)
...全文
117 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxshandle 2005-06-14
  • 打赏
  • 举报
回复
是sokect连接错误,你看一下你的socket连接是否正确
zhjxzhj 2005-06-14
  • 打赏
  • 举报
回复
SQL没问题呀 数据库名称 用户名、密码都和tomcat文件中的配置一致。
服务也启动了呀
darkattack 2005-06-14
  • 打赏
  • 举报
回复
估计你SQL SERVER连接有问题,连接串、数据库名称、用户名密码是否正确?
SQL SERVER服务是否已启动?
zhjxzhj 2005-06-14
  • 打赏
  • 举报
回复
public class SqlserverDBConnection {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
private DBConnectionManager dcm=null;

void init() {
dcm = DBConnectionManager.getInstance();
conn = dcm.getConnection("sqlserver");
}

/**
* 构造数据库的连接和访问类
*/
public SqlserverDBConnection() throws Exception {
init();
stmt = conn.createStatement();
}

public SqlserverDBConnection(int resultSetType, int resultSetConcurrency)
throws Exception {
init();
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
}

/**
* 构造数据库的连接和访问类
* 预编译SQL语句
* @param sql SQL语句
*/
public SqlserverDBConnection(String sql) throws Exception {
init();
this.prepareStatement(sql);
}

public SqlserverDBConnection(String sql, int resultSetType, int resultSetConcurrency)
throws Exception {
init();
this.prepareStatement(sql, resultSetType, resultSetConcurrency);
}

/**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection() {
return conn;
}

/**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}

public void prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
prepstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}

/**
* 设置对应值
*
* @param index 参数索引
* @param value 对应值
*/
public void setString(int index,String value) throws SQLException {
prepstmt.setString(index, value);
}
public void setInt(int index,int value) throws SQLException {
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws SQLException {
prepstmt.setBoolean(index,value);
}
public void setDate(int index,Date value) throws SQLException {
prepstmt.setDate(index,value);
}
public void setTimestamp(int index,Timestamp value) throws SQLException {
prepstmt.setTimestamp(index,value);
}
public void setLong(int index,long value) throws SQLException {
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException {
prepstmt.setFloat(index,value);
}
public void setBytes(int index,byte[] value) throws SQLException{
prepstmt.setBytes(index,value);
}



public void clearParameters()
throws SQLException
{
prepstmt.clearParameters();
prepstmt=null;
}
/**
* 返回预设状态
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement() {
return stmt;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}

public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else return null;
}

/**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* 关闭连接
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
if (conn!=null)
{

dcm.freeConnection("sqlserver",conn);

}

}
}
zhjxzhj 2005-06-14
  • 打赏
  • 举报
回复
package com.rannuo.connectdb;

import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.log4j.*;
/**
* <p>Title: 后台管理系统</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005-03-12</p>
* <p>Company: 然诺信科</p>
* @author ygy
* @version 1.0
*/

public class DBConnectionManager {
static private DBConnectionManager instance; // 唯一实例
static private int clients;
static Logger log = Logger.getLogger(DBConnectionManager.class);

private Vector drivers = new Vector();
private Hashtable pools = new Hashtable();

/**
* 返回唯一实例.如果是第一次调用此方法,则创建实例
*
* @return DBConnectionManager 唯一实例
*/
static synchronized public DBConnectionManager getInstance() {
if (instance == null) {
instance = new DBConnectionManager();
}
clients++;
return instance;
}


/**
* 建构函数私有以防止其他对象名重复
*/
private DBConnectionManager() {
init();
}

/**
* 将连接对象返回给由名字指定的连接池
*
* @param name 在属性文件中定义的连接池名字
* * @param con 连接对象名
*/
public void freeConnection(String name, Connection con) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
pool.freeConnection(con);
}
}

/**
* 获得一个可用的(空闲的)连接.如果没有可用连接,且已有连接数小于畜连接数
* 限制,则创建并返回新连接
*
* @param name 在属性文件中定义的连接池名字
* @return Connection 可用连接或null
*/
public Connection getConnection(String name) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection();
}
return null;
}

/**
* 获得一个可用连接.若没有可用连接,且已有连接数小于畜连接数限制,
* 则创建并返回新连接.否则将在指定的时间内等待其他连接释放
*
* @param name 连接池名字
* @param time 以毫秒为单位
* @return Connection 可用连接或null
*/
public Connection getConnection(String name, long time) {
DBConnectionPool pool = (DBConnectionPool) pools.get(name);
if (pool != null) {
return pool.getConnection(time);
}
return null;
}

/**
* 关闭所有连接,撤销驱动程序的注册
*/
public synchronized void release() {
// 等待直到某一个客户程序调用
if (--clients != 0) {
return;
}

Enumeration allPools = pools.elements();
while (allPools.hasMoreElements()) {
DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();
pool.release();
}
Enumeration allDrivers = drivers.elements();
while (allDrivers.hasMoreElements()) {
Driver driver = (Driver) allDrivers.nextElement();
try {
DriverManager.deregisterDriver(driver);
if (log.isInfoEnabled())
log.info("撤消jDBC驱动程序" + driver.getClass().getName()+"的注册");
}
catch (SQLException e) {
log.error("无法撤销下列JDBC驱动程序的注册 " + driver.getClass().getName());
}
}
}

/**
* 根据指定属性创建连接池实例.
*
* @param props 连接池属性
*/
private void createPools(Properties props) {
Enumeration propNames = props.propertyNames();
while (propNames.hasMoreElements()) {
String name = (String) propNames.nextElement();
if (name.endsWith(".driver")) {
String poolName = name.substring(0, name.lastIndexOf("."));
String driverClassName = props.getProperty(poolName + ".driver");
if (driverClassName == null) {
log.error("没有为连接池" + poolName + "指定Driver");
continue;
}
String url = props.getProperty(poolName + ".url");
String user = props.getProperty(poolName + ".user");
String password = props.getProperty(poolName + ".password");
String maxconn = props.getProperty(poolName + ".maxconn", "0");
int max = 0; //max=0 最大畜连接数不受限制
try {
max = Integer.valueOf(maxconn).intValue();
Driver driver = (Driver)Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
drivers.addElement(driver);
if (log.isInfoEnabled())
log.info("成功注册JDBC驱动程序"+ driverClassName);
}
catch (NumberFormatException ne) {
log.error("最大畜连接数限制: " + maxconn + " .连接池: " + poolName);
}
catch (Exception e) {
log.error(e.getMessage());
}

DBConnectionPool pool = new DBConnectionPool(poolName,
url,
user,
password,
max);
pools.put(poolName, pool);
if (log.isInfoEnabled())
log.info("成功创建连接池" + poolName);
}
}
}

/**
* 读取属性蛠E沙跏蓟?
*/
private void init() {
InputStream is = getClass().getResourceAsStream("/db.properties");
Properties dbProps = new Properties();
try {
dbProps.load(is);
}
catch (Exception e) {
log.error("不能读取属性文件. 因为b.properties在CLASSPATH指定的路径中");
return;
}
createPools(dbProps);
}



}
zhjxzhj 2005-06-14
  • 打赏
  • 举报
回复
连接池代码如下:public class DBConnectionPool {
static Logger log = Logger.getLogger(DBConnectionPool.class);
private int checkedOut;
private Vector freeConnections = new Vector();
private int maxConn;
private String name;
private String password;
private String URL;
private String user;

/**
* 创建新的连接池
*
* @param name 连接池名字
* @param URL 数据库的JDBC URL
* @param user 数据库帐号 null
* @param password 密码 null
* @param maxConn 此连接池允最大的畜连接数
*/
public DBConnectionPool(String name, String URL, String user, String password,
int maxConn) {
this.name = name;
this.URL = URL;
this.user = user;
this.password = password;
this.maxConn = maxConn;
}

/**
* 将不再使用的连接返回给连接池
*
* @param con 客户程序释放的连接
*/
public synchronized void freeConnection(Connection con) {

// 将指定连接加到向量末尾
freeConnections.addElement(con);
checkedOut--;
notifyAll();
}

/**
* 从连接池获得一个可用连接.如没有空闲的连接且当前连接数小于畜连接
* 限制,则创建新连接.如原来登记为可用的连接不再有效,则从向量删除之,
* 然后递归调用自己以尝试新的可用连接.
*/
public synchronized Connection getConnection() {
Connection con = null;
if (freeConnections.size() > 0) {
// 获取向量中第一个可用连接
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try {
if (con.isClosed()) {
if (log.isInfoEnabled())
log.info("从连接池" + name+"删除一个无效连接");
// 递归调用自己,尝试再次获取可用连接
con = getConnection();
}
}
catch (SQLException e) {
if (log.isInfoEnabled())
log.info("从连接池" + name+"删除一个无效连接");
// 递归调用自己,尝试再次获取可用连接
con = getConnection();
}
}
else if (maxConn == 0 || checkedOut < maxConn) {
con = newConnection();
}
if (con != null) {
checkedOut++;
}
notifyAll();
return con;
}

/**
* 从连接池获取可用连接.可以指定客户程序能够等待的时间
* 参见前一个getConnection()方法.
*
* @param timeout 以毫秒为时间限制
*/
public synchronized Connection getConnection(long timeout) {
long startTime = new Date().getTime();
Connection con;
while ((con = getConnection()) == null) {
try {
wait(timeout);

}
catch (InterruptedException e) {}
if ((new Date().getTime() - startTime) >= timeout) {
// wait()返回的原因是超时
return null;
}
}
return con;
}

/**
* 关闭所有连接
*/

public synchronized void release() {
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements()) {
Connection con = (Connection) allConnections.nextElement();
try {
con.close();
if (log.isInfoEnabled())
log.info("关闭连接池" + name+"中的一个连接");
}
catch (SQLException e) {
//log(e, "无法关闭连接池" + name+"中的连接");
}
}
freeConnections.removeAllElements();
}

/**
* 创建新的连接
*/
private Connection newConnection() {
Connection con = null;
try {
if (user == null) {
con = DriverManager.getConnection(URL);
}
else {
con = DriverManager.getConnection(URL, user, password);
}
if (log.isInfoEnabled())
log.info("连接池" + name + "创建一个新的连接");
}
catch (SQLException e) {
log.error("无法创建下列URL的连接: " + URL, e);
e.printStackTrace();
return null;
}
return con;
}
}
darkattack 2005-06-14
  • 打赏
  • 举报
回复
贴你的代码,连接数据库那段

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧