當我嘗試了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();
}
}
這裏是我的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
你最好學習像Spring這樣的東西,而不是試圖消化Sun關於J2EE模式的着述。真。你的生活會更容易,你的代碼更乾淨。 – Paul
你在servlet中獲得nullpointerexception的位置? – Kal