Browse DevX
Sign up for e-mail newsletters from DevX


Employing a Winning Porting Strategy : Page 3

Find out how you can cross-compile an open source ASP.NET web application to a Java EE application, to easily port it from Windows and SQL Server Express to Tomcat and MySQL.

Database Dialects
It was necessary to make some code changes to make the application compatible with the MySQL database. If you take a look at the code in the BlogEngine class, you'll see where changes were made to the data access methods. In this case, you can see a change that was required to support MySQL—handling DateTime—as an example. In SQL Express, the DateTime field looks like this:
5/1/2007 12:00:00 AM
This format is supported when using the .NET DateTime datatype to populate the corresponding database type. In MySQL, however, a DateTime field is formatted like this:
2007-04-01 12:00:00
If a .NET DateTime value is used to populate a MySQL DateTime column, MySQL registers an exception. To solve this communication breakdown, you can add a new property called DatePostedMySQL to the Post class, which returns a formatted string that complies with MySQL's DateTime. Take a look at how the existing DatePosted property is utilized:
public string DatePostedMySQL
    string _datePostedMySQL;
      DatePosted = DateTime.Now;

      StringBuilder sb = new StringBuilder();
      sb.Append(" ");

      string _datePostedMySQL = sb.ToString();
      _datePostedMySQL.Replace("{", "");
      _datePostedMySQL.Replace("}", "");

      return _datePostedMySQL;    
Generate and Configure the Java EE Project
Remove the original Java EE project from the solution. Right-click the Visual Studio project, and select Generate Java EE Project from the menu. The Generate Java EE Project wizard appears. The next screen gives you the option of saving the original ASP.NET solution file. Select this option, and continue to the next screen. Click OK and Grasshopper will jump into action, creating a new Java EE project.

The ported Java application will use the MySQL JDBC driver to access your MySQL server. If it's not already preconfigured in your Tomcat server, add the JDBC driver as a reference to your project. If you're wondering how to add a Java assembly to a Visual Studio project, Grasshopper takes care of it by using the tools it has integrated with your IDE. Right-click the References folder in your Java EE project, and select Java Reference from the list.

Select Browse, and go to the Java References folder in the Visual Studio project example. Select the mysql-connector-java-5.0.5-bin.jar file and click Open. Grasshopper processes the reference and adds it to the list. Change the "Blog" connection string in the project's web.config to specify this driver, the MySQL server IP address, database, and the login details:

<add name="Blog" 
    user=<<your username>>;
    password=<<your password>>" />

Thanks for your registration, follow us on our social networks to keep up-to-date