Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MySQL
- {
- private String host;
- private int port;
- private String user;
- private String password;
- private String database;
- private Connection conn;
- public MySQL()
- {
- File file = new File("plugins/MySQLBan/", "database.yml");
- FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
- cfg.options().header("Ersetze den localhost durch den MySQL-Serverport. [localhost ist Standard]\nErsetze den port durch den MySQL-Serverport. [3306 ist Standard]\nErsetze das root durch deinen MySQL-Server User. [z.B. root]\nErsetze das root durch dein MySQL-Server Passwort. [z.B. root]\nErsetze die Datenbank durch deine MySQL Datenbank die du verwenden willst.");
- String db = "database.";
- cfg.addDefault(db + "MySQLHost", "localhost");
- cfg.addDefault(db + "MySQLPort", Integer.valueOf(3306));
- cfg.addDefault(db + "MySQLUser", "root");
- cfg.addDefault(db + "MySQLPasswort", "root");
- cfg.addDefault(db + "MySQLDatenbank", "Datenbank");
- cfg.options().copyDefaults(true);
- try
- {
- cfg.save(file);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- this.host = cfg.getString(db + "MySQLHost");
- this.port = cfg.getInt(db + "MySQLPort");
- this.user = cfg.getString(db + "MySQLUser");
- this.password = cfg.getString(db + "MySQLPasswort");
- this.database = cfg.getString(db + "MySQLDatenbank");
- this.conn = openConnection();
- }
- public Connection openConnection()
- {
- try
- {
- Class.forName("com.mysql.jdbc.Driver");
- Connection conn = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.database, this.user, this.password);
- this.conn = conn;
- return conn;
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- return null;
- }
- public Connection getConnection()
- {
- return this.conn;
- }
- public boolean hasConnection()
- {
- try
- {
- return this.conn != null | this.conn.isValid(1);
- }
- catch (SQLException e) {}
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement