Page 1 of 1

request for very simple SQL extension

Posted: 06 Jun 2011, 18:56
by rutabuga
can any one give a very simple code snippet that:

conects to an SQL server
querys and a data value

all help is apreciated

ive tryed the following which dosnt seam to work

Code: Select all

IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
		Connection connection;
		try {
			connection = dbManager.getConnection();
			PreparedStatement stmt = connection.prepareStatement("SELECT pword,id FROM muppets WHERE name=Kermit");
			ResultSet res = stmt.executeQuery();
			dbPword = res.getString("pword");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
:x :?:

Posted: 07 Jun 2011, 07:43
by Sigtran
how did you determine that it doesnt work? e.g. did you print out the results / sent it to the client?

You can try the following, if you like:
on the server:

Code: Select all

addRequestHandler("getMuppets", GetMuppets.class);
and create an extension:

Code: Select all

import java.sql.SQLException;
import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;
import com.smartfoxserver.v2.extensions.ExtensionLogLevel;

public class GetMuppets extends BaseClientRequestHandler
{
	@Override
	public void handleClientRequest(User theUser, ISFSObject params)
	{
		IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
		String sql = "SELECT pword,id FROM muppets WHERE name=Kermit";
		trace(sql);
		
		try
		{
			ISFSArray result = dbManager.executeQuery(sql);
			ISFSObject response = new SFSObject();
			response.putSFSArray("Muppets", result);
			
			// Send back to requester
			send("getMuppets", response, theUser);
		}
		catch (SQLException e)
		{
			trace(ExtensionLogLevel.WARN, "SQL Failed: " + e.toString());
		}
	}
}
Hope you can figure the Client side and the above helps. The prepared statement you have created seems correct to me, so just make sure you are actually tracing it / logging / requesting /etc :)