机智的哈路
2017-12-01 10:58
采纳率: 100%
浏览 1.5k

学习Java web的小菜鸟求问一个问题!web信息想传入数据库一直出错

按照网上的教程配置好了MySQL数据库连接池。
1.Tomcat的context.xml文件下加了
<Resource name="jdbc/mysql"

auth="Container"

type="javax.sql.DataSource"

driverClassName="com.mysql.jdbc.Driver"

username="root"

password="123456"

maxActive="100"

maxIdle="30"

maxWait="10000"
url="jdbc:mysql://localhost:3306/blog "/>

2.在工程和Tomcat的lib文件夹下都加入了JAR包图片说明

3.servlet源程序:
package cn.com.seven.blog;

import java.io.IOException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BlogServlet extends HttpServlet {
private static final long serialVersionUID=1L;

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doPost(request,response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String title=request.getParameter("title");
    String content=request.getParameter("content");
    String categoryId=request.getParameter("category");

    //System.out.println(title);
    //System.out.println(content);
    //System.out.println(categoryId);

    DataSource ds = null;

    try {
        // 通过在context.xml文件,设定的数据源对象的名字,获取数据源对象
        Context context = new InitialContext();
        ds = (DataSource) context.lookup("java:/comp/env/jdbc/mysqlds");
    } catch (Exception e) {
        System.out.println("获取数据源时出错");
    }
            Connection conn;
        try {
            conn=ds.getConnection();
            String sql = "insert into blog (title,content,category_id,createdtime) values (?,?,?,now())";
            PreparedStatement pstmt =conn.prepareStatement(sql);
            pstmt.setString(1, title);
            pstmt.setString(2, content);
            pstmt.setInt(3,Integer.parseInt(categoryId));

            int result=pstmt.executeUpdate();

            System.out.println(result);

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   

}
 }

然后在界面中输入信息后报错:
获取数据源时出错
十二月 01, 2017 6:50:08 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [BlogServlet] in context with path [/blog] threw exception
java.lang.NullPointerException
at cn.com.seven.blog.BlogServlet.doPost(BlogServlet.java:49)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2516)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2505)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

【不知道什么原因,请大神帮帮我~~~】

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

16条回答 默认 最新

相关推荐 更多相似问题