<body><hr>
<html:form action="/student">
学号: <html:text property="studNo"/>
<html:errors property="studNo"/><br/>
姓名: <html:text property="name"/>
<html:errors property="name"/><br/>
年龄: <html:text property="age"/>
<html:errors property="age"/><br/>
性别: <html:select property="sex">
<html:option value="男"></html:option>
<html:option value="女"></html:option>
</html:select>
<br>
<html:submit/><html:cancel/>
</html:form>
</body>
---------------------------------------------------------------------------------------------
studentAction.java
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
StudentForm studentForm = (StudentForm) form;
String sex=studentForm.getSex();
String age=studentForm.getAge();
String name=studentForm.getName();
String studNo=studentForm.getStudNo();
ArrayList arr=new ArrayList();
arr.add(studNo);
arr.add(name);
arr.add(sex);
arr.add(age);
StudentDAO da=new StudentDAO();
boolean mark=false;
try {
mark = da.saveData(arr);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String prompt;
if(mark)prompt="Success";
else prompt="Fail";
return mapping.findForward(prompt);
}
-------------------------------------------------------------------------
studentDAO.java
Connection conn;
PreparedStatement ps;
ResultSet rs;
public StudentDAO(){
}
public boolean saveData(ArrayList arr) throws SQLException, InstantiationException, ClassNotFoundException, IllegalAccessException{
conn=dbPool.getConnection();
String studNo=(String)arr.get(0);
String name=(String)arr.get(1);
String sex=(String)arr.get(2);
String age=(String)arr.get(3);
try{
String sql="insert into t_student values(?,?,?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1, studNo);
ps.setString(2, name);
ps.setString(3, sex);
ps.setString(4, age);
int rowCount=ps.executeUpdate();
dbPool.dbClose(conn, ps, rs);
return true;
}catch(SQLException e){return false;}
}
--------------------------------------------------------------
dbpool.java
public static synchronized Connection getConnection() throws SQLException, InstantiationException, ClassNotFoundException, IllegalAccessException
{
String url ="jdbc:mysql://localhost:3306/xxxx"; //数据库连接字符串
Class.forName("org.gjt.mm.mysql.Driver").newInstance(); //加载驱动程序
return(DriverManager.getConnection(url,"root","xxxxx")); //建立连接
}
7 个解决方案
#1
错误是什么???
没看出什么!
没看出什么!
#2
把错误贴上来看看
#3
root cause
java.lang.NullPointerException
studentInfo.dbPool.dbClose(dbPool.java:43)
studentInfo.StudentDAO.saveData(StudentDAO.java:26)
com.yourcompany.struts.action.StudentAction.execute(StudentAction.java:55)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
数据内容已经提交到数据库了,但是显示错误
java.lang.NullPointerException
studentInfo.dbPool.dbClose(dbPool.java:43)
studentInfo.StudentDAO.saveData(StudentDAO.java:26)
com.yourcompany.struts.action.StudentAction.execute(StudentAction.java:55)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
数据内容已经提交到数据库了,但是显示错误
#4
dbPool.java:43
43行是啥代码,出现空指针了
43行是啥代码,出现空指针了
#5
性别: <html:select property="sex">
<html:option value="男"> </html:option>
<html:option value="女"> </html:option>
</html:select>
---------------------------------------
改为
--------------------------------------------
性别: <html:select property="sex">
<html:option value="男" selected> </html:option>
<html:option value="女"> </html:option>
</html:select>
<html:option value="男"> </html:option>
<html:option value="女"> </html:option>
</html:select>
---------------------------------------
改为
--------------------------------------------
性别: <html:select property="sex">
<html:option value="男" selected> </html:option>
<html:option value="女"> </html:option>
</html:select>
#6
错误信息说的太笼统了,设断点调试吧
#7
你的rs是控制,只声明,ResultSet rs,没有进行创建或赋值操作,所以在dbPool.dbClose如果调用
rs.close()
就会出现空指针异常。
rs.close()
就会出现空指针异常。
#1
错误是什么???
没看出什么!
没看出什么!
#2
把错误贴上来看看
#3
root cause
java.lang.NullPointerException
studentInfo.dbPool.dbClose(dbPool.java:43)
studentInfo.StudentDAO.saveData(StudentDAO.java:26)
com.yourcompany.struts.action.StudentAction.execute(StudentAction.java:55)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
数据内容已经提交到数据库了,但是显示错误
java.lang.NullPointerException
studentInfo.dbPool.dbClose(dbPool.java:43)
studentInfo.StudentDAO.saveData(StudentDAO.java:26)
com.yourcompany.struts.action.StudentAction.execute(StudentAction.java:55)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
数据内容已经提交到数据库了,但是显示错误
#4
dbPool.java:43
43行是啥代码,出现空指针了
43行是啥代码,出现空指针了
#5
性别: <html:select property="sex">
<html:option value="男"> </html:option>
<html:option value="女"> </html:option>
</html:select>
---------------------------------------
改为
--------------------------------------------
性别: <html:select property="sex">
<html:option value="男" selected> </html:option>
<html:option value="女"> </html:option>
</html:select>
<html:option value="男"> </html:option>
<html:option value="女"> </html:option>
</html:select>
---------------------------------------
改为
--------------------------------------------
性别: <html:select property="sex">
<html:option value="男" selected> </html:option>
<html:option value="女"> </html:option>
</html:select>
#6
错误信息说的太笼统了,设断点调试吧
#7
你的rs是控制,只声明,ResultSet rs,没有进行创建或赋值操作,所以在dbPool.dbClose如果调用
rs.close()
就会出现空指针异常。
rs.close()
就会出现空指针异常。