public void insertPhone(PhoneSearchVo vo) {
db = new Dbconn();
PreparedStatement ps = null;
String sql = "insert into phonesearch(userNum,phoneNum,times) values(?,?,?)"; try {
ps = db.getConn().prepareStatement(sql);
ps.setString(1, vo.getUserNum());
ps.setString(2, vo.getPhoneNum());
ps.setString(3, vo.getTimes()); ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
} } }处理类是这样的,然后传入值是这样的:<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<jsp:directive.page import="com.message.model.PhoneModel"/>
<jsp:useBean id="vo" class="com.message.view.PhoneSearchVo" scope="page" />
<jsp:setProperty name="vo" property="*" />
<%--jsp:useBean id="in" class="com.message.model.PhoneModel" /--%>
<html>
<head>
<title>AddProcess</title>
</head>
<body>
<%
PhoneModel pm = new PhoneModel();
vo.getUserNum();
vo.getPhoneNum();
vo.getTimes();
pm.insertPhone(vo);
%>
<script type="text/javascript">
alert("添加成功!");
</script>
<jsp:forward page="phonelist.jsp"></jsp:forward>
</body>
</html>
但是怎么也传不进去值,一直报告null值错误?请问是怎么回事?
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at com.message.model.PhoneModel.insertPhone(PhoneModel.java:25)
at org.apache.jsp.phoneadd_jsp._jspService(org.apache.jsp.phoneadd_jsp:75)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:322)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
(1)FUNCTION中第一行打印vo属性,看有没有值;
(2)JSP中显示vovo属性,看有没有值。。
不知道你这几句话是做什么用的。
vo.getUserNum();
vo.getPhoneNum();
vo.getTimes();不过像这种插入异常,可以在插入的方法里面打印相应的信息来判段错误出在哪里!
vo.getUserNum();
vo.getPhoneNum();
vo.getTimes();这三句话没有用,是写错了,但是pm.insertPhone(vo);这句是插入值的 为什么插不进去呢?
com.message.model.PhoneModel的代码:package com.message.model;import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import com.message.common.Dbconn;
import com.message.view.PhoneSearchVo;public class PhoneModel {
Dbconn db; public void insertPhone(PhoneSearchVo vo) {
db = new Dbconn();
PreparedStatement ps = null;
String sql = "insert into phonesearch(userNum,phoneNum,times) values(?,?,?)"; try {
ps = db.getConn().prepareStatement(sql);
ps.setString(1, vo.getUserNum());
ps.setString(2, vo.getPhoneNum());
ps.setString(3, vo.getTimes()); ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
} } } public ArrayList selectAll() {
db = new Dbconn();
PhoneSearchVo vo = null;
ArrayList a = new ArrayList();
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from phonesearch"; try {
stmt = db.getConn().createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
vo = new PhoneSearchVo();
vo.setId(rs.getInt(1));
vo.setUserNum(rs.getString(2));
vo.setPhoneNum(rs.getString(3));
vo.setTimes(rs.getString(4));
a.add(vo); }
} catch (SQLException e) {
e.printStackTrace();
} return a;
}
public void deletePhoneNum(int id) {
db = new Dbconn();
Statement stmt = null; String phoneSql = "delete from phonesearch where id=" + id; try {
stmt.executeUpdate(phoneSql);
System.out.println(phoneSql); } catch (SQLException e) {
e.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(db != null)
db.closeConn(); } } public ArrayList selectPage(int count, int page) {
db = new Dbconn();
PhoneSearchVo vo = null;
ArrayList a = new ArrayList();
Statement stmt = null;
ResultSet rs = null;
String sql = "select top " + count + " * from phonesearch where id not in(select top " + page * count + " id from phonesearch order by id desc) order by id desc"; try {
stmt = db.getConn().createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
vo = new PhoneSearchVo();
vo.setId(rs.getInt(1));
vo.setUserNum(rs.getString(2));
vo.setPhoneNum(rs.getString(3));
vo.setTimes(rs.getString(4));
a.add(vo); }
} catch (SQLException e) {
e.printStackTrace();
} return a;
} public int getPageCount() {
db = new Dbconn();
int pagecount = 0;
int count = 0;
Statement stmt = null;
ResultSet rs = null;
String sql = "select count(*) from phonesearch"; try {
stmt = db.getConn().createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
count = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
if (count % 2 == 0) {
pagecount = count / 2;
} else {
pagecount = (count / 2) + 1; }
return pagecount; }
}
public void insertPhone(PhoneSearchVo vo) {
db = new Dbconn();
PreparedStatement ps = null;
String sql = "insert into phonesearch(userNum,phoneNum,times) values(?,?,?)"; try {
ps = db.getConn().prepareStatement(sql);
ps.setString(1, vo.getUserNum());
ps.setString(2, vo.getPhoneNum());
ps.setString(3, vo.getTimes()); ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
} } }
这个函数里面增加几个测试代码。在try之前,增加一个system.out.print(vo.getUserNum());
如果在这里的值是空的,那么原因就是从页面就没有吧vo这个对象传进来。然后再在页面上作此处理,看看是不是页面得到了需要得值,这样就能判断出来错误在哪里了。
<jsp:useBean id="vo" class="com.message.view.PhoneSearchVo" scope="page" />
<jsp:setProperty name="vo" property="*" />
没有起作用,那几个属性你还得给他们赋值,
比如 vo.getUserNum(要设的值);
vo.getPhoneNum(要设的值);
vo.getTimes(要设的值);
让后再调用 pm.insertPhone(vo);
这样才能插入成功
这是我的,你稍等下,我打包给你!
虽然不怎么严谨,毕竟你还是能看懂吧
关键在于问题出在哪里的细节问题
String id = request.getParameter("userNum");
你从phone.jsp没有把值提交到phoneAdd.jsp页面中,你在看看
你知道问题出在哪里了吗?
你犯了低级的错误就是
get和set方法弄错了
String id = request.getParameter("usernum");
System.out.println(id);
String phoneNum = request.getParameter("phonenum");
System.out.println(phoneNum);
String times = request.getParameter("times");
System.out.println(times);
PhoneModel pm = new PhoneModel();
PhoneSearchVo db = new PhoneSearchVo();
db.setUsernum(id);
db.setPhonenum(phoneNum);
db.setTimes(times);
要不然,你光提交个页面过去,哪里来的值呢?
试试看吧
这才是你要选择的!
把所有的处理都提交到servlet当中去,看起来舒服多了。