我的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是作為工具類寫的,之前用的時候也報過錯!報錯也是和這次類似!也是同樣的位置的問題!
再次謝謝大神的幫助!
這個你單步調試一下看看具體信息吧