HTTP状态500 servlet java + mysql

时间:2015-01-02 14:00:58

标签: java mysql

我在java servlet中使用过Mysql连接。在Intellij Idea中安装了Mysql连接器驱动程序,db“new_schema”,创建了表“users”。我在那里加了1个便条。所以我想在web-brouser中打印这个笔记的字段。出现了htto status 500错误。


HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
    DBconnect.getData(DBconnect.java:34)
    mainClass.doGet(mainClass.java:25)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/8.0.15 logs.

________

帮帮我PLZ!

MainClass

import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.awt.print.PrinterException;
    import java.io.IOException;
    import java.io.PrintWriter;


    /**
     * Created by Yuriy on 01.01.2015.
     */
    @WebServlet(name = "mainClass")
    public class mainClass extends HttpServlet {


        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            DBconnect db = new DBconnect();
            PrintWriter y = response.getWriter();
                db.getData();
                y.println(db.name);
                y.println(db.age);
            y.close();

        }
    }

DBconnect类

import java.sql.Statement;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Yuriy on 02.01.2015.
 */
public class DBconnect  implements Serializable{
    private Connection con;
    private Statement st;
    private ResultSet res;
    public String name;
    public int age;
    public DBconnect(){

            try{
                Class.forName("com.mysql.jdbc.Driver");
                con=DriverManager.getConnection("jdbc:mysql://localhost:3306/new_schema","shipovalovyuriy","12321098");
                st = con.createStatement();
            }catch(Exception ex){
                System.err.println ("Cannot connect to database server");
                ex.printStackTrace();
            }

    }


    public void getData(){
        String query = "select * from users";
        try {
            res = st.executeQuery(query);
            setName(res.getString("name"));
            setAge(res.getInt("age"));
        } catch (SQLException e) {
            e.getErrorCode();
        }
    }
    public void setName(String x){
        name = x;
    }
    public void setAge(int y){
        age = y;
    }
}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>

        <servlet-name>FirstSevlet</servlet-name>
        <servlet-class>mainClass</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FirstSevlet</servlet-name>
        <url-pattern>/servlet</url-pattern>
    </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:0)

您需要将Mysql驱动程序连接器.jar文件复制到Tomcat / lib目录