Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.hyperplay.SQLLib;
- import java.sql.*;
- public class SQLLib {
- Connection con = null;
- PreparedStatement pst = null;
- Statement st = null;
- ResultSet rs = null;
- String url = null;
- String user = null;
- String password = null;
- public SQL(String url, String user, String password){
- this.url = url;
- this.user = user;
- this.password = password;
- }
- public boolean createTable(String table) {
- try {
- con = DriverManager.getConnection(url, user, password);
- st = con.createStatement();
- String query = "CREATE TABLE IF NOT EXISTS `" + table + "` ( cKey VARCHAR(255) NOT NULL, cValue VARCHAR(255),
- UNIQUE KEY(cKey) );";
- st.executeUpdate(query);
- } catch (SQLException ex) {
- return false;
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (pst != null) {
- pst.close();
- }
- if (con != null) {
- con.close();
- }
- } catch (SQLException ex) {
- return false;
- }
- }
- return true;
- }
- public boolean set(String table, String key, String value) {
- try {
- if(!this.createTable(table)){
- return false;
- }
- con = DriverManager.getConnection(url, user, password);
- String query = "INSERT INTO `" + table + "` (cKey, cValue) VALUES (?, ?);";
- pst = con.prepareStatement(query);
- pst.setString(1, key);
- pst.setString(2, value);
- pst.executeUpdate();
- rs = pst.executeQuery();
- } catch (SQLException ex) {
- return false;
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (pst != null) {
- pst.close();
- }
- if (con != null) {
- con.close();
- }
- } catch (SQLException ex) {
- return false;
- }
- }
- return true;
- }
- public boolean set(String table, String key, int value) {
- return this.set(table, key, String.valueOf(value));
- }
- public boolean set(String table, String key, boolean value) {
- return this.set(table, key, String.valueOf(value));
- }
- public boolean set(String table, String key, double value) {
- return this.set(table, key, String.valueOf(value));
- }
- public Object get(String table, String key){
- try {
- con = DriverManager.getConnection(url, user, password);
- String query = "SELECT * FROM `" + table + "` WHERE cKey = ?;";
- pst = con.prepareStatement(query);
- pst.setString(1, key);
- pst.executeUpdate();
- rs = pst.executeQuery();
- if(rs.getObject(1) == null){
- return null;
- }else{
- return rs.getObject(1);
- }
- } catch (SQLException ex) {
- return null;
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (pst != null) {
- pst.close();
- }
- if (con != null) {
- con.close();
- }
- } catch (SQLException ex) {
- return null;
- }
- }
- }
- public boolean removeTable(String table){
- try {
- con = DriverManager.getConnection(url, user, password);
- String query = "DROP TABLE `" + table + "`;";
- pst = con.prepareStatement(query);
- rs = pst.executeQuery();
- rs.next();
- return true;
- } catch (SQLException ex) {
- return false;
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (pst != null) {
- pst.close();
- }
- if (con != null) {
- con.close();
- }
- } catch (SQLException ex) {
- return false;
- }
- }
- }
- public boolean removeEntry(String table, String entry){
- try {
- con = DriverManager.getConnection(url, user, password);
- String query = "DELETE FROM `" + table + "` WHERE cKey = ?;";
- pst = con.prepareStatement(query);
- pst.setString(1, entry);
- rs = pst.executeQuery();
- rs.next();
- return true;
- } catch (SQLException ex) {
- return false;
- } finally {
- try {
- if (rs != null) {
- rs.close();
- }
- if (pst != null) {
- pst.close();
- }
- if (con != null) {
- con.close();
- }
- } catch (SQLException ex) {
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement