我试图修复我很久以前为Craftbukkit写过的插件,但我在一节中难以理解。我很幸运地搜索过Google,而且我已经让其他Java开发人员听到我不应该使用for循环,因为它相当基本,或者我是&#39 ; m在错误的位置使用布尔表达式。没人会告诉我 如何 我可以解决这个问题,所以我知道将来的参考资料 - 下面是抛出错误的类:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.logging.Logger;
import org.bukkit.configuration.file.FileConfiguration;
public class Database
{
private static String host = null;
private static String port = null;
private static String database = null;
private static String table = null;
private static String username = null;
private static String password = null;
private static String colUsername = null;
private static Logger logger = null;
public static void init(FileConfiguration config, Logger log)
{
logger = log;
host = config.getString("DBHost");
port = config.getString("DBPort");
database = config.getString("DBName");
table = config.getString("DBTable");
username = config.getString("DBUser");
password = config.getString("DBPass");
colUsername = config.getString("ColUsername");
}
public static Hashtable<String, Object> getUserInfo(String user)
{
String url = "jdbc:mysql://" + host + ":" + port + "/" + database;
String query = "SELECT * FROM " + table + " WHERE " + colUsername + " = ?";
Connection connect = null;
PreparedStatement stmt = null;
ResultSet result = null;
Hashtable<String, Object> userInfo = new Hashtable<String, Object>();
try
{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection(url, username, password);
stmt = connect.prepareStatement(query);
stmt.setString(1, user);
result = stmt.executeQuery();
ResultSetMetaData rsmd;
int i;
for (; result.next(); i <= rsmd.getColumnCount())
{
rsmd = result.getMetaData();
i = 1; continue;
userInfo.put(rsmd.getColumnName(i), result.getObject(i));i++;
}
return userInfo;
}
catch (ClassNotFoundException e)
{
logger.warning("Unable to load driver. Using default behaviour.");
e.printStackTrace();
}
catch (SQLException e)
{
logger.warning("Database error. Using default behaviour.");
e.printStackTrace();
}
finally
{
if (result != null) {
try
{
result.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (stmt != null) {
try
{
stmt.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
if (connect != null) {
try
{
connect.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
return null;
}
}
我遇到的错误出现在代码的这一部分:
for (; result.next(); i <= rsmd.getColumnCount())
{
rsmd = result.getMetaData();
i = 1; continue;
userInfo.put(rsmd.getColumnName(i), result.getObject(i));i++;
}
我收到错误&#34;令牌上的语法错误&#34;&lt; =&#34;,无效的AssignmentOperator&#34;
我该如何解决这个问题,如何改进呢?
编辑#1:
这是我更新的代码,根据Jon的回答:
try
{
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection(url, username, password);
stmt = connect.prepareStatement(query);
stmt.setString(1, user);
result = stmt.executeQuery();
ResultSetMetaData rsmd = result.getMetaData();
while (result.next()) {
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
rsmd = result.getMetaData();
userInfo.put(rsmd.getColumnName(i), result.getObject(i));i++;
}
}
return userInfo;
}
答案 0 :(得分:6)
基本上这是错误的方式:
for (; result.next(); i <= rsmd.getColumnCount())
可能:
for (; i <= rsmd.getColumnCount(); result.next())
虽然更有可能,但实际上你想要:
while (result.next()) {
// This outer loop is executed once per row
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
// This inner loop is executed once per column (per row, as it's
// within the outer loop)
}
}
话虽如此,你甚至没有初始化rsmd
,这没有用。我怀疑你可能想打电话给ResultSet.getMetadata()
,例如
rsmd = result.getMetadata();
作为参考,for
语句声明的三个部分如下:
false
有关详细信息,请参阅section 14.14.1 of the JLS或for
statement part of the Java tutorial。
答案 1 :(得分:1)
这不适用于for
循环的最后一部分:
i <= rsmd.getColumnCount()
也许你是这个意思?
for (; i <= rsmd.getColumnCount(); result.next())
答案 2 :(得分:0)
for
循环的基本语法:
for (any; boolean; any)
你的是
for (any; any; boolean)
只需更改订单。