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 :)

Popularity: 16% [?]




December 22, 2009   Posted in: Java, Php, Red5, RTMP

19 Responses

  1. arkansas transportation jobs - March 7, 2011

    Find Jobs in My State…

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  2. Grado Sr80i - March 10, 2011

    Steve’s Super Site…

    [...] You may leave a response, or trackback from your personal webpage. Learn more via the original source: Geeked Up: В« Tech blog Share and [...]…

  3. Article submission service - March 11, 2011

    Article submission…

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  4. salon greenville nc - March 12, 2011

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  5. Smoked Salmon Delivered - March 13, 2011

    Smoked Salmon Delivered…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  6. local mobile monopoly - March 16, 2011

    local mobile monopoly…

    [...]below you’ll find the link to some sites that we think you should visit[...]…

  7. Anonymous - March 17, 2011

    how to tone your stomach…

    [...]the time to check out sites we have linked to underneath the[...]…

  8. online mlm training - March 17, 2011

    online mlm secrets…

    [...]the time to read or visit the content or sites we have linked to below the[...]…

  9. Magniwork - March 19, 2011

    Magniwork…

    I found your blog via blog catalog. I thought I’d check out the post as I just wrote on this subject today. You add some great suggestions that I didn’t have. Thanks for the tips….

  10. topamax lawsuits - March 25, 2011

    Nintendo 3DS…

    [...]I am normally looking for value. This article makes so much. just want to keep tabs on[...]…

  11. motorhome rentals denver - March 29, 2011

    how do i toilet train a puppy…

    [...]Sites of interest we like to link to[...]…

  12. golf paintings - April 3, 2011

    richard chorley golf…

    Thanks for such an excellent page, bookmarked, ill visit soon!…

  13. safety signs - April 4, 2011

    safety signs…

    Thanks for such an excellent page, bookmarked, ill visit soon!…

  14. laptop charger - April 4, 2011

    laptop charger…

    Thanks for such an excellent page, bookmarked, ill visit soon!…

  15. Acupressure To Induce Labor - April 6, 2011

    Acupressure To Induce Labor…

    Thanks for such a brilliant blog post, bookmarked and shared!…

  16. 7 Day Programmable Thermostat - April 6, 2011

    7 Day Programmable Thermostat…

    Thanks for such a brilliant blog post, bookmarked and shared!…

  17. How To Treat Bacterial Vaginosis - April 6, 2011

    How To Treat Bacterial Vaginosis…

    Thanks for such a brilliant blog post, bookmarked and shared!…

  18. Dremel Stylus - April 6, 2011

    Dremel Stylus…

    Bookmarked your site, so I can visit frequently, thank you!…

  19. Lose belly fat fast - April 8, 2011

    How to lose belly fat…

    [...]Sites of interest we like to link to[...]…

Leave a Reply