2011-10-23 55 views
1

當我嘗試了DAO模式讀取 http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html 後,但我得到的NullPointerException當我運行的servletNullPointerException異常使用DAO類

DAOFactory

package dao; 
import java.sql.SQLException; 
public abstract class DAOFactory { 
// List of DAO types supported by the factory 
public static final int MYSQL = 1; 

public abstract UserDAO getUserDAO() throws SQLException; 

public static DAOFactory getDAOFactory(int whichFactory) { 

switch (whichFactory) { 
    case MYSQL: 
     return new MySQLDAOFactory(); 
    default  : 
     return null; 
} 
} 
} 

MySQLDAOFactory

package dao; 
import java.sql.*; 
public class MySQLDAOFactory extends DAOFactory { 

public MySQLDAOFactory() { 
} 
public static final String DRIVER="com.mysql.jdbc.Driver"; 
public static final String DBURL="jdbc:mysql://localhost:3306/musicompany"; 

public static Connection createConnection() { 
    Connection conn=null; 
    try{ 
     Class.forName(DRIVER); 
     conn = DriverManager.getConnection(DBURL,"root","toor"); 
    }catch(Exception e){} 
    return conn; 
} 
public UserDAO getUserDAO() throws SQLException{ 
    return new MySQLUserDAO(); 
} 
} 

MySQLUserDAO

package dao; 
import java.sql.*; 
import java.util.ArrayList; 
import model.User; 

public class MySQLUserDAO implements UserDAO { 

Connection conn=null; 
Statement s=null; 
public MySQLUserDAO() throws SQLException{ 
    conn = MySQLDAOFactory.createConnection(); 
    s = conn.createStatement(); 
} 

public int insertUser(String fname, String lname) 
{ 
    try{ 
    //code to insertUser 
    }catch(Exception e){} 
    return key; 
} 


public ArrayList<User> selectUsers() { 
    ResultSet rs=null; 
    ArrayList customerList=null; 
    try{ 
     rs =s.executeQuery("select * from users"); 
     customerList = new ArrayList<User>(); 
     while(rs.next()) 
     { 
      customerList.add(new User(rs.getString("name"), rs.getString("pass"), rs.getInt("type"), rs.getString("email"))); 
     } 
     return customerList; 
    }catch(Exception e){} 
    return customerList; 
} 
} 

UserDAO的

package dao; 
import java.util.ArrayList; 
import model.User; 

public interface UserDAO { 
public int insertUser(String fname, String lname); 
public ArrayList<User> selectUsers(); 
} 

model.User.java

此類具有以下字段和相應的存取方法。

String name; 
String pass; 
short type; 
String email; 

我的servlet文件

public class AddRetrieve extends HttpServlet { 
protected void processRequest(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException{ 
    response.setContentType("text/html;charset=UTF-8"); 
    PrintWriter out = response.getWriter(); 
    try { 
     // create the required DAO Factory 
     DAOFactory factoryObj = DAOFactory.getDAOFactory(DAOFactory.MYSQL); 

     // Create a DAO 
     UserDAO custDAO = factoryObj.getUserDAO(); 

     // print existing users 
     ArrayList list = custDAO.selectUsers(); 
     Iterator i = list.iterator(); 
     while(i.hasNext()) 
     { 
      User o = (User)i.next(); 
      out.println(o.getName()); 
     } 

    } catch(Exception e) 
    { 
     out.println(e); 
    } 
    finally {    
     out.close(); 
    } 
} 
+0

這裏是我的SQL表 - 「用戶」名稱\t varchar(10); \t \t \t pass \t varchar(10); \t \t \t type \t tinyint(4); 電子郵件\t varchar(45); \t \t \t id \t int(11)\t PRI auto_increment; – John

+2

你最好學習像Spring這樣的東西,而不是試圖消化Sun關於J2EE模式的着述。真。你的生活會更容易,你的代碼更乾淨。 – Paul

+2

你在servlet中獲得nullpointerexception的位置? – Kal

回答

2

堆棧跟蹤點MySQLUserDAO類和線s = conn.createStatement()

所以,connnull

讓我們來看看你是如何創建conn

public static Connection createConnection() { 
    Connection conn=null; 
    try{ 
     Class.forName(DRIVER); 
     conn = DriverManager.getConnection(DBURL,"root","toor"); 
    }catch(Exception e){} 
    return conn; 
} 

所以,你忽略拋出的任何異常並返回一個null連接,而不是拋出一個異常,從而使代碼立即停止。

你的技術問題是由駕駛者顯然不是在classpath是造成你的真正的設計問題是,除非你是100%肯定它是安全的繼續代碼流你永遠不應該忽略的異常。

這是不必要的方式加載驅動程序每次。只需在類初始化時加載一次。

重寫那塊如下:

static { 
    try{ 
     Class.forName(DRIVER); 
    } catch (ClassNotFoundException e) { 
     throw new ExceptionInInitializerError("Driver " + DRIVER + " missing in classpath", e); 
    } 
} 

public static Connection createConnection() throws SQLException { 
    return DriverManager.getConnection(DBURL, "root", "toor"); 
} 
+0

+1不捕捉異常並繼續。這經常出乎意料地出現。 –

+0

@BalusC,謝謝。你說得對。這是一個驅動問題。 – John

0

我有同樣的問題,因爲這,原來我只是缺少在我的web內容區域使用mysql-connector罐子。所以這需要參考2個地方...

相關問題