Archive for May, 2007
JRuby is a 100% pure-Java implementation of the Ruby programming language that runs in the JVM. MySQL is a one of the most popular open source databases around and is used by many prominent organizations from Yahoo to NASA.
This brief tutorial demonstrates how to install and configure JRuby to connect to the MySQL database. The tutorial is written for beginners who are familiar with Java code and JDBC. Exposure to Ruby will make the syntax easier to follow. You don’t need any specific integrated development environment (IDE) or tool knowledge. Familiarity with a text editor and setting environment variables is required.
The tutorial assumes that your Java, JRuby and MySQL 4.1+ environment is successfully installed and configured.
Creating a Database and User
Again, I am assuming that you have MySQL running and are familiar with the basics. Run the mysql client program from the command line (as shown below) so that we can execute some administration commands.
C:/mysql/bin> mysql –user=root mysql
First create a database called “ruby” within MySQL and the user is root.
mysql> CREATE DATABASE ruby;
Query OK, 1 row affected (0.02 sec)
and check that it has been created using
mysql> SHOW DATABASES;
+————+
| Database |
+————+
| ruby |
| mysql |
| test |
+————+
3 rows in set (0.01 sec)
Let’s create a table in our database ruby
use ruby;
create table student (id VARCHAR(2), name VARCHAR(20), rank VARCHAR(2));
To check whether the table has been created, type:
show tables;
+———————+
| Tables_in_ruby |
+———————+
| student |
+———————+
1 row in set (0.00 sec)
To verify your table, type:
mysql> describe student;
+——–+—————-+——+——+———–+——–+
| Field | Type | Null | Key | Default | Extra |
+——–+—————-+——+——+———–+——–+
| id | char(2) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| rank | char(2) | YES | | NULL | |
+——–+—————-+——+——+———–+——–+
3 rows in set (0.04 sec)
Now let us insert 3 records into student table:
mysql> insert into student values (’01′, ‘Peter’, ‘10′);
Query OK, 1 row affected (0.06 sec)
mysql> insert into student values (’02′, ‘Bruce’, ‘08′);
Query OK, 1 row affected (0.01 sec)
mysql> insert into student values (’03′, ‘Pat’, ‘06′);
Query OK, 1 row affected (0.00 sec)
Download type 4 pure JDBC driver
Download mySQL Connector/J Ver 3.1.10 or later open source JDBC driver for the above database. Unzip and Install the pure JDBC driver to C:/default folder ie. C:/mysql-connector-java-3.1.8
Next, copy the file mysql-connector-java-3.1.10-bin.jar to the lib folder of your Java installation and then add this .jar file location to your system environment variable classpath.
Connect to MySQL
Now that you’ve created a database and populated it with some data, your next task is to connect to that database using JRuby. To do so, create a new file named jrubyjdbc.rb. Open this file in a text editor, and enter the lines shown below:
require ‘java’
module JavaLang
include_package “java.lang”
end
module JavaSql
include_package ‘java.sql’
end
begin
JavaLang::Class.forName(”com.mysql.jdbc.Driver”).newInstance
conn = JavaSql::DriverManager.getConnection(”jdbc:mysql://127.0.0.1:3306/ruby?user=root”);
stmt = conn.createStatement
rs = stmt.executeQuery(”select name from student”)
while (rs.next) do
puts rs.getString(”name”)
end
rs.close
stmt.close
conn.close()
rescue JavaLang::ClassNotFoundException
puts “ClassNotFoundException”
rescue JavaSql::SQLException
puts “SQLException”
end
The code should work and you should get the output as:
C:/rubyprograms>jruby jrubyjdbc.rb
Peter
Bruce
Pat
Some code explanation
- Create a Ruby module into which you put all the classes in java.sql. Because you’re using numerous classes from the java.sql package, it
makes sense to include the whole package. Keep in mind that including the package slows your script. This use of a module is a convenient way to access all of these classes. - There can be name clashes between Java class names and Ruby class names. Class is an example of this; Java has java.lang.Class and Ruby also has Class. To resolve this name clash, define a Ruby module that includes the Java class definition.
- There’s no need to declare the type of the variable named connection. Ruby is a dynamically typed language; therefore types aren’t required for variable declarations.
- Class.forName and DriverManager.getConnection both throw checked exceptions. In Java code, the equivalent code won’t compile until you do something with those exceptions. Ruby doesn’t have checked exceptions, so you aren’t forced to handle them. However, when writing an application, you generally want to do something with exceptions if you can. Two checked exceptions could arise from your code: ClassNotFoundException and SQLException. Exceptions in Ruby are handled similarly to the Java approach. You have a block starting with begin instead of try and ending with end instead of }. Instead of catch, you use rescue.
Go on! Digg It !
Technorati Tags: Connect JRuby to MySQL using JDBC, Java, JDBC, JRuby, MySQL, Ruby
Posted by Satish TalimVincent Spehner heads the company ThinkDRY, based in Pune. Recently, I had a chance to talk to him and requested him to share some of his experiences. Here goes.
Could you tell us something about yourself - your background, where you are based?
I come from Marseille in the south of France and I am a University Engineer specialized in Sciences, Computer Sciences and Artificial Intelligence. I am still studing Philosophia through an e-learning program of the University of Provence.
Could you tell us a little more about ThinkDRY?
ThinkDRY is a small but fast growing WEB 2.0 Engineering company. We are mainly working using Rails framework and Ajax. We also have skills on Flash, if the customer asks for. We are actually 6 and plan to hire 5 others RoR experts by the end of the year.
All our customers are from France. They ask for a French speaking contact and contract under French laws because they dont trust outsourced plateforms. That is what we offer.
Why did you choose Ruby on Rails (RoR) as a framework for ThinkDRY?
I discovered Ruby on Rails, eight months ago for a R&D personnal project. I have been using PHP last five years, but when you see how simple it is to make a basic DB application using Rails, you give up on PHP for sure!
Were there any surprises in working with RoR?
Yes. The community is very active but not well structured, in my point of view. Information is available but sometimes very difficult to find. You have to scroll many forums to find the tips you were looking for. Furthermore, there is no real documentation on line.
You have an off shore development center in Pune. What is your impression about the RoR scene in India?
I have worked with many Freelancers here. The main thing I can say is: they are not reliable at all. Amazing!! They always have a good reason for not finishing the work. Just two of them were really efficient. We are still working together. Concerning the RoR scene in India: I dont know it very well. I spend all my week working for ThinkDRY. You know that in a startup it is 70 hours of work per week!
What advice would you give startups about platform choices?
For a classic Web Application (95% of modern web site) use Ruby on Rails without hesitation. But if you are thinking about some very tricky stuff, may be PHP is still a good solution.
Getting back to ThinkDRY, what are your future plans?
We should deliver our third project during the next few months and two other projects are already in the pipe. We simply plan to conquer the European Market with the help of our numerous contacts in Germany, England and France. For this purpose we are in the process of opening a French office. Another one will be open next year, in Germany.
Technorati Tags: France, India, Pune, ThinkDRY, Vincent Spehner
Posted by Satish TalimLike Peter Cooper, I too constantly get emails from various readers of RubyLearning.com, who are looking for Rails developers in India for their projects. As I don’t do this myself, I have to keep giving out a list of Rails developers I know and trust or have had good feedback about.
My previous blog post Ruby on Rails in India just lists down all the companies working in Rails in India.
Here, I am listing down companies (in no particular order) I’d recommend and will keep it updated as a resource for everyone to use.
If you have worked with any of these companies or with some company not listed here, please do comment about your experience and why you would recommend or not recommend that company..
(Disclaimer: None of these companies has ever paid me anything and none requested this post.. just to be clear!)
- Blue Whale Labs (Contact Person: Ranvir Gujral), Delhi provides strategic consulting, conceptualization, design, and development for innovative social applications. They have an extensive history in usability, user experience, design, and web application development. They couple that expertise with deep and current technology domain knowledge to satisfy complex client requirements. They also provide a thorough understanding of business, operations, and, most importantly, how to launch robust web applications from scratch with limited time and budget.
The Blue Whale team is led by seasoned social software architects and strategists. They bring specific end-to-end knowledge of the challenges that face today’s emerging and established social application providers. Blue Whale Labs was founded on the premise that social application providers are best served by a firm that completely understands and can deliver upon the emerging social technology topology. - BetterLabs (Contact Person: Vaibhav Domkundwar), Pune is a product development company focused on building products that address critical pain points and deliver an easy and simple user experience. Their products fall in the consumer Internet and web-based business application segments. At BetterLabs, they have created an unique model for new product development where they build products and aim to take them through the first 12-18 months to demonstrate a successful revenue model, at which point, such products may be spun out. They use this model for their internal product development and work with very early stage Silicon Valley startup founders to build their product from the concept stage to go live, acting as their product development team. They primarily develop in Ruby on Rails and PHP. The products that they have delivered so far include:
- dealplumber
- iNods
- iLetYou*
- YouStyleMe.com (not yet launched)*
- NotAProblem, Inc. (not yet launched)*
- ChitChat, Inc. (not yet launched)*
- Friendput.com (not yet launched)*
- CityFile, Inc. (not yet launched)*
* = all these are startup companies in the US.
- Vinayak Solutions (Vinsol - Contact Person: Manik Juneja), Delhi is a small “boutequish” web application development firm. They have been working on Ruby on Rails exclusively for more than an year now, working on client projects, doing consulting gig and even providing training. They are probably the only Indian company to have released Rails plugins. They have also released a Paypal library at ruby gems. Another area which is their forte is Rails deployment’. They have experience doing automated deployment using Capistrano on standalone servers to clusters with providers ranging form Textdrive (Joyent) and RimuHosting to Engineyard. They are a test driven, agile team. They implement scrum for executing their projects. Some of their successful projects on Rails have been nosuni.com and boostedmobile.com
- Synerzip (Contact Person: Vinayak Joglekar), Pune is a company based in Dallas, TX with its Offshore Development Center in Pune, India. Synerzip focuses on helping early stage and mid level companies to develop innovative software products using the agile methodology of development. Synerzip has developed more than a dozen products for various companies in the legal, supply chain, manufacturing, healthcare and telecommunication verticals. Their focus is on technologies like J2EE, .Net and Ruby on Rails. All the products developed by Synerzip are having a sizeable component that is web based and uses MVC architecture using frameworks like Struts. They have made use of ORM layer using frameworks such as Hibernate or Ibatis in almost all of their products. They have just signed their first customer using Ruby on Rails. They are planning to aggressively recruit talent and create a high quality practice for serious work in Ruby on Rails.
- CircleSource (Contact Person: Sanchit Jain), Bangalore provides software product engineering services designed to reduce time-to-market, development and maintenance costs, risks, and enable the customer to focus on the business intelligence of the product. They are located in Burmingham, Alabama and Bangalore, India. They are ISO 9001:2000 certified. They are an STPI (software technology parks of India) unit. They have worked extensively on Ruby on Rails and have a 10 people team working on rails projects. They are currently working on a healthcare application, a social networking application, and an event management application using rails. They are using advanced features of Ruby on Rails / AJAX and are also working on a framework to deploy large rails applications on distributed servers.
- MangoSpring (Contact Person: Vishwa Malhotra), Pune is a product technology startup focused on building community and communication related network of services. These services are platform agnostic and would offer a seamless, end to end user experience across multiple mediums. They truly believe that these services will make you wonder how you managed without them so far! They love it when they are able to use upcoming technologies to rapidly build innovative solutions. So as a company one of their latest excitement is Ruby on Rails. They put the money where their mouth is and so are currently building majority of their server systems on the Rails framework. These servers will provide rendering and back-end services to both Web 2.0 and Mobile clients enabling users to have seamless access to communication and collaborations services anytime, anywhere.
Information
There is a lot of fuss around about startlogic hosting. They not only offer the services of windows hosting but also they offer linux based hosting through dedicated servers. They also take regular data backup and provide the facility of the number of times you want them to take backups. They also remind you to renew domains when your domain is about to expire plus they have ppc advertising plans and offer various tutorials not only on how to use them on your website but also they have various tutorials about wireless internet how, how to use different databases etc. Indeed they are offering quality web hosting.
Technorati Tags: Delhi, India, Pune, Rails Developers in India, Ruby on Rails
Posted by Satish Talim



