日期:2014-05-20  浏览次数:20915 次

请各位棒棒忙看下了
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("name");
String price = request.getParameter("price");
String product = request.getParameter("product");
String displacement=request.getParameter("displacement");
String level=request.getParameter("level");

String sql = "insert into newCars(name,price,product,displacement,level) values (?,?,?,?,?)";
Object params[] = {name,price,product,displacement,level};
QueryRunner qr = DbHelper.getQueryRunner();

try {
qr.update(sql,params);
} catch (SQLException e) {
e.printStackTrace();
}
运行后出现这样的错误:
Servlet.service() for servlet newCarsServlet threw exception
java.lang.NullPointerException
at com.mysql.jdbc.ResultSetMetaData.getField(ResultSetMetaData.java:397)
at com.mysql.jdbc.ResultSetMetaData.getColumnType(ResultSetMetaData.java:275)
at com.mysql.jdbc.MysqlParameterMetadata.getParameterType(MysqlParameterMetadata.java:80)
at org.apache.commons.dbutils.QueryRunner.fillStatement(QueryRunner.java:192)
at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:595)
at org.apache.commons.dbutils.QueryRunner.update(QueryRunner.java:655)
at auto.repair.newCars.newCarsServlet.add(newCarsServlet.java:134)
at auto.repair.newCars.newCarsServlet.doPost(newCarsServlet.java:34)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)


------解决方案--------------------
你确定在游览器端表单的元素的名字是否分别为name,price,product,displacement,level
或者类似:url?name=xxx&price=xxx&.... 参数都写全了?
还有一点是从request传来的参数都得判断是否为null,这是肯定的,没有判断直接用。是不合理的!
------解决方案--------------------
数据库中没有对应的字段。
------解决方案--------------------
没看到LZ的连接数据库的操作。看下我的方式:
Java code

try {
    QueryRunner qr = new QueryRunner();
    Connection conn = DBConnection.getConn();
    qr.update(sql, params);
    } catch (SQLException e) {
      e.printStackTrace();
    }


DBConnection.java
public static Connection getConn()
    {
        Connection conn=null;
        try
        {
           Class.forName("com.mysql.jdbc.Driver");
        }
        catch(ClassNotFoundException ex)
        {
            ex.printStackTrace();
        }
        try
        {
            
           String url="jdbc:mysql://localhost:3306/test";    //本人用的是mysql数据库                                      
           conn=DriverManager.getConnection(url,"root","root"); 
           
        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
        }
        return conn;
    }