Retrieve content from 2 tables in Java using Oracle database

48 Views Asked by At

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

3

There are 3 best solutions below

2
aled On
Connection connection = null;

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.

2
RedSnack-BCS On

This code executes get "Results not found",which means the code does not have an error. And SQL execute in SQL Developer Tool is also successful,which means SQL does not make errors as well.

In the code for this preparedStatement.setInt(1,Id);, the "Id" is a parameter You should check this parameter.

You can set the "Id" value to preparedStatement.setInt(1,"1");. If you get the result, it indicates that there is a problem the parameter values passed.

0
MT0 On

UnCOMMITted data is only visible within the session that created it (and will ROLLBACK at the end of the session if it has not been COMMITted). If you can't see the data from another session (i.e. in Java) then make sure you have issued a COMMIT command in the SQL client where you INSERTed the data (i.e. SQL Developer).

Note: even if you connect as the same user, this will create a separate session and you will not be able to see the uncommitted data in the other session.

From the COMMIT documentation:

Until you commit a transaction:

  • You can see any changes you have made during the transaction by querying the modified tables, but other users cannot see the changes. After you commit the transaction, the changes are visible to other users' statements that execute after the commit.
  • You can roll back (undo) any changes made during the transaction with the ROLLBACK statement (see ROLLBACK).