Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Database extends Source{
- private Connection connection;
- private Statement statement;
- private String host, database, username, password;
- private int port;
- public Database(String host, String database, String username,
- String password, int port) {
- super(SourceType.Database);
- this.setHost(host);
- this.setDatabase(database);
- this.setUsername(username);
- this.setPassword(password);
- this.setPort(port);
- try {
- connectToDatabase();
- }catch(Exception e) {
- Bukkit.broadcastMessage("§cConnection to " + host + ":" + port + " failed");
- Bukkit.broadcastMessage("§c" + e.getMessage());
- return;
- }
- }
- private void connectToDatabase() throws SQLException,
- ClassNotFoundException {
- if (connection != null && !connection.isClosed()) {
- return;
- }
- synchronized (this) {
- if (connection != null && !connection.isClosed()) {
- return;
- }
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection("jdbc:mysql://"
- + this.host + ":" + this.port + "/" + this.database,
- this.username, this.password);
- setStatement(connection.createStatement());
- }
- }
- @Override
- public void cut(SeaInfos s) {
- // TODO Auto-generated method stub
- }
- @Override
- public void writeConfig(String way, Object o) {
- // TODO Auto-generated method stub
- }
- @Override
- public void writeDatabase(String table, String field, String o) {
- // TODO Auto-generated method stub
- }
- @Override
- public Object readConfig(String path) {
- return null;
- }
- @Override
- public Object readDatabase(String table, String fieldName,
- String condition, String pass) {
- return getObjects(fieldName, table, condition, pass);
- }
- @SuppressWarnings("null")
- public List<Object> getObjects(String fieldName, String table, String condition, String pass) {
- try {
- ResultSet result = statement.executeQuery("SELECT "+fieldName+" FROM " + table + " WHERE " + condition + " = " + pass + ";");
- List<Object> objects = null;
- while(result.next()) {
- objects.add(result.getObject(fieldName));
- }
- return objects;
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- public void setObject(String table, String[] keys, String[] values, Object c) {
- try {
- statement.executeQuery("INSERT INTO " + table + "(" + keys +") VALUES ("+values+");");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public Connection getConnection() {
- return connection;
- }
- public void setConnection(Connection connection) {
- this.connection = connection;
- }
- public String getHost() {
- return host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public String getDatabase() {
- return database;
- }
- public void setDatabase(String database) {
- this.database = database;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public Statement getStatement() {
- return statement;
- }
- public void setStatement(Statement statement) {
- this.statement = statement;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement