configuration via sql (done)

This commit is contained in:
Sola
2015-11-23 16:29:20 +08:00
parent 5d01a9669a
commit ad0c75c12a
4 changed files with 65 additions and 6 deletions

View File

@@ -1,5 +1,7 @@
package love.sola.netsupport.sql;
import com.google.gson.Gson;
import javax.naming.InitialContext;
import javax.sql.DataSource;
@@ -12,6 +14,7 @@ import javax.sql.DataSource;
public class SQLCore {
public static DataSource ds;
public static Gson gson = new Gson();
static {
try {

View File

@@ -1,14 +1,39 @@
package love.sola.netsupport.sql;
import love.sola.netsupport.config.Settings;
import java.sql.*;
/**
* ***********************************************
* Created by Sola on 2015/11/10.
* Don't modify this source without my agreement
* ***********************************************
*/
public class TableConfig {
public class TableConfig extends SQLCore{
public static final String KEY_SYS = "sys";
public static Settings getSettings() {
try (Connection conn = ds.getConnection()) {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM settings WHERE type='" + KEY_SYS + "'");
if (rs.next()) {
return gson.fromJson(rs.getString("data"), Settings.class);
}
} catch (SQLException e) { }
return null;
}
public static int saveSettings(Settings obj) {
try (Connection conn = ds.getConnection()) {
PreparedStatement ps = conn.prepareStatement("UPDATE settings SET data=? WHERE type=?");
ps.setString(1, gson.toJson(obj));
ps.setString(2, KEY_SYS);
return ps.executeUpdate();
} catch (SQLException e) {
}
return -1;
}
}