Share & Learn

Tuesday, November 13, 2007

Integrating Flex and Servlets

We will create an application that will read names and profession from a database and display them to the user.

1. Create a Servlet, name it ReadData, and paste the following code under the doGet/ doPost method. Create a DSN for your table (Employee) using ODBC (Control panel => Administrative Tools)

========================================================================

response.setContentType("text/xml;charset=UTF-8");
PrintWriter out = response.getWriter();
String driver ="sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:odbc:Priyank";
String username = "";
String password = "";
try {
// Load database driver if not already loaded.
Class.forName(driver);
// Establish network connection to database.
Connection connection = DriverManager.getConnection(url,password, username);

String query = "SELECT empid,ename FROM Employee";
String xml = "";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query);
while(rs.next())
{
xml = xml + "" + rs.getString("empid") + "" + rs.getString("ename") + "";
}
xml = xml + "
";
rs.close();
stmt.close();
out.println(xml);
}catch(Exception e)
{
out.println(e);
}
finally {
out.close();
}
2. Start Abode Flex Builder 2.0. Create a new Project and add an MXML application.
I have kept the code in my code bin(repository for your code) Code
follow the link.

If you guys need any explanation with the code, ping me

blog comments powered by Disqus
blog comments powered by Disqus