Tuesday, December 8, 2009

Mysql-PHP in JAVA using quercus resin server in windows

Mysql connection with the quercus resin server from java module

In the previous post you can see the installation of the resin server with the hello world example now in this post you can see the mysql connection with the java module in quercus resin server

Here I want to show that basic php-mysql application can be make it run with the java module using resin server where you can write db connection in java and make use of connection in php pages

List of API or jar files required to execute this .

1.mysql-connector-java-5.1.5-bin.jar (from mysql’s download page) (http://www.mysql.com/products/connector/)
2. quercus.jar (in the quercus zip from caucho)
3. resin-util.jar (in the quercus zip from caucho)
4. script-10.jar (in the quercus zip from caucho)
Put all this jar files in lib folder

Follow the steps to setup the mysql-php-java module

Step 1.
Inside \resin-pro-3.1.9\webapps\ROOT\WEB-INF folder create web.xml with the following code

servlet-class="com.caucho.quercus.servlet.QuercusServlet"/>



jdbc:mysql://localhost:3306/test
root





Step2.
inside \resin-pro-3.1.9\webapps\ROOT\WEB-INF\classes\example create java class file with the

DBconnect .java

package example;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import com.caucho.quercus.module.AbstractQuercusModule;
public class DBconnect extends AbstractQuercusModule{
/**
* The DataSource for the table.
*/
private DataSource _ds = null;
/**
* Sets the data source.
*/
public void setDataSource(DataSource ds)
{
_ds = ds;
}
/**
* Initializes the reference to the CourseBean home interface.
*/
public String test_fun()
{
return "Hello raki ";
}
public String conn_db()
{
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String rtn_str=" ";

try
{
conn=DriverManager.getConnection("jdbc:mysql://localhost/testdb?" + "user=root"); //change testdb to your database name
stmt = conn.createStatement();
//rtn_str+="\n middle \n";
if (stmt.execute("SELECT * FROM table")) {
rs = stmt.getResultSet();
while(rs.next())
{
String filed1 = rs.getString("filed1");//change filed1 to your table column
String filed2 = rs.getString("filed2");//change filed2 to your table column
rtn_str+= " filed1:-"+ filed1 + " filed2:- " + filed2 +"
";
//rtn_str+=" \n"+rs.getInt(1)+"\n";
}
}

}
catch(Exception e)
{
}


//rtn_str+="<\pre>";
return rtn_str;
}


}

Step3.
inside \resin-pro-3.1.9\webapps\ROOT\WEB-INF\classes\META-INF\services create the following file with name “com.caucho.quercus.QuercusModule” put the content

example.DBconnect


step4.
Now inside \resin-pro-3.1.9\webapps\ROOT create the php page which calls the java module with name php_mysql_java.php

echo "your module data comes here “."
";
echo conn_db ().”
";

?>


Step 5.
Now run the hello_world.php from the URL as
http://localhost:8080/ php_mysql_java.php

You should see “your module data comes here
The data base values should comes here ”

No comments: