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{
- String s =
- "create table if not exists users(" +
- "uuid varchar(36) not null," +
- "name varchar(20) not null," +
- "coins int not null,"+
- "primary key(uuid));";
- try {
- getConnection().prepareStatement("SELECT * FROM `users`").executeUpdate(s);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- 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()) {
- String s = "INSERT INTO users (uuid, name, coins) VALUES (" +
- "'" + u.getUuid().toString() + "'," +
- "'" + u.getName() + "',"+
- "'" + u.getCoins() + "'"+
- ") ON DUPLICATE KEY UPDATE " +
- "name='" + u.getName() + "',"+
- "coins='" + u.getCoins() +"';";
- getConnection().prepareStatement("SELECT * FROM `users`").executeUpdate(s);
- 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