Red5 to mysql without JDBC

In this tutorial we see how to connect to a mysql database, regardless of whether you have access to JDBC adapter on your Red5 running server. Definitely JDBC would be faster, but this method will get you across just fine in any Red5 project involving databases.

Since we wont be using Java way of connecting to mysql, i pick php as the delegate for that. so our communication will be like this:

JAVA -> PHP -> MYSQL

and

MYSQL -> PHP -> JAVA

If you have worked with actionscript loadvars / urlloader this will be very simple to understand.

Java Code Snippet:

public String sayHello(String name) throws IOException
{
OutputStreamWriter writer = null;
BufferedReader reader = null;
URL url=new URL("http://localhost/red5phpresponser.php");
String result = null;

String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8");

try  
{  
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
   
    writer = new OutputStreamWriter(conn.getOutputStream());
    writer.write(data);
    writer.flush();
   
    reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    result = (String) reader.readLine();
   
}  
catch(Exception e)  
{  
    result = null;
}
finally
{
    writer.close();
    writer = null;
    reader.close();
    reader = null;
}


return result;
}

As you can see , the above function takes a parameter “name” and send it to php via a normal URL encoded call. The php script will thereof capture these variables as if sent from browser. Bellow is the corresponding php script red5phpresponser.php located at my www root.

Php Code Snippet:

<?php
$name = $_REQUEST['name'];
$name = "Hello ".$name;
print $name;
?>

the above code concatenates “Hello ” to sent parameter and prints to output. (“Hello flashvisions”). The outstream of php becomes the input stream for our previous java code as it reads the print parameters using readline().

Hence all together if we use this function as:

try{
Log.info(sayHello("flashvisions"));
}catch(Exception e){
}

the output in log is expected as:

Hello flashvisions

Hope this helps 🙂

If you have trouble getting emails from this domain, please check your email spam & mark messages from flashvisions.com as 'Not Spam'

Comments are closed