Solved Help with MySQL

Discussion in 'Plugin Development' started by Horsey, May 26, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Horsey

    Hey bukkit!
    I'm trying to make a plugin that works with a website- it needs to save data to a MySQL table so that the website can draw it from there, but I'm having issues understanding what exactly MySQL does.
    I know MySQL is a database, which I can use to store and receive data. What I don't know is how exactly the end user would use the database, would they need to download it themselves? Would I need to use a MySQL API?
    Thanks <3
     
  2. Offline

    Caderape2

    @Horsey A SQL table it's just a stockage. There's no download for a player. It's like a file.
    that will just share information between your wesite and your plugin.

    Here a little class for hook with a database from your plugin.
    Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    
    public final class Database
    {
    
        private String hostname = "localhost";
        private int port = 3306;
        private String database;
        private String user;
        private String passeword;
       
        private Connection connection;
           
       
       
        public Database(String databasename, String username, String password)
        {
            this.database = databasename;
            this.user = username;
            this.passeword = password;
           
            openConnection();
        }
       
       
        public Database(String hostname, int port, String databasename, String username, String password)
        {
            this.hostname = hostname;
            this.port = port;
            this.database = databasename;
            this.user = username;
            this.passeword = password;
           
            openConnection();
        }
       
    
       
        private boolean openConnection()
        {
            try
            {
                if (connection == null || connection.isClosed())
                {               
                    Class.forName("com.mysql.jdbc.Driver");
                    connection = DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database, user, passeword);
                }
                return connection != null && !connection.isClosed();
               
            } catch (ClassNotFoundException | SQLException e) {
            }
           
            return false;
        }
       
       
        public boolean hasDatabaseConnection() {
            try {
                return this.connection != null && !this.connection.isClosed();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return false;
        }
       
       
        public int updateSQL(String query) throws SQLException  {
    
            if (openConnection())
            {
                Statement statement = connection.createStatement();
               
                int result = statement.executeUpdate(query);
    
                return result;
            }
    
            return 0;
        }
       
       
    
        public ResultSet querySQL(String query) throws SQLException, ClassNotFoundException {
           
            if (openConnection())
            {
                Statement statement = connection.createStatement();
                
                ResultSet result = statement.executeQuery(query);
           
                return result;
            }
           
            return null;
        }
    }
    
    - the method update SQL will execute a simple query, and the method querySQL return a statement ResultSet
     
  3. Offline

    Horsey

    @Caderape2 So I don't need to host a MySQL database?if so, how will I get the data on my website?
     
  4. Offline

    Caderape2

    @Horsey Of course you need phpmyadmin on your machine.
    If you use linux, it's easy to install, with windows, i don't know how to do
     
Thread Status:
Not open for further replies.

Share This Page