[SOLVED] Help with MySQL

Discussion in 'Plugin Development' started by Miles_, Jun 24, 2012.

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

    Miles_

    Im new to a lot of the coding here but Im trying to make it so that if someone types /register [password] it creates an account for my servers website. Right now Im just making it so I can type "register test testpass" in the console and it will add the user test with the password converted to a sha and a random salt. Right now it just returns SQL Error. Thanks.

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = null;if (sender instanceof Player) {player = (Player) sender;}
            if (cmd.getName().equalsIgnoreCase("register")){
                if (player == null) {
                    if (args.length > 2) {
                        return false;
                    }
                    if (args.length < 2) {
                        return false;
                    }
                    String username = args[0];
                    String password = args[1];
                    Random r = new SecureRandom();
                    byte[] salt = new byte[8];
                    r.nextBytes(salt);
                    MessageDigest md = null;
                    try {
                        md = MessageDigest.getInstance("SHA-256");
                    } catch (NoSuchAlgorithmException e1) {
                        e1.printStackTrace();
                    }
                    try {
                        md.update(password.getBytes("UTF-8"));
                    } catch (UnsupportedEncodingException e1) {
                        e1.printStackTrace();
                    }
                    byte[] passwordb = md.digest();
                    String passwordc = new String(passwordb);
                    String saltb = new String(salt);
                    System.out.println(username);
                    System.out.println(password);
                    System.out.println(passwordb);
                    System.out.println(passwordc);
                    System.out.println(salt);
                    System.out.println(saltb);
                    System.out.println(args[0]);
                    System.out.println(args[1]);
                    try {
                    Connection conn = DriverManager.getConnection("jdbc:mysql://my.database:3306","myusername","mypassword");
                                System.out.println("Inserting values in Mysql database table!");
                                PreparedStatement ps = conn.prepareStatement("INSERT INTO users(username,password,salt) VALUES(?,?,?)");
                                ps.setString(1,username);
                                ps.setString(2,passwordc);
                                ps.setString(3,saltb);
                                    ps.executeUpdate();   
                                sender.sendMessage(ChatColor.GREEN + "Successfully added user");
                                  }
                                  catch (SQLException s){
                                sender.sendMessage(ChatColor.RED + "SQL Error");
                                  }
     
  2. Offline

    hockeygoalie5

    What is the SQL exception? In your catch clause, add:
    Code:
    s.printStackTrace();
    
    and run it to get the exception.
     
  3. Offline

    Miles_

    I added it and found out I forgot to specify which database. It works now. Thanks
     
  4. Offline

    hockeygoalie5

    Glad to have helped.
     
Thread Status:
Not open for further replies.

Share This Page