Getting "Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections" when connecting via jdbc

Hi, I am using JDBC to connect to a Postgres DB.  I am calling a method that connects to the DB twice.  It connects once to do a select statement and another time to do an insert statement.  I am checking to see if the a session_id exists and if it doesn't I am adding the session_id into the DB.   It does the initial select fine, but when I try to connect the second time to do the insert I am getting the error in the title above. Here is what my code looks like.  Can anyone help me out with resolving this?

private void initialInsert(String sessionId) {

		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
			log.info("Class Not Found: " + e.getMessage());
		}
		log.info("Entering the initial insert");
		String sessionIdFromDB = "";
		String url = "jdbc:postgresql://10.233.52.71:5432/CustomersLikeYou";
		Properties props = new Properties();
		props.setProperty("user", "itrd");
		props.setProperty("password", "itrd");
		Connection conn = null;
		ResultSet rs = null;
		try {
			log.info("Entering select");
			conn = DriverManager.getConnection(url, props);
			Statement statement = conn.createStatement();
			rs = statement.executeQuery(
					"select session_id from public.individual_customer where session_id = '" + sessionId + "'");
			log.info("Succeful select");
			while (rs.next()) {
				if (StringUtils.isNotEmpty(rs.getString("session_id"))) {
					sessionIdFromDB = rs.getString("session_id");
				}
			}

		} catch (SQLException e) {
			log.info(e.getMessage());
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					log.info(e.getMessage());
				}
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					log.info(e.getMessage());
				}
			}
		}

		try {
			log.info("Entering insert");
			conn = DriverManager.getConnection(url, props);
			Statement statement = conn.createStatement();

			if (StringUtils.isEmpty(sessionIdFromDB)) {
				// insert session id
				statement.executeUpdate(
						"insert into public.individual_customer (session_id) values '" + sessionId + "'");
			}
			log.info("Successful insert");
		} catch (SQLException e) {
			log.info(e.getMessage());
		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {
					log.info(e.getMessage());
				}
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					log.info(e.getMessage());
				}
			}
		}

	}

Open in new window

Kevin BAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
Are you certain that the first request is succeeding?

That error is pretty clear.  It's saying that when you ask to connect on 10.233.52.71:5432 you're getting no response.

So either:
   that's the wrong IP, or
   the wrong port, or
   the db server is not running there or
   the firewall is not configured to accept requests there

Generally those things are all pretty static and wouldn't come and go between requests.

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
 It connects once to do a select statement and another time to do an insert statement.
You don't need to do that. In versions >= 9.5 you can use UPSERT and for earlier you could at least do INSERT INTO foo WHERE NOT EXISTS. Either way, you should be using PreparedStatement. If you can use a reasonably recent version of Java, you can get rid of all those nasty boilerplate close blocks using try-with-resources too
0
Kevin BAuthor Commented:
Both of you were helpful.  I had a coding error.  I was connected to the wrong DB.  However, it was good to change this to a single statement.

Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JDBC

From novice to tech pro — start learning today.