Monday, October 29, 2012

Gujarat Assembly Elections: what an occation!

Gujarat Assembly elections are set to be held from 13th to 17th December. Well what a magnificent time to be in Gujarat and it can never be any bigger! When I was a school kid, elections meant to me just for some school holidays but now when I am concerned about the country and especially the region where I grew up, its feels amazing to follow the news.

On one hand, Modi is preparing hard to become the fourth time Chief Minister of one of the fastest developing states contributing the most to the national economy. On the other side talks are taking strolls about him to take a bigger national role.

Last month I was in Trento, Italy and with some friends we were discussing the Modi (with passionate Dhaval talking about "Vikas" -> "Development" in Gujarat) about what can be his strategy this time to win.

In any case, the Assembly elections were never so interesting without Modi. I grew up in Gandhinagar - the capital of Gujarat where all these ministers live and do campaign very often. I remember last time when Modi set the stage just few meters away from my house and how the people gatherd! He is really an ear-candy! His typical outspoken, blunt and direct personal remarks take heart whether it can be Prime Minister of India, President of opposition party or Army Chief of Pakistan. This time in news I see Modi calling Prime Minister "Moun Mohan Singh" and Shashi Throor's wife "50-crore-rupee girlfriend"!! I can imagine how it will be in Gujarat and I miss it, hopefully when I am back to Gandhinagar this December, at least once I see him live.



On the other hand, If he fails this time which looks extremely difficult, it would be chaos in Gujarat, because the next CM will definietely not be able to keep up with the speed he has created. I have always be opposer of Modi and probably will be because of 2003 elections but I think the Time and Nation really needs some one like him. USA rejected his visa and he responded "I don't need visa of US but I want to see them taking visa of Gujarat.." and ironically in Gujarat Vibrant Summits many international delegates just queue up to invest. After running the state for last 3 terms cumulated to more than 10 years, Gujarat is all set to evaluate the government and it would really be make or break time for him. Lets see!

Update: He won the fourth time. From then, he has started addressing public in "Hindi", increased his visits and appearances in Delhi, there is excitement in BJP about him with outlook for 2014 national elections but there is agitation too over him. 

Tomcat JSP MySQL Linux -- A Starting Kit

This is just to get you started if you are interested in the above configuration. I spent a lot of time exploring many forums for the errors encountered meanwhile which I try to dump here.

First you need following packages:

1. tomcat, tomcat-webapps, tomcat-admin-webapps -- try to get through yum or apt-get repositories.
2. mysql -- again try to do the same to get it

Start all the services:
service mysqld start
service httpd start
service tomcat start

To check if these things are working properly:

1. mysql:

mysql -hlocalhost -uuser -ppassword -e "show databases;"


Note: there is no space between "-u" and "user".

This should show the present databases created by the specified user.

Alternatively you can access the mysql from the browser through:

http://localhost/phpMyAdmin/

For this will need the package "phpmyadmin" installed. Make sure, each time you do some administrative changes, restart services.

2. tomcat:

You should be able to view the examples provided by the tomcat in the browser. To start with a new project, the place of interest will be "webapps" directory where you have to put your project root directory. Create a directory say "test" in "webapps" and in which create a small static html page say "test.html". Try to access the page from your browser with URL

http://localhost:8080/test/test.html

With that you are ready with your basic set up and should move forward with the mysql integration.
Now the basic page which accesses some table of a mysql database is shown below:
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.sql.*" %>

<form>
<head>
<title>Testing the structure of jsp, mysql and tomcat!</title>
<style>
* { font-size: 12px; font-family: Verdana }
</style>
</head>
<div>

<%

Statement stmt = null;
Connection con = null;
ResultSet resultSet = null;
String url = "jdbc:mysql://localhost/dbname?characterEncoding=UTF-8";
%>lang1
<h2>Display of data from Database</h2>
<table>
 <tr>
  <th>col1</th>
  <th>col2</th>
  <th>col3</th>
  <th>col4</th>
 </tr>
 <%
 try {
  String input = request.getParameter("lang1");
  Class.forName("com.mysql.jdbc.Driver");
  con = DriverManager.getConnection(url, "username", "passwd"); 

  stmt = con.createStatement();
  resultSet = stmt.executeQuery("select * from table where col1 = \""+input+"\"");
  while (resultSet.next()) {%>
   <tr>
    <td><%=resultSet.getString(1)%></td>
    <td><%=resultSet.getString(2)%></td>
    <td><%=resultSet.getFloat(3)%></td>
    <td><%=resultSet.getInt(4)%></td>

   </tr>
  <%}
 } catch (SQLException e) {
  e.printStackTrace(System.out);
 } 
 finally {
  try {stmt.close();} catch (Exception e) {
  }
  try {resultSet.close();} catch (Exception e) {
  }
  try {
   if (null != con) {con.close();}
  } catch (SQLException ex) {
   ex.printStackTrace(System.out);
  }
 }
 %>

</table>

</form>
</body>
</html>
In summary, the above code gets a string from the previous page some input in terms of form data. Tries to access the database and gets all the records where col1 value is equal to input and prints it. If you get some java-like errors saying that MySQL is unknown class then you should perform following operations.
The files of interest is:
/usr/share/tomcat/conf/catalina.policy

First you should grant the permission to access the mysql.
grant codeBase "file:${catalina.base}/webapps/test/-" {

     permission java.net.SocketPermission "localhost:8080", "connect";

};

And then make mysql known to tomcat by adding jar in classpath and for that, put mysql-connector-xyz.jar in tomcat_home/lib directory. Add its reference to the classpath by adding following line in tomcat_home/bin/catalina-tasks.xml between

<fileset file="${catalina.home}/lib/mysql-connector-xyz.jar"></fileset>

and restart tomcat service!