Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.sql.*;
- /* IP Adress: 134.58.95.246
- * Port: 1521
- * Username: student
- * Password: student
- */
- public class Main {
- static {
- try {
- /* Type 4 Driver */
- Class.forName("oracle.jdbc.OracleDriver");
- }
- catch (ClassNotFoundException e) {
- System.err.println("Could not load Oracle 10g driver \n");
- System.err.println(e.getMessage());
- System.exit(1);
- }
- }
- public static String askDiscipline() {
- String discipline = null;
- try
- {
- BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("Enter a discipline: ");
- discipline = br1.readLine();
- }
- catch (IOException e)
- {
- System.out.print("Failed to get discipline");
- System.out.println(":" + e.getMessage());
- System.exit(1);
- }
- return discipline;
- }
- public static void main(String[] args) {
- /* Location of the database */
- String url = "jdbc:oracle:thin:@134.58.95.246:1521:XE";
- /* Connect to Oracle database */
- Connection conn = null;
- try {
- conn = DriverManager.getConnection(url, "student", "student");
- System.out.println("Connection established...");
- System.out.println();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- /* Initial input */
- String discipline = askDiscipline();
- /* Start loop that asks for input, query, and displays result */
- while (!discipline.equals("0")) { // => geeft fout: The local variable discipline may not have been initialized
- try {
- /* Query */
- String query = "SELECT LASTNAME, FIRSTNAME, RESULT " +
- "FROM ATHLETE.ATHLETE, ATHLETE.DISCIPLINE, ATHLETE.RESULT " +
- "WHERE ATHLETE.ANR = RESULT.ANR " +
- "AND DISCIPLINE.DNR = RESULT.DNR " +
- "AND DISCIPLINE.DNAME = " + discipline; // + " " +
- //"ORDER BY RESULT DESC LIMIT 0,3 ";
- /* Create statement */
- System.out.println("De query is:\n" + query);
- Statement stmt = conn.createStatement();
- /* Execute the query */
- ResultSet rs = stmt.executeQuery(query);
- /* Output */
- System.out.println("First name // Last name // Result");
- System.out.println("-------------------------------");
- while (rs.next()) {
- System.out.print(rs.getString(1));
- System.out.print((" // "));
- System.out.print(rs.getString(2));
- System.out.print((" // "));
- System.out.println(rs.getString(3));
- }
- rs.close();
- stmt.close();
- conn.close();
- }
- catch (SQLException e) {
- System.out.println("SQL Exception: ");
- System.err.println(e.getMessage());
- }
- discipline = askDiscipline();
- }
- System.out.println("End Of Session");
- }
- }
Add Comment
Please, Sign In to add comment