日期:2014-05-20  浏览次数:20793 次

获取数据库连接出错了
package org.mmxbb.exam.dao;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;

import org.mmxbb.exam.bean.Examinee;
import org.mmxbb.exam.business.LogonForm;
import org.mmxbb.exam.business.examinee.ExamineeActionForm;
import org.mmxbb.exam.business.program.RandomDistributeExamPaperForm;
import org.mmxbb.exam.util.DBConn;


public class ExamineeDAO {
DBConn dbconn = null;
private Connection conn = null;

private int rowCount;
private int pageCount;
private int length;
private String pagestr;
private String conditionStr = "";


private static final String IS_USER =
"SELECT password FROM EX_EXAMINEE WHERE examinee_id = ?";
private static final String FIND_BY_KEY =
"SELECT * FROM EX_EXAMINEE WHERE examinee_id = ?";
/**
* get a connection from a DB pool
* @return Connection
*/
public ExamineeDAO() {
try {
dbconn = new DBConn();
System.out.println("---------ExamineeDAO() -----------------------------------");
conn = dbconn.getConnection();
System.out.println("---------ExamineeDAO() ----已获得数据库的连接了-------------------------------");
} catch (Exception ex) {
ex.printStackTrace();
}
}

public boolean isUser(LogonForm logonForm) {
PreparedStatement pstmt = null;
ResultSet rs = null;
boolean isUser = false;

try {
System.out.println("-----ExamineeDAO.java---isUser()--------------"+conn);
pstmt = conn.prepareStatement(IS_USER);
pstmt.setString(1, logonForm.getUserName());
rs = pstmt.executeQuery();
if (rs.next()) {
if (rs.getString(1).equals(logonForm.getPassword())) {
isUser = true;
}
}
} catch (SQLException ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
rs = null;
pstmt.close();
pstmt = null;
conn.close();
conn = null;
} catch (SQLException ex1) {
ex1.printStackTrace();
}
}
return isUser;
}
}



tomcat 控制台出现以下信息:

2008-4-15 14:36:03 org.apache.catalina.core.ApplicationContext log
信息: HTMLManager: list: Listing contexts for virtual host 'localhost'
---------ExamineeDAO() -----------------------------------
---------ExamineeDAO() ----已获得数据库的连接了-------------------------------
-----ExamineeDAO.java---isUser()--------------null


请问为什么我得不到connection?得到却是null


------解决方案--------------------
conn = dbconn.getConnection(); 

那就去你的DbCon 类啊!为什么他没有返回Connection 而是 null


------解决方案--------------------
如果数据连接的代码有误,要查看是不是你所用的桥架不对呀,那就是IDE的DB conn 工具啦,myeclipse 用DB browser啦,jbuilder 也有可视化连接验证的界面啦,