gpt4 book ai didi

java - JSONObject 类型的 getBackMysql() 方法未定义

转载 作者:行者123 更新时间:2023-11-29 10:18:43 25 4
gpt4 key购买 nike

我现在正在编写一个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 显示以下错误: JSONObject 类型的方法 getBackMysql() 未定义。我对 JSONObject 还很陌生,不知道发生了什么。我已经更改了几次类名,但不起作用。另外,这是大型项目之前的一个小测试。

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

最佳答案

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

因为 getBackMysql() 与 main 方法位于同一个类中。您可以替换该行

JSONObject json=json.getBackMysql();

JSONObject json=getBackMysql();

关于java - JSONObject 类型的 getBackMysql() 方法未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49724763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com