Search in sources :

Example 31 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class JRecord method del.

/**
 * 删除数据
 * @param key
 */
@Override
public void del(String key) throws HongsException {
    // table.db.open( );
    table.db.ready();
    try (PreparedStatement ps = table.db.prepareStatement("DELETE FROM `" + table.tableName + "` WHERE id = ?")) {
        ps.setString(1, key);
        ps.executeUpdate();
    } catch (SQLException ex) {
        throw new HongsException.Common(ex);
    }
}
Also used : SQLException(java.sql.SQLException) HongsException(app.hongs.HongsException) PreparedStatement(java.sql.PreparedStatement)

Example 32 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class JRecord method set.

/**
 * 设置过期
 * @param key
 * @param exp
 * @throws HongsException
 */
@Override
public void set(String key, long exp) throws HongsException {
    long now = System.currentTimeMillis() / 1000;
    // table.db.open( );
    table.db.ready();
    try (PreparedStatement ps = table.db.prepareStatement("UPDATE `" + table.tableName + "` SET xtime = ?, mtime = ? WHERE id = ?")) {
        ps.setString(3, key);
        ps.setLong(1, exp);
        ps.setLong(2, now);
        ps.executeUpdate();
    } catch (SQLException ex) {
        throw new HongsException.Common(ex);
    }
}
Also used : SQLException(java.sql.SQLException) HongsException(app.hongs.HongsException) PreparedStatement(java.sql.PreparedStatement)

Example 33 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class JRecord method set.

/**
 * 设置数据
 * @param key
 * @param val
 * @param exp
 */
@Override
public void set(String key, T val, long exp) throws HongsException {
    // 序列化值
    byte[] arr;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(bos)) {
        out.writeObject(val);
        out.flush();
        arr = bos.toByteArray();
    } catch (IOException ex) {
        throw new HongsException.Common(ex);
    }
    long now = System.currentTimeMillis() / 1000;
    // table.db.open( );
    table.db.ready();
    try (PreparedStatement ps = table.db.prepareStatement("UPDATE `" + table.tableName + "` SET data= ?, xtime= ?, mtime= ? WHERE id = ?")) {
        ps.setString(4, key);
        ps.setBytes(1, arr);
        ps.setLong(2, exp);
        ps.setLong(3, now);
        if (ps.executeUpdate() > 0) {
            return;
        }
    } catch (SQLException ex) {
        throw new HongsException.Common(ex);
    }
    try (PreparedStatement ps = table.db.prepareStatement("INSERT INTO `" + table.tableName + "` (id, data, xtime, mtime) VALUES (?, ?, ?, ?)")) {
        ps.setString(1, key);
        ps.setBytes(2, arr);
        ps.setLong(3, exp);
        ps.setLong(4, now);
        if (ps.executeUpdate() > 0) {
            return;
        }
    } catch (SQLException ex) {
        throw new HongsException.Common(ex);
    }
}
Also used : HongsException(app.hongs.HongsException) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 34 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class JRecord method del.

/**
 * 清除数据
 * @param exp
 */
@Override
public void del(long exp) throws HongsException {
    // table.db.open( );
    table.db.ready();
    try (PreparedStatement ps = table.db.prepareStatement("DELETE FROM `" + table.tableName + "` WHERE xtime <= ? AND xtime != 0")) {
        ps.setLong(1, exp);
        ps.executeUpdate();
    } catch (SQLException ex) {
        throw new HongsException.Common(ex);
    }
}
Also used : SQLException(java.sql.SQLException) HongsException(app.hongs.HongsException) PreparedStatement(java.sql.PreparedStatement)

Example 35 with HongsException

use of app.hongs.HongsException in project HongsCORE by ihongs.

the class FileAction method create.

@Override
@Action("create")
public void create(ActionHelper helper) throws HongsException {
    CoreLocale lang = CoreLocale.getInstance("manage");
    String path = helper.getParameter("path");
    String type = helper.getParameter("type");
    String text = helper.getParameter("text");
    File file;
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.required"));
        return;
    }
    path = realPath(path);
    if (path == null) {
        helper.fault(lang.translate("core.manage.file.path.is.error"));
        return;
    }
    file = new File(path);
    if (file.exists()) {
        helper.fault(lang.translate("core.manage.file.path.is.exist"));
        return;
    }
    if (isDenyFile(file)) {
        helper.fault(lang.translate("core.manage.file.interdicted"));
        return;
    }
    // 创建目录
    if ("dir".equals(type)) {
        new File(path).mkdirs();
        helper.reply("");
        return;
    }
    // 写入文件
    try {
        saveFile(file, text);
    } catch (Exception ex) {
        CoreLogger.error(ex);
        helper.fault(lang.translate("core.manage.file.create.failed"));
        return;
    }
    helper.reply("");
}
Also used : CoreLocale(app.hongs.CoreLocale) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HongsException(app.hongs.HongsException) IAction(app.hongs.dh.IAction) Action(app.hongs.action.anno.Action)

Aggregations

HongsException (app.hongs.HongsException)89 Map (java.util.Map)42 HashMap (java.util.HashMap)34 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)15 HashSet (java.util.HashSet)15 LinkedHashMap (java.util.LinkedHashMap)15 Set (java.util.Set)15 List (java.util.List)13 File (java.io.File)11 SQLException (java.sql.SQLException)10 FileNotFoundException (java.io.FileNotFoundException)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)9 PreparedStatement (java.sql.PreparedStatement)8 Iterator (java.util.Iterator)8 DocumentBuilder (javax.xml.parsers.DocumentBuilder)8 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)8 HongsExpedient (app.hongs.HongsExpedient)7 FormSet (app.hongs.action.FormSet)7 Table (app.hongs.db.Table)7