Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
titlesrc/main/java/Oracle.java
import java.sql.*;


public class Oracle {
    public static void main(String[] args) {
        String dsn = "hydra2";
        String login = "AIS_RPC";
        String password = "********";

        try {
            // Establish the database connection
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@" + dsn, login, password);
            // Call exec method
            Oracle.select(conn);
            // Close the database connection
            conn.close();
        } catch (SQLException e) {
            System.err.println("Connection error!");
            e.printStackTrace();
        }
    }

    public static void exec(Connection conn) {
        // Select query
        String query = """
			SELECT VC_SUBJ_TYPE_NAME, VC_CODE 
			FROM SI_V_SUBJECTS WHERE N_SUBJECT_ID = 100
		""";

        try {
            // Create an Oracle statement for execution
            Statement stmt = conn.createStatement();
            // Execute query
            ResultSet rs = stmt.executeQuery(query);
            // Get and output the result.
            while (rs.next()) {
                String vcSubjTypeName = rs.getString("VC_SUBJ_TYPE_NAME");
                String vcCode = rs.getString("VC_CODE");
                System.out.println("VC_SUBJ_TYPE_NAME: " + vcSubjTypeName);
                System.out.println("VC_CODE: " + vcCode);
            }
        } catch (SQLException e) {
            System.err.println("Select error!");
            e.printStackTrace();
        }
    }
}

...