Advertisement
Guest User

Untitled

a guest
Nov 30th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. public class MySQL {
  2.  
  3. private static HikariConfig config = new HikariConfig();
  4. private static HikariDataSource ds;
  5.  
  6. static {
  7. config.setJdbcUrl("jdbc:mysql://localhost:3306/mordziaty");
  8. config.setUsername("root");
  9. config.setPassword("");
  10. config.addDataSourceProperty("cachePrepStmts", "true");
  11. config.addDataSourceProperty("prepStmtCacheSize", "250");
  12. config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
  13. ds = new HikariDataSource(config);
  14. }
  15.  
  16. public static Connection getConnection() throws SQLException {
  17. return ds.getConnection();
  18. }
  19.  
  20. public static void createTable() throws SQLException{
  21.  
  22. String s =
  23. "create table if not exists users(" +
  24. "uuid varchar(36) not null," +
  25. "name varchar(20) not null," +
  26. "coins int not null,"+
  27. "primary key(uuid));";
  28.  
  29. try {
  30. getConnection().prepareStatement("SELECT * FROM `users`").executeUpdate(s);
  31. } catch (SQLException e) {
  32. e.printStackTrace();
  33.  
  34. }
  35. }
  36.  
  37. public static void loadData() throws SQLException {
  38. int i = 0;
  39. PreparedStatement ps = getConnection().prepareStatement("SELECT * FROM `users`");
  40. ResultSet rs = ps.executeQuery();
  41. while (rs.next()) {
  42. User u = User.get(UUID.fromString(rs.getString("uuid")));
  43. u.setName(rs.getString("name"));
  44. u.setCoins(rs.getInt("coins"));
  45. i++;
  46. }
  47. System.out.print("Loaded " + i + " users");
  48. }
  49.  
  50.  
  51. public static void saveData() throws SQLException {
  52. int i = 0;
  53. try {
  54. for (User u : UserUtils.getUsers()) {
  55. String s = "INSERT INTO users (uuid, name, coins) VALUES (" +
  56. "'" + u.getUuid().toString() + "'," +
  57. "'" + u.getName() + "',"+
  58. "'" + u.getCoins() + "'"+
  59. ") ON DUPLICATE KEY UPDATE " +
  60. "name='" + u.getName() + "',"+
  61. "coins='" + u.getCoins() +"';";
  62. getConnection().prepareStatement("SELECT * FROM `users`").executeUpdate(s);
  63. i++;
  64. }
  65. System.out.print("Saved " + i + " users");
  66. } catch (SQLException e) {
  67. e.printStackTrace();
  68. System.out.print("Blad z zapisem danych!");
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement