2 u012813082 u012813082 于 2013.12.20 15:33 提问

我的sql连接有问题吗?用的是Dwr!看下面代码以及出的问题,请大神们帮我呀!

我的DBManager!用于连接数据库!
package com.qhit.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBManager {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
PreparedStatement ps = null;
DataSource ds = null;
String jndiName = "java:comp/env/jdbc/bk";
private static DBManager db;

private DBManager() throws NamingException {

    Context context = new InitialContext();
    ds = (DataSource) context.lookup(jndiName);

}

public static DBManager getDBManager() throws NamingException {
    if (db != null) {
        return db;
    } else {
        db = new DBManager();
    }
    return db;
}

public int executeUpdate(String sql) throws SQLException {
    conn = ds.getConnection();
    st = conn.createStatement();

    return st.executeUpdate(sql);
}

public ResultSet executeQuery(String sql) throws SQLException {
    conn = ds.getConnection();
    st = conn.createStatement();
    return st.executeQuery(sql);
}

public int excuteUpdateByPrepareStatement(String sql, Object[] objs)
        throws SQLException {
    ps = conn.prepareStatement(sql);
    for (int i = 1; i <= objs.length; i++) {
        ps.setObject(i, objs[i - 1]);

    }
    return ps.executeUpdate();
}

public void close() {
    try {
        if (rs != null) {
            if (!rs.isClosed()) {
                rs.close();
            }
        }
        if (st != null) {
            if (!st.isClosed()) {
                st.close();
            }
        }
        if (ps != null) {
            if (!ps.isClosed()) {
                ps.close();
            }

        }
        if (conn != null) {
            if (!conn.isClosed()) {
                conn.close();
            }
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

我的dao===========

package com.qhit.dao;

import java.sql.SQLException;

import javax.naming.NamingException;

import com.qhit.entity.UserInfo;
import com.qhit.util.DBManager;

public class userDao {
public boolean userNameExist(String uname) {
boolean result = false;
try {
DBManager db = DBManager.getDBManager();
String sql = "select uid from userInfo where uname='" + uname + "'";
result = db.executeQuery(sql).next();
db.close();
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}

public void saveUserInfo(UserInfo ui){
    try {
        DBManager dbManager=DBManager.getDBManager();
        String sql="insert into userInfo(uname, trueName, upass, usex, uphone,uoicq, uphoto, usignName, uemail, uaddress) values(?,?,?,?,?,?,?,?,?,?)";
        Object[] objs = new Object[10];
            objs[0]=ui.getUname();
            objs[1]=ui.getTrueName();
            objs[2]=ui.getUpass();
            objs[3]=ui.isUsex();
            objs[4]=ui.getUphone();
            objs[5]=ui.getUoicq();
            objs[6]=ui.getUphoto();
            objs[7]=ui.getUsignName();
            objs[8]=ui.getUemail();
            objs[9]=ui.getUaddress();
        dbManager.excuteUpdateByPrepareStatement(sql, objs);
        dbManager.close();
    } catch (NamingException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

}

我的Dwr

package com.qhit.dwr;

import com.qhit.dao.userDao;
import com.qhit.entity.UserInfo;

public class userInfoDwr {
userDao udao = new userDao();
public String saveUserInfo(UserInfo ui){
String name=ui.getUname();
if(name=="" || name.length()>20 || ui.getUname().length()<5){
return "您的用户名不是5-20位";
}
if (udao.userNameExist(name)) {

return "用户名已存在,不可用!";
}else{
udao.saveUserInfo(ui);
}
return ui.getUname()+"用户名可用";
}

}

我的js

function checkUp(){
var userinfo={

uname:DWRUtil.getValue('txtname')
};
UserInfoDWR.saveUserInfo(userinfo,function(data){
alert(data);
});

}

Myeclipse报的错误是说我的Connection 已经关闭!

java.sql.SQLException: Connection is closed.

但是如果我将我的dao里面的第一个连接不关闭的时候,在页面输入英文和汉字都好着!可一旦输入汉字的时候,就会抛出一个Error错误!

所以请大神们帮我看下我的DBManager哪里有错误!因为我的DBManager是作为工具类写的,之前用的时候也报过错!报错也是和这次类似!也是同样的位置的问题!

再次谢谢大神的帮助!

1个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.19 11:51
已采纳

这个你单步调试一下看看具体信息吧

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!