Row ophalen uit database en vergelijken met datum

Status
Niet open voor verdere reacties.

djmaster329

Gebruiker
Lid geworden
12 mei 2009
Berichten
304
Hallo,

Ik ben bezig met een plugin voor mijn Minecraft server.
Het is de bedoeling dat de plugin als een speler inlogt, de row uit de database ophaalt en de datum (die in een kolom staat) ophaalt en vergelijkt met de datum van vandaag. Als de datum in het verleden ligt moet de speler geweigerd worden, en als de datum in de toekomst ligt, moet er niets gebeuren.

Ik heb tot nu toe:

Code:
package me.djmaster329.SubscriptionManager;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.event.EventHandler;
import java.sql.*; 
import java.util.logging.Logger;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.io.*;
 
/*
* extends pugin means that it is a plugin
* implements Listener declares this class as an event listener. It makes sense to put listeners for
* different stuff into different classes but this is just a simple example
*/
public class SubscriptionManager extends JavaPlugin implements Listener {
	
	Logger log = Logger.getLogger("Minecraft");
	
    @Override
    public void onEnable() {
        //this method is called once your plugin is enabled
 
        //this gets the plugin manager for our plugin
        PluginManager pm = this.getServer().getPluginManager();
 
        /*
        * this registers our listener. The first argument is the listener the second the plugin
        * if you put the listener into a different class you have to put an istance of that class
        * as the first argument
        */
        pm.registerEvents(this, this);
        CreateFolder();
        
        
    }
    
    public void CreateFolder(){
    	boolean exists = (new File("plugins/SubscriptionManager/")).exists();
    	if (exists) {
    	    // do nothing
    	} else {
    		boolean successmainfolder = (new File("plugins/SubscriptionManager/")).mkdir();
      			  if (successmainfolder) {
      				  log.info("Folder created!");
      			  }
    	}
    }
    
 
    
    public void onDisable() {
        //this method is called once your plugin is diabled you can save stuff or clean up here
    }
 
    /*
    * The on command method is called every time a command which is registered for your plugin is called
    * @sender: this is the sender of the command. Can be a player or the command line
    * [USER=55020]CMD[/USER]: a command object. Look into the API docs for what you can do with it
    * @commandLabel: a string of which command has been issued. Useful if you have more than one command
    * @args: an array of arguments supplied to the command
    */
    /*public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
        if(cmd.getName().equalsIgnoreCase("basic")){ // If the player typed /basic then do the following...
            //doSomething
            return true;
        } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
        //you should always return true if the command has been processed by your plugin and false if not
        return false;
    }/*
 
    /*
    * this is an event hanldler event handlers have to be in classes which implement Listener
    * the @EventHandler annotation marks this method as an event handler
    * the method name can be anything you like
    * event handler have one parameter which also defines which event they handle
    */
    
    @EventHandler
    public void loginHandler(PlayerLoginEvent event) {
        /*
        * this method is called every time a player logs in you can find out more about the event
        * via the given parameter
        * e.g. we can determine which player logged in and send him a welcome message
        */
    	String username = event.getPlayer().getDisplayName().toLowerCase();
    	if(username.contains(" ")){ //anti SQL-injection
    		event.getPlayer().kickPlayer("Your username contains a space, which is not allowed for security reasons!");
    	}else{
    		if(username.contains("'")) {
    			event.getPlayer().kickPlayer("Your username contains a single quote, which is not allowed for security reasons!");
    		}else{
    			if(username.contains(";")){
    				event.getPlayer().kickPlayer("Your username contains illegal characters!");
    			}else{
    				CheckSubscription(username);
    			}
    		}
    	}
        
    }
 
    /*
    * This is another event handler with high priority. So if there are other handlers for the same
    * event this one will be called first
    */
    
    private String getDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date();
        return dateFormat.format(date);
    }
    
    public void CheckSubscription(String username) {
    	String CurrentDate = getDate().toString();
    	
    	boolean exists = (new File("plugins/SubscriptionManager/" + username + ".txt")).exists();
    	if (exists) {

    	    Connection con = null;
    	    String url = "jdbc:mysql://thomasdomein.nl:3306/";
    	    String db = "dbname";
    	    String driver = "com.mysql.jdbc.Driver";
    	    try{
    	    	Class.forName(driver);
    	    	con = DriverManager.getConnection(url+db,db.toString(),"pass");
    	    	try{
    	 	Statement st = con.createStatement();
    	 	ResultSet val = st.executeQuery("SELECT `user`, `date` FROM `users` WHERE `user` = '" + username +"'");
    	 	log.info(val.getString("date"));

    	    	}
    	    	catch (SQLException s){
    	    		System.out.println("SQL statement is not executed!");
    	    		s.printStackTrace();
    	    	}
    	  }
    	  catch (Exception e){
    		  e.printStackTrace();
    	  }

            
    	} else {
    		System.out.println("Inserting values in Mysql database table!");
    	    Connection con = null;
    	    String url = "jdbc:mysql://thomasdomein.nl:3306/";
    	    String db = "dbname";
    	    String driver = "com.mysql.jdbc.Driver";
    	    try{
    	    	Class.forName(driver);
    	    	con = DriverManager.getConnection(url+db,db.toString(),"pass");
    	    	try{
    	 	Statement st = con.createStatement();
    	 	int val = st.executeUpdate("INSERT INTO `w2278368_mcpay`.`users` (`id`, `pincode`, `user`, `date`) VALUES (NULL, '', '" + username.toString() + "', '"+ CurrentDate +"');");
    	 	System.out.println("Successfully wrote new user " + username.toString() + " to database!");
    	 	Writeuserfile(username);
    	    	}
    	    	catch (SQLException s){
    	    		System.out.println("SQL statement is not executed!");
    	    		s.printStackTrace();
    	    	}
    	  }
    	  catch (Exception e){
    		  e.printStackTrace();
    	  }
    	}
    	  
    }
    
    public void Writeuserfile(String username){
    	try {
  		  File f;
  		  f=new File("plugins/SubscriptionManager/" + username + ".txt");
			  f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
    	  
    }
    
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    	
        
            return true;
	}
}

Mijn database ziet er als volgt uit:
Code:
CREATE TABLE IF NOT EXISTS `users` (
  `id` int(6) NOT NULL AUTO_INCREMENT,
  `pincode` text COLLATE latin1_general_ci NOT NULL,
  `user` varchar(128) COLLATE latin1_general_ci NOT NULL,
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=3 ;

Dus mijn probleem is nu, hoe kan ik de datum die bij de username die inlogt uit de database halen en dan kijken of de datum in het verleden ligt of in de toekomst?

Groeten,
Djmaster329
 
Status
Niet open voor verdere reacties.

Nieuwste berichten

Terug
Bovenaan Onderaan