【问题标题】:The method getBackMysql() is undefined for the type JSONObjectJSONObject 类型的方法 getBackMysql() 未定义
【发布时间】:2018-09-18 08:51:39
【问题描述】:

我现在正在编写一个 java 程序来取回 Mysql 数据库中的数据。肯定已经阅读了有关此问题的所有帖子(仍然无法弄清楚),并且我已将 org.json.jar 包含在我的参考库中。

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

import org.json.*;


public class getBackMysql
{
    static final String JDBC_Driver = ...;
    static final String DB_URL = ...;

    static final String USER = ...;
    static final String PASS = ...;

    public static JSONObject jsonobject;

    public static JSONObject getBackMysql()
    {
        Connection conn = null;
        Statement stmt = null;

        try
        {
            Class.forName("com.mysql.jdbc.Driver");

            System.out.println("Connecting the Database...");
            conn = DriverManager.getConnection( DB_URL, USER, PASS );

            System.out.println("Instantiating Statement...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT t, obtid FROM t_localobtmind where obtid = 'G3742' order by ddatetime desc limit 1;";
            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next())
            {
                String obtid = rs.getString("obtid");
                int temp = rs.getInt("t");

                String temperature = (String)("Temperature: " + temp / 10 + "℃");

                jsonobject.put("obtid", obtid);
                jsonobject.put("temperature", temperature);
            }

            rs.close();
            stmt.close();
            conn.close();

        }

        catch(SQLException se)
        {
            se.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (stmt != null)
                {
                    stmt.close();
                }
            }
            catch(SQLException se2)
            {

            }
            try
            {
                if (conn != null)
                {
                    conn.close();
                }
            }
            catch(SQLException se)
            {
                se.printStackTrace();
            }
        }

    System.out.println("----Program ends----");
    return jsonobject;
    }

    public static void main( String[] args ) 
    {

        JSONObject json = json.getBackMysql();
        int tmp = json.getInt("temperature");
        String obtid = json.getString("obtid");


    }
}

Eclipse 向我显示以下错误:方法 getBackMysql() 未定义 JSONObject 类型。我对 JSONObject 仍然很陌生,不知道发生了什么。我已经多次更改类名,但它不起作用。另外,这是一个大型项目之前的一个小测试。

有人知道发生了什么吗? 提前致谢。

【问题讨论】:

  • 请粘贴您的完整代码以及上述每个代码sn-p的文件名
  • 已修改。只有一个文件“getBackMysql.java”。

标签: java mysql json


【解决方案1】:

JSONObject 类没有getBackMysql() 方法,所以我们无法从JSONObject 类的对象访问它。

因为 getBackMysql() 与存在 main 方法的类在同一个类中。你可以换行

JSONObject json=json.getBackMysql();

JSONObject json=getBackMysql();

【讨论】:

  • 一旦我做了这样的替换,程序就会抛出未处理的异常类型 JSONException 并要求我用 try & catch 包围代码。
  • 在 catch 块中添加 JSONException 类,就像你为 SQLException 和 Exception 类做的一样
猜你喜欢
  • 1970-01-01
  • 2012-09-19
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 2016-06-07
  • 2021-06-19
相关资源
最近更新 更多