Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.57 KB | None | 0 0
  1. package me.MaYuan.Utils;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DatabaseMetaData;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9. import java.util.HashMap;
  10.  
  11.  
  12. public class DatabaseUtil {
  13.    
  14.     private static HashMap<String, Long> coins = new HashMap<String, Long>();
  15.     private static HashMap<String, Boolean> exists = new HashMap<String, Boolean>();
  16.     private static String url = "jdbc:mysql://127.0.0.1/sys?user=root&password=Aa14&useUnicode=true&characterEncoding=UTF8";
  17.     private static Connection co = null;
  18.     public static String table = "test";
  19.  
  20.     public static void connectMySQL(){
  21.         try{
  22.             Class.forName("com.mysql.jdbc.Driver");
  23.             co = DriverManager.getConnection(url);
  24.         }catch(Exception e){
  25.             e.printStackTrace();
  26.             System.out.println("Failed to connect");
  27.         }
  28.     }
  29.     public static void closeConnect(){
  30.         try{
  31.             if(co.isClosed()){
  32.                 return;
  33.             }
  34.             co.close();
  35.         }catch(Exception e){
  36.             e.printStackTrace();
  37.             System.out.println("Failed to close connection, Please redo");
  38.         }
  39.     }
  40.     public static void reloadConnect(){
  41.         try{
  42.             if(co == null){
  43.                 closeConnect();
  44.                 connectMySQL();
  45.             }
  46.         }catch(Exception e){
  47.             e.printStackTrace();
  48.             System.out.println("Failed to reload connection");
  49.         }
  50.     }
  51.  
  52.     public static Boolean createTable(String Table){
  53.         String sqlcode = "create table MyTable(Player varchar(255), Coins int, primary key(Player))".replace("MyTable", Table);
  54.         try{
  55.             if(tableExit(Table) != true){
  56.             System.out.println("creating a new table called " + Table);
  57.             Statement s = co.createStatement();
  58.             int result = s.executeUpdate(sqlcode);
  59.             if(result != -1){
  60.                 System.out.println("create table successful");
  61.                 return Boolean.valueOf(true);
  62.             }
  63.             }
  64.         }catch(Exception e){
  65.             e.printStackTrace();
  66.             System.out.println("Failed to create table on MySQL");
  67.     }finally{
  68.         closeConnect();
  69.     }
  70.         return Boolean.valueOf(false);
  71. }
  72.     public static boolean tableExit(String Table){
  73.         try{
  74.             if(co == null){
  75.                 connectMySQL();
  76.             }
  77.             DatabaseMetaData meta = co.getMetaData();
  78.               ResultSet rsTables = meta.getTables(null, null, Table, null);
  79.               if (rsTables.next()) {
  80.                 return Boolean.valueOf(true);
  81.               }
  82.               System.out.println("have not found table exit");
  83.               return Boolean.valueOf(false);
  84.             }catch (Exception e){
  85.               e.printStackTrace();
  86.               System.out.println("Failed to check table");
  87.               return Boolean.valueOf(false);
  88.             }
  89.     }
  90.     public static boolean coinExists(String name){
  91.         String sqlcode = "select * from test where Player = ?";
  92.         connectMySQL();
  93.         try{
  94.             PreparedStatement p = co.prepareStatement(sqlcode);
  95.             p.setString(1, name);
  96.             ResultSet rs = p.executeQuery();
  97.               if (rs.next()) {
  98.                 exists.put(name, Boolean.valueOf(true));
  99.               }else{
  100.                   exists.put(name, Boolean.valueOf(false));
  101.               }
  102.         }catch(Exception e){
  103.             e.printStackTrace();
  104.             System.out.println("Failed to check coin exit");
  105.             exists.put(name, Boolean.valueOf(false));
  106.         }
  107.         return ((Boolean)exists.get(name)).booleanValue();
  108.     }
  109.     public static long getCoins(String name){
  110.         String sqlcode = "select * from test where Player = ?";
  111.         connectMySQL();
  112.         if (!coinExists(name)) {
  113.               return 0L;
  114.             }
  115.     try{
  116.         PreparedStatement p = co.prepareStatement(sqlcode);
  117.         p.setString(1, name);
  118.         ResultSet rs = p.executeQuery();
  119.           if(rs.next()){
  120.           coins.put(name, Long.valueOf(rs.getLong("Coins")));
  121.           }
  122.     }catch(Exception e){
  123.         e.printStackTrace();
  124.         System.out.println("Failed to get coins for +" + name);
  125.         coins.put(name, Long.valueOf(0L));
  126.     }finally{
  127.         closeConnect();
  128.     }
  129.     return ((Long)coins.get(name)).longValue();
  130.     }
  131.     public static void setCoins(String name, long money){
  132.         String sqlcode = "insert into test(Player, Coins) VALUES (?, ?)";
  133.         connectMySQL();
  134.         try{
  135.             deleteBank(name);
  136.             PreparedStatement p = co.prepareStatement(sqlcode);
  137.             p.setString(1, name);
  138.             p.setLong(2, money);
  139.             p.executeUpdate();
  140.         }catch(Exception e){
  141.             e.printStackTrace();
  142.             System.out.println("Failed to set coins for " + name);
  143.         }finally{
  144.             closeConnect();
  145.         }
  146.     }
  147.     public static void deleteBank(String name){
  148.         String sqlcode = "delete * from test where Player = ?";
  149.         connectMySQL();
  150.         if (!coinExists(name)) {
  151.               return;
  152.             }
  153.         try{
  154.             PreparedStatement p = co.prepareStatement(sqlcode);
  155.             p.setString(1, name);
  156.             p.executeUpdate();
  157.         }catch(Exception e){
  158.             e.printStackTrace();
  159.             System.out.println("Failed to delete bank");
  160.         }
  161.     }
  162.  
  163.    
  164.    
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement