So, I figured out the solution. I hadn't mentioned the port name in the url connection to the database.
String url = "jdbc:mysql://" + server_name + "/" + database;
Probably, hence it led to many leaking connections. Once done, I tried with the example given here. It now doesn't throw any error. Thanks to BalusC, as I figured this out only due to his comment on changing the port number on MySQL. To add, the way to change the MySQL port number is NOT by changing the my.ini file but by running the MySQL instance config wizard under Start -> Programs -> MySQL Server 5.1 -> MySQL Server Instance Config Wizard. It was also interesting to note the code didn't throw any error when the port number wasn't specified and the program ran smoothly. Probably, JDBC connects to 3306 by default. If anyone has any particular idea about the same, please share.
For benefit of folks, here's the source code:
DBUtils.java
package DB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import com.mysql.jdbc.Driver;
/**
* @author decorrea
*
*/
public class DBUtils {
public static String jdbc_driver_name = "com.mysql.jdbc.Driver";
private static String server_name ;
private static String database;
private static String username;
private static String password;
private static int maxActive = 20;
private static int maxIdle = 2 ;
public String getServer_name() {
return server_name;
}
public void setServer_name(String serverName) {
server_name = serverName;
}
public String getDatabase() {
return database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static DataSource getDataSource(String server_name, String database, String username, String password){
BasicDataSource datasource = new BasicDataSource();
datasource.setDriverClassName(jdbc_driver_name);
String url = "jdbc:mysql://" + server_name + "/" + database;
System.out.println(url);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setUrl(url);
datasource.setMaxActive(maxActive);
datasource.setMaxIdle(maxIdle);
return datasource;
}
}
TwitterAPI.java
public class TwitterAPI {
private static String server_name = "localhost:7777";
private static String twitter_databse = "twitter";
private static String username = "root";
private static String password = "password";
public static String twitter_unique_usernames_file = "twitter_unique_usernames_file.txt";
public static String language_model_file = "C:\\de\\JARS\\lingpipe-4.0.0\\demos\\models\\langid-leipzig.classifier";
public static DataSource dataSource = DBUtils.getDataSource(server_name, twitter_databse, username, password);
public static Connection startDBConnection(String server_name, String database, String username, String password) {
//Set DB parameters
//DBUtils mysql_obj = setDBParams(server_name, database, username, password);
Connection connection = null;
//connection = mysql_obj.createConnection();
try {
connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public static DBUtils setDBParams(String server_name, String database, String username, String password){
DBUtils mysql_obj = new DBUtils();
mysql_obj.setServer_name(server_name);
mysql_obj.setDatabase(database);
mysql_obj.setUsername(username);
mysql_obj.setPassword(password);
return mysql_obj;
}
public static String getTweets(BigInteger id){
Connection connection = startDBConnection(server_name,twitter_databse,username,password);
ResultSet resultSet = null;
String tweet = new String();
try {
Statement statement = connection.createStatement();
String query = SQL_queries.get_tweets_on_id + id.toString();
//Execute the query
resultSet = statement.executeQuery(query);
while(resultSet.next()){
tweet = resultSet.getString("content");
}
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
finally{
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return tweet;
}
Hope this helps.