1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +00:00

fix: 修复SQL预处理语句执行错误 调整命令解析提示

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2017-03-29 20:42:43 +08:00
parent e65d38445d
commit 2766713ff9
5 changed files with 78 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package pw.yumc.YumCore.bukkit;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.logging.Handler; import java.util.logging.Handler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -363,4 +364,24 @@ public class Log {
public static boolean isGlobalDebug() { public static boolean isGlobalDebug() {
return globalDebug; return globalDebug;
} }
public static String osn(List<?> classes) {
StringBuilder str = new StringBuilder("[");
classes.forEach(c -> str.append(c.getClass().getSimpleName()).append(", "));
return classes.isEmpty() ? "[]" : str.substring(0, str.length() - 2) + "]";
}
public static String osn(Object... classes) {
return osn(Arrays.asList(classes));
}
public static String csn(List<Class> classes) {
StringBuilder str = new StringBuilder("[");
classes.forEach(c -> str.append(c.getSimpleName()).append(", "));
return classes.isEmpty() ? "[]" : str.substring(0, str.length() - 2) + "]";
}
public static String csn(Class[] classes) {
return csn(Arrays.asList(classes));
}
} }

View File

@ -78,7 +78,7 @@ public class CommandMain implements CommandExecutor {
if (ci != null) { if (ci != null) {
injectPluginCommand(ci); injectPluginCommand(ci);
Class[] params = method.getParameterTypes(); Class[] params = method.getParameterTypes();
Log.d("注册主命令 %s 参数类型: %s", ci.getName(), Arrays.toString(params)); Log.d("注册主命令 %s 参数类型: %s", ci.getName(), Log.csn(params));
try { try {
Class<? extends CommandSender> sender = params[0]; Class<? extends CommandSender> sender = params[0];
cmds.add(ci); cmds.add(ci);

View File

@ -65,7 +65,7 @@ public class CommandParse {
if (parse == null) { throw new ParseException(String.format("存在无法解析的参数类型 %s", clazz.getName())); } if (parse == null) { throw new ParseException(String.format("存在无法解析的参数类型 %s", clazz.getName())); }
this.parse.add(parse.clone().parseAnnotation(annotations).handleAttrs()); this.parse.add(parse.clone().parseAnnotation(annotations).handleAttrs());
} }
Log.d("命令解析器 %s", String.valueOf(parse)); Log.d("命令解析器 %s", Log.osn(parse));
} }
public static CommandParse get(Method method) { public static CommandParse get(Method method) {
@ -110,7 +110,7 @@ public class CommandParse {
throw new ParseException(String.format("第 %s 个参数 %s", isMain ? 1 : 2 + i, e.getMessage())); throw new ParseException(String.format("第 %s 个参数 %s", isMain ? 1 : 2 + i, e.getMessage()));
} }
} }
Log.d("解析参数: %s => %s", Arrays.toString(args), pobjs); Log.d("解析参数: %s => %s", Arrays.toString(args), Log.osn(pobjs));
return pobjs.toArray(); return pobjs.toArray();
} }

View File

@ -235,7 +235,7 @@ public class CommandSub implements TabExecutor {
CommandInfo ci = CommandInfo.parse(method, clazz); CommandInfo ci = CommandInfo.parse(method, clazz);
if (ci != null) { if (ci != null) {
Class[] params = method.getParameterTypes(); Class[] params = method.getParameterTypes();
Log.d("注册子命令: %s 参数类型: %s", ci.getName(), Arrays.toString(params)); Log.d("注册子命令: %s 参数类型: %s", ci.getName(), Log.csn(params));
try { try {
Class<? extends CommandSender> sender = params[0]; Class<? extends CommandSender> sender = params[0];
// 用于消除unuse警告 // 用于消除unuse警告

View File

@ -5,6 +5,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.Arrays;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
@ -59,12 +60,12 @@ public abstract class DataBaseCore {
* SQL执行异常 * SQL执行异常
*/ */
public boolean execute(String sql, Object... obj) throws SQLException { public boolean execute(String sql, Object... obj) throws SQLException {
debug(sql); debug(sql, obj);
PreparedStatement ps = prepareStatement(sql); PreparedStatement ps = prepareStatement(sql);
for (int i = 0; i < obj.length; i++) { for (int i = 0; i < obj.length; i++) {
ps.setObject(i + 1, obj[i]); ps.setObject(i + 1, obj[i]);
} }
boolean result = ps.execute(sql); boolean result = ps.execute();
ps.close(); ps.close();
return result; return result;
} }
@ -98,6 +99,24 @@ public abstract class DataBaseCore {
return st.executeQuery(sql); return st.executeQuery(sql);
} }
/**
* 查询数据库
*
* @param sql
* SQL查询语句
* @return 查询结果
* @throws SQLException
* SQL查询异常
*/
public ResultSet query(String sql, Object[] params) throws SQLException {
debug(sql, params);
PreparedStatement pst = prepareStatement(sql);
for (int i = 0; i < params.length; i++) {
pst.setObject(i + 1, params[i]);
}
return pst.executeQuery();
}
/** /**
* 更新数据库内的数据 * 更新数据库内的数据
* *
@ -127,16 +146,40 @@ public abstract class DataBaseCore {
* SQL执行异常 * SQL执行异常
*/ */
public int update(String sql, Object[] obj) throws SQLException { public int update(String sql, Object[] obj) throws SQLException {
debug(sql); debug(sql, obj);
PreparedStatement ps = prepareStatement(sql); PreparedStatement ps = prepareStatement(sql);
for (int i = 0; i < obj.length; i++) { for (int i = 0; i < obj.length; i++) {
ps.setObject(i + 1, obj[i]); ps.setObject(i + 1, obj[i]);
} }
int result = ps.executeUpdate(sql); int result = ps.executeUpdate();
ps.close(); ps.close();
return result; return result;
} }
/**
* 获得数据操作对象
*
* @return 操作对象
* @throws SQLException
* SQL执行异常
*/
public Statement getStatement() throws SQLException {
return getConnection().createStatement();
}
/**
* 获得数据操作对象(预处理)
*
* @param sql
* SQL语句
* @return 操作对象
* @throws SQLException
* SQL执行异常
*/
public PreparedStatement prepareStatement(String sql) throws SQLException {
return getConnection().prepareStatement(sql);
}
/** /**
* 发送警告 * 发送警告
* *
@ -158,26 +201,14 @@ public abstract class DataBaseCore {
} }
/** /**
* 获得数据操作对象 * SQL调试消息
* *
* @return 操作对象
* @throws SQLException
* SQL执行异常
*/
protected Statement getStatement() throws SQLException {
return getConnection().createStatement();
}
/**
* 获得数据操作对象(预处理)
*
* @param sql * @param sql
* SQL语句 * SQL语句
* @return 操作对象 * @param params
* @throws SQLException * 参数
* SQL执行异常
*/ */
protected PreparedStatement prepareStatement(String sql) throws SQLException { private void debug(String sql, Object... params) {
return getConnection().prepareStatement(sql); Log.d("[SQL] %s 参数: %s", sql, Arrays.toString(params));
} }
} }