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
MYSQL -> PHP -> JAVA
If you have worked with actionscript loadvars / urlloader this will be very simple to understand.
Java Code Snippet:
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");
URLConnection conn = url.openConnection();
writer = new OutputStreamWriter(conn.getOutputStream());
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
result = (String) reader.readLine();
result = null;
writer = null;
reader = null;
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:
$name = $_REQUEST['name'];
$name = "Hello ".$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:
the output in log is expected as:
Hope this helps 🙂