Hello Everyone,

I am using ths class as my reusable SQL select for my queries in other classes. But when I used this in other classes, it generated me this error:

java.sql.SQLException: Network error IOException: Address already in use: connect

Can anyone give me some idea or advise on how to eliminate this kind of error?

Thanks in advance

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

public class SQLRun {
	
	public static Vector QuerySelect(String query,int resultset){
		
		Vector VectorToReturn =  new Vector();
		String errMsg = "";
		
		//Establish DB connection
		Connection conn = null;
		
		//Retrieve report parameters from the database
		ResultSet rs = null;
		Statement stmt = null;
		
		String jdbcURL = GlobalVars.DatabaseURL;
		String dbUserName = GlobalVars.ParmUserName;
		String dbPassword = GlobalVars.ParmPassword;
		
		try
		{
			
			if (GlobalVars.dbType.equals("ORACLE")) {
				Class.forName("oracle.jdbc.driver.OracleDriver");
				conn = DriverManager.getConnection(jdbcURL,dbUserName,dbPassword);
			}
			
			if (GlobalVars.dbType.equals("SQLSERVER")) {
				Class.forName("net.sourceforge.jtds.jdbc.Driver");
	            conn = DriverManager.getConnection(jdbcURL, dbUserName,dbPassword);
			}
			
			stmt = conn.createStatement();

			//SimpleLog.write("SQLRun query:" + query);
			
			if (resultset==1){
				rs = stmt.executeQuery(query);
			    while (rs.next()) {
			    	String ColValue = rs.getString(1);
			    	//System.out.println(ColValue);
			    	VectorToReturn.add(ColValue);
			    }
			    rs.close();
			    stmt.close();
			}else{
				stmt.executeUpdate(query);
			}
		}
		
		catch(SQLException e)
		{
			errMsg += "SQLRun SQLException: " + e.getMessage();
			SimpleLog.write(errMsg);
			e.printStackTrace();
			while((e = e.getNextException()) != null)
				//errMsg += e.getMessage();
				//System.out.print(errMsg);
				SimpleLog.write(errMsg);
		}

		catch(ClassNotFoundException e)
		{
			errMsg += "SQLRun ClassNotFoundException: " + e.getMessage();
			SimpleLog.write(errMsg);
			e.printStackTrace();
			//System.out.print(errMsg);
		}
		
		catch (Exception e)
	        {
				errMsg += "SQLRun Exception: " + e.getMessage();
				SimpleLog.write(errMsg);
				e.printStackTrace();
	        }
		 
		finally
		{
		//Clean up resources, close the connection.
			if(conn != null)
			{
				try
				{
					conn.close();
				}
				catch (Exception ignored) {}
			}
		}
		//System.out.println("StringToReturn:" + StringToReturn);
		return VectorToReturn;
	}
}

Check the url parameter for SQLSERVER.

Thanks for the reply.

The URL is working fine but some instances that it will execute the query successfully and the next one are not.

For example, class1 and class2 uses this function then I use these two classes in class3.

Class0 - Scan the parameter of the class using using the JDBC connection class then
Class1 - Get the textfiles in a specific folder then
Class2 - Parses Values in the textfile Consists of the 4 classes using the JDBC connection class for the parse values

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.