This is my connection code and when I try same query directly in to SQL Developer tool I got my results. But when I'm trying to get that results in my java code that time I'm getting error.
I try this in java
Connection connection = null;
try {
String sql1="SELECT u.FIRSTNAME, u.MOBILE, ca.STREET_ADDRESS, ca.CITY, ca.POSTAL_CODE " +
"FROM USERS u " +
"JOIN ADDRESSES ca ON u.ID = ca.CUSTOMER_ID " +
"WHERE u.id = ?";
try {
PreparedStatement preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setInt(1,Id);
//preparedStatement.setString(2, pswd);
ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
String userId = resultSet.getString("MOBILE");
String un= resultSet.getString("FIRSTNAME");
String ad=resultSet.getString("STREET_ADDRESS");
System.out.println(ad);
System.out.println(un);
System.out.println("User ID: " + userId);
}
else {
System.out.println("Results not found");
# resultSet.close();
preparedStatement.close();
I'm getting Results not found
The problem is that you haven't established a connection to the database first. Because of your exception handling you are missing the actual error because you are not printing the actual exception and only see that message. I recommend to read some tutorials on how to establish JDBC connections and always log the real exception.