Jump to content

join in JDBC


herghost

Recommended Posts

Hi all

 

I am wondering if someone could help me with a join statement, basically I dont have a clue where to start! I have read some sql tut;s but its beyond me.

 

Basically I am writing a script which checks a users balance from table 'money'and then updating the balance minus the cost of a action which is an int.

 

The table money is simple

p_name, balance

 

basically the steps I have are:

1, get current balance

2, if conditions met, subtract int from current balance

3, update balance

 

The problem I have is that in JDBC a while loop will cause an java output error in console even though the command operates as it should if you use more than one statement

 

This is a plugin for minecraft.

 

My code

package me.herghost.Fiery.commands;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import me.herghost.Fiery.util.Configuration;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;


public class banCommand implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
{
	readCommand((Player) sender, commandLabel, args);
	return true;
}

@SuppressWarnings("unused")
private void readCommand(Player player, String cmd, String[] args )
{


	if(cmd.equalsIgnoreCase("ban")&& player instanceof Player)
	{

		Bukkit.getOfflinePlayer(args[0]).setBanned(true);
		if (Bukkit.getPlayer(args[0]) != null) Bukkit.getPlayer(args[0]).kickPlayer("Banned by admin.");
	    Command.broadcastCommandMessage(player, "Banning " + args[0]);
	    
	    
	    
		String user = Configuration.getString("settings.mysql.user");
		String pass = Configuration.getString("settings.mysql.pass");
		String url = "jdbc:mysql://localhost:3306/Fiery";


		String v = Configuration.getString("money.iscalled");
		boolean t = Configuration.getBoolean("money.isenabled");
		int cost = Configuration.getInt("commandcharge.ban");

		int balance;
	     Player p = (Player) player;
	    


		try
		{
			Connection conn = DriverManager.getConnection(url, user, pass);

			Statement select = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
			ResultSet rs = select.executeQuery("SELECT balance FROM money WHERE p_name ='" + p.getName() + "'"); 
			while (rs.next()) 
			{
				balance = rs.getInt("balance");

				if(t = true && cost > 0)
				{
					int nbalance;
					if(cost < balance )
					{
						nbalance = balance - cost;
						int rs1 = select.executeUpdate("UPDATE money SET balance = '" + nbalance + "'WHERE p_name ='" + p.getName() + "'"); 
						p.sendMessage("You have been charged " + cost + " " + v + " - your new balance is " + nbalance + " " + v + "");

												}
					else
					{
						p.sendMessage("Sorry, your balance is to low to execute this command");
					}

				}

			}
			select.close();
		}

		catch
		(SQLException e1) 
		{
			e1.printStackTrace();
		}

		}

	}
}

 

and the error

 

2011-11-30 22:58:19 [sEVERE] java.sql.SQLException: Operation not allowed after ResultSet closed
2011-11-30 22:58:19 [sEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
2011-11-30 22:58:19 [sEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
2011-11-30 22:58:19 [sEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
2011-11-30 22:58:19 [sEVERE] 	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
2011-11-30 22:58:19 [sEVERE] 	at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:794)
2011-11-30 22:58:19 [sEVERE] 	at com.mysql.jdbc.ResultSetImpl.next(ResultSetImpl.java:7145)
2011-11-30 22:58:19 [sEVERE] 	at me.herghost.Fiery.commands.banCommand.readCommand(banCommand.java:61)
2011-11-30 22:58:19 [sEVERE] 	at me.herghost.Fiery.commands.banCommand.onCommand(banCommand.java:23)
2011-11-30 22:58:19 [sEVERE] 	at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
2011-11-30 22:58:19 [sEVERE] 	at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:163)
2011-11-30 22:58:19 [sEVERE] 	at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:364)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:756)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:721)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:714)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.Packet3Chat.a(Packet3Chat.java:33)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.NetworkManager.b(NetworkManager.java:226)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:92)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.NetworkListenThread.a(SourceFile:108)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:516)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:414)
2011-11-30 22:58:19 [sEVERE] 	at net.minecraft.server.ThreadServerApplication.run(SourceFile:457)

 

Link to comment
https://forums.phpfreaks.com/topic/252163-join-in-jdbc/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.