MySQL Forums
Forum List  »  GIS

address already in use Unable to connect to any hosts due to exception
Posted by: Gopal Patil
Date: April 30, 2009 09:12AM

Hello All,

I am uploading as data from excel sheet to database.
and facing following exception.

----------------------------------------------------------------------
20:30:01,757 ERROR [STDERR] java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.BindException: Ad
dress already in use: connect
20:30:01,757 ERROR [STDERR] at com.mysql.jdbc.Connection.createNewIO(Connection.java:1719)
20:30:01,757 ERROR [STDERR] at com.mysql.jdbc.Connection.<init>(Connection.java:432)
20:30:01,757 ERROR [STDERR] at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:400)
20:30:01,757 ERROR [STDERR] at java.sql.DriverManager.getConnection(DriverManager.java:582)
20:30:01,757 ERROR [STDERR] at java.sql.DriverManager.getConnection(DriverManager.java:185)
20:30:01,772 ERROR [STDERR] at com.spotnana.dbConnection.DBConnection.select(DBConnection.java:66)
----------------------------------------------------------------------




###########################################################################

this is my DBConnection.java class

i am calling close connection after every select() statement i.e. select method of my DBConnection. and destroy() method in each iteration.

any one can tell me whats the problem???
thanks in advance.

------------------------------------------------------------------------
package com.spotnana.dbConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import com.mysql.jdbc.PreparedStatement;

//DBConnection Class used for Database connectivity

public class DBConnection {

Connection cn = null;
Statement stmt = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;

public DBConnection() {
try {
Connection cn = null;
Statement stmt = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
} catch (Exception e) {
e.printStackTrace();
}
}

// Login Function for Checking Successful Login in application
public void setAutoCommit(boolean commit) {
try {
cn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jnjcrm-23-04", "root", "root");
cn.setAutoCommit(false);
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public boolean login(String username, String password) {
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jnjcrm-23-04", "root", "root");
stmt = cn.createStatement();
rs = stmt.executeQuery("select * from login where Username='"
+ username + "' and Password='" + password + "'");
if (rs.next()) {
cn.close();
return true;
}

} catch (Exception e) {
e.printStackTrace();
}
return false;
}

// Select Function for getting data from Database

public ResultSet select(String query) {
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jnjcrm-23-04", "root", "root");
stmt = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
System.out.println("QUERY fired for SELECT STATEMENT :: " + query);
rs = stmt.executeQuery(query);
return rs;
} catch (Exception e) {
System.out
.println("*** Exception Occurred during SELECT QUERY ***");
e.printStackTrace();
} finally {

}
return null;
}

// Select Function for storing data in Database

public boolean insert(String query) {
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jnjcrm-23-04", "root", "root");
cn.setAutoCommit(false);
stmt = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
System.out.println("QUERY in INSERT:" + query);
int i = stmt.executeUpdate(query);
System.out.println("After update...!!!");
cn.commit();
cn.close();
System.out.println(" i--------------->"+i);
if (i == 0) {
return false;
} else {
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}

finally{
cn=null;
}
}

// this function is used to insert escape sequens into the databse

private String convertString(String str) {

if (str.contains("'")) {
str = str.replaceAll("'", "\'");
}

if (str.contains("=\'")) {
str = str.replaceAll("=\'", "='");
}

if (str.contains("(\'")) {
str = str.replaceAll("(\'", "('");
}
if (str.contains("\')")) {
str = str.replaceAll("\')", "')");
}
if (str.contains(",\'")) {
str = str.replaceAll(",\'", ",'");
}
if (str.contains("\',")) {
str = str.replaceAll("\',", "',");
}

System.out.println(str);
return str;

}

public boolean delete(String query) {
try {
Class.forName("com.mysql.jdbc.Driver");
cn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/jnjcrm-23-04", "root", "root");
stmt = cn.createStatement();
System.out.println("query : " + query);
int i = stmt.executeUpdate(query);
if (i == 0) {
cn.commit();
cn.close();
return false;
} else {
cn.commit();
cn.close();
return true;
}

// cn.commit();

} catch (Exception e) {
System.out
.println("*** Exception Occurred during SELECT QUERY ***");
e.printStackTrace();
return false;
} finally {

}

}

// Close Function for Closing connection with Database

public void close() {
try {
if(cn !=null)
cn.close();
} catch (Exception e) {
e.printStackTrace();
}
}

//destroy all connections
public void destroy() {
try {
cn = null;
stmt = null;
preparedStatement = null;
rs = null;
} catch (Exception e) {
e.printStackTrace();
}
}

}
-------------------------------------------------------------------------------


Regards,
gopal

Options: ReplyQuote


Subject
Views
Written By
Posted
address already in use Unable to connect to any hosts due to exception
6292
April 30, 2009 09:12AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.