简体   繁体   English

JAVA servlet从数据库返回客户端获取结果

[英]JAVA servlet get result from database return to client

So as my title says i am having problems with my server side application, 所以我的标题说我的服务器端应用程序有问题,

I have a class with the getter and setter methods called LocationBean 我有一个名为LocationBean的getter和setter方法的类

public class LocationBean {

    private String location;
    private String ReaderID;

    public String getLocation () {
        return location;
    }

    public String getReaderID () {
        return ReaderID;
    }

    public void setLocation (String newlocation) {
        location = newlocation;
    }

    public void setReaderID (String newReaderID) {
        ReaderID = newReaderID;
    }
}

then i have my main servlet class called getlocation 然后我有一个名为getlocation的主servlet类

    package location;

import java.io.*;
import java.util.List;

import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

public class getlocation extends HttpServlet {

    String ReaderID;
    String locs;


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

        ReaderID = request.getParameter("readerid");
        locs = LocationDAO.getloc(ReaderID);

        JSONObject loc = new JSONObject();

        loc.put("Location", locs);

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();

        out.print(loc);
    }

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

Last is my DAO class called LocationDAO 最后是我的DAO类名为LocationDAO

package location;
import java.sql.*;

public class LocationDAO {
    public static String getloc(String ReaderID) {
        // TODO Auto-generated method stub
        Connection conn =null; // Create connection object
        Statement stmt = null;
        ResultSet rs1 = null;
        String database = ""; // Name of database
        String user = ""; // 
        String password = "";
        String url = "jdbc:mysql://:3306/" + database;
        String Location = null;
        String Rid = ReaderID;    

        try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch(Exception e) {
            System.err.println(e);
        }

        // connecting to database
        try {
            conn = DriverManager.getConnection(url, user, password);

        } catch(SQLException se) {
            System.err.println(se);
        }
        // Create select statement and execute it

        try {
            String selectSQL = "select Location from `LocationTable` where ReaderID ='"+Rid + "'";
            stmt = conn.createStatement();

            rs1 = stmt.executeQuery(selectSQL);
            // Retrieve the results

            if(rs1.next()) {                              
                Location =  rs1.getString("Location");    
            }

            conn.close();
        } catch(SQLException se) {
            System.err.println(se);
        }
        return Location;
    }
}

I have removed the database details such as the url password etc as this is hosted on my own server and would not like it to be posted on the internet. 我删除了数据库详细信息,例如url密码等,因为它托管在我自己的服务器上,不希望它发布在互联网上。 So they problem when data is sent to this is that i get this error message 因此,当数据发送到此时它们会出现问题,即我收到此错误消息

HTTP Status 500 - HTTP状态500 -

type Exception report 类型异常报告

message 信息

description The server encountered an internal error that prevented it from fulfilling this request. description服务器遇到内部错误,导致无法完成此请求。

exception 例外

java.lang.NullPointerException
location.LocationDAO.getloc(LocationDAO.java:39)
location.getlocation.doGet(getlocation.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

if anyone could help in anyway it would be appreciated 如果有人可以提供任何帮助,我们将不胜感激

When this code connects to the database, if there is a problem (and there are many possible problems, like an incorrect url, or a missing driver jar, or the database is down or not reachable) it throws an exception. 当此代码连接到数据库时,如果存在问题(并且存在许多可能的问题,例如错误的URL或缺少的驱动程序jar,或者数据库已关闭或无法访问),则会引发异常。 The exception gets trapped in the catch block surrounding the connection code and is written to System.err. 异常被捕获在连接代码周围的catch块中,并写入System.err。 Then the program proceeds on its way and uses the connection, calling createStatement on it. 然后程序继续前进并使用连接,在其上调用createStatement Since the connection never got initialized it is still null, causing a NullPointerException. 由于连接从未初始化,因此它仍为null,从而导致NullPointerException。

The exception-handling here is counterproductive, it would be much better to let the exception get thrown than to catch it and keep blundering on. 这里的异常处理适得其反,让抛出异常比抓住它并继续浮雕要好得多。 Because now you're looking at some stacktrace that is unlikely to point to the original problem, it's just a side-effect of the real issue. 因为现在你正在研究一些不太可能指向原始问题的堆栈跟踪,它只是真正问题的副作用。

There's some kind of problem with how you're connecting to the database, exactly what is not knowable from what's posted here. 你如何连接到数据库有一些问题,确切地说是从这里发布的内容不可知的。 Look at the Tomcat console output to see what gets written to System.err, that should give you an indication of what is wrong. 查看Tomcat控制台输出以查看写入System.err的内容,这应该可以指示出现了什么问题。

-- -

Here is the line from the console that the OP found (copied from the comments): 这是OP找到的控制台中的行(从注释中复制):

Mar 06, 2014 2:47:01 PM org.apache.catalina.startup.HostConfig deployWAR INFO: Deploying web application archive /home/pi/apache-tomcat-7.0.50/webapps/l$ java.lang.ClassNotFoundException: com.mysql.jdbc.Driver java.sql.SQLException: No suitable driver found for jdbc:mysql://softwareprojec$ java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 2014年3月6日下午2:47:01 org.apache.catalina.startup.HostConfig deployWAR INFO:部署Web应用程序归档/home/pi/apache-tomcat-7.0.50/webapps/l$ java.lang.ClassNotFoundException:com .mysql.jdbc.Driver java.sql.SQLException:没有为jdbc找到合适的驱动程序:mysql:// softwareprojec $ java.lang.ClassNotFoundException:com.mysql.jdbc.Driver

See this answer for how to deal with this. 请参阅此答案以了解如何处理此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM