【发布时间】: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”。