我将TomCat 8.0服务器,h2数据库和IntelliJ IDEA 14用于我的WebApplication,但是当我运行app(按钮事件)时,我收到了
java.lang.NullPointerException image. 我已成功连接到数据库(已经过测试):
public class DbConnection {
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:tcp://localhost/~/Students";
private static final String USER_NAME = "doncho";
private static final String PASS = "";
private static DbConnection instance;
private static Connection conn;
private DbConnection(){
}
public static DbConnection getInstance(){
if(instance == null){
instance = new DbConnection();
}
return instance;
}
public Connection getConnect() throws SQLException{
Connect();
return conn;
}
private void Connect() throws SQLException {
if (conn == null) {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
System.out.println("Driver isn't found");
e.printStackTrace();
}
DriverManager.getConnection(URL, USER_NAME, PASS);
}
}
public void Disconnect() throws SQLException{
if(conn != null){
conn.close();
conn=null;
}
}
我创建了类RegisterDao
,它必须将数据插入到名为“Name”的表中,并且只有一列 - “name”
public class RegisterDAO {
Connection conn = null;
PreparedStatement state = null;
public StudentBean registerStudent(String userName) throws SQLException{
StudentBean result = null;
String sql = "INSERT INTO Name VALUES(?)";
try {
conn = DbConnection.getInstance().getConnect();
state = conn.prepareStatement(sql);
state.setString(1, userName);
state.execute();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if(state != null){
state.close();
}
}
DbConnection.getInstance().Disconnect();
return result;
}}
这是我的类(java bean)StudentBean
@javax.ejb.Stateless(name = "StudentEJB")
public class StudentBean {
private String name;
public StudentBean() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}}
我使用这些类通过servlet在表“Name”中插入数据 - RegisterServlet
public class RegisterServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
Connection conn = DbConnection.getInstance().getConnect();
System.out.println("It Works");
} catch (SQLException e) {
e.printStackTrace();
}
String username = request.getParameter("usernameReg");
StudentBean student;
try {
student = new RegisterDAO().registerStudent(username);
request.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
} catch (SQLException e) {
e.printStackTrace();
}
try {
DbConnection.getInstance().Disconnect();
} catch (SQLException e) {
e.printStackTrace();
}
}}
但是我得到了错误。我很困惑,因为我在Eclipse中测试了源码并且它在那里工作,但是在IntelliJ中它没有。 我是IntelliJ的新手,所以我希望有更多经验的人会给我一个解决问题的想法。
P.S。来自servlet的System.out.println("It Works");
可以正常工作,我保证将servlet称为“index.jsp”正确。
对不起这篇长篇文章和最好的问候, D.Balamjiev。
答案 0 :(得分:0)
它适用于Eclipse,您确定吗?
DriverManager.getConnection(URL, USER_NAME, PASS);
您尚未将其分配给conn
,因此conn
仍为null
,然后conn.prepareStatement
会引发异常,因为conn
为null
。我认为你需要做的只是:
conn = DriverManager.getConnection(URL, USER_NAME, PASS);