Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQL {
- private static HikariConfig config = new HikariConfig();
- private static HikariDataSource ds;
- static {
- config.setJdbcUrl("jdbc:mysql://localhost:3306/mordziaty");
- config.setUsername("root");
- config.setPassword("");
- config.addDataSourceProperty("cachePrepStmts", "true");
- config.addDataSourceProperty("prepStmtCacheSize", "250");
- config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
- ds = new HikariDataSource(config);
- }
- public static Connection getConnection() throws SQLException {
- return ds.getConnection();
- }
- public static void createTable() throws SQLException{
- StringBuilder sb = new StringBuilder();
- sb.append("create table if not exists users(");
- sb.append("uuid varchar(36) not null,");
- sb.append("name varchar(20) not null,");
- sb.append("coins int not null,");
- sb.append("primary key(uuid));");
- try {
- getConnection().createStatement().executeUpdate(sb.toString());
- System.out.print("udalo sie stworzyc tabeleki");
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.print("Nie udalo sie stworzyc tabelek");
- }
- }
- public static void loadData() throws SQLException {
- int i = 0;
- PreparedStatement ps = getConnection().prepareStatement("SELECT * FROM `users`");
- ResultSet rs = ps.executeQuery();
- while (rs.next()) {
- User u = User.get(UUID.fromString(rs.getString("uuid")));
- u.setName(rs.getString("name"));
- u.setCoins(rs.getInt("coins"));
- i++;
- }
- System.out.print("Loaded " + i + " users");
- }
- public static void saveData() throws SQLException {
- int i = 0;
- try {
- for (User u : UserUtils.getUsers()) {
- StringBuilder sb = new StringBuilder();
- sb.append("'" + u.getUuid().toString() +"',");
- sb.append("'" + u.getName() +"',");
- sb.append("'" + u.getCoins() +"'");
- sb.append(") ON DUPLICATE KEY UPDATE ");
- sb.append("name='" + u.getName() +"',");
- sb.append("coins='" + u.getCoins() +"';");
- getConnection().createStatement().executeUpdate(sb.toString());
- i++;
- }
- System.out.print("Saved " + i + " users");
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.print("Blad z zapisem danych!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement