0

I am trying to learn to connect a database to a Java program and I encounter an issue that all the entries I provided from my Oracle client are not showing up in my JDBC program

import java.sql.*;
public class jdbc {
    public static void main(String[] args) {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con =DriverManager.getConnection("jdbc:oracle:thin:@LAPTOP-PCVGGS4F:1521:xe", "system", "---"); 
            String q = "select * from example";
            Statement stmt =con.createStatement();
            ResultSet set = stmt.executeQuery(q);
            while (set.next()) {
            String temp =set.getString(1);
            System.out.println(temp);
            }
//            String q = "insert into example values('Input from jdbc')";
//            PreparedStatement stmt = con.prepareStatement(q);
//           stmt.executeUpdate();
            }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

output of java program

output from oracle client

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Adarsh
  • 1
  • 3
    The easiest answer would be that the rows were inserted but not committed in the `SQL*Plus` session. If that's not it, post a reproducible test case. – Justin Cave May 20 '22 at 22:03
  • @JustinCave - I thought that standard behavior for Oracle JDBC drivers is that autocommit is true by default. See https://stackoverflow.com/questions/11021304 – Stephen C May 20 '22 at 22:44
  • 1
    @StephenC - from your screenshot your client is SQL\*Plus, which doesn't use JDBC. And it defaults to not autocommit (after each statement anyway; it will commit on exit by default though). You can run `show autocommit` to see that it's turned off. I don't recall ever seeing anyone change that setting; you can, but I'd recommend leaving it off unless you have a really good reason to change it. – Alex Poole May 20 '22 at 22:58
  • @StephenC JDBC may default to autocommit but SQL\*Plus does not use JDBC. – MT0 May 20 '22 at 22:59
  • So ... if the OP is using SQL*Plus, why are they asking this as a JDBC question? And showing us JDBC code? No. The behavior of SQL*Plus vs autocommit is relevant for a JDBC question. – Stephen C May 21 '22 at 04:15
  • 1
    @StephenC They inserted the data using SQL\*Plus, and cannot see it when querying for it with JDBC. That means the commit behaviour for SQL\*Plus is very relevant. Almost all of this type of questions happen because the data wasn't committed. – Mark Rotteveel May 21 '22 at 06:36

0 Answers0