tags:

views:

26

answers:

0

The program worked a second ago, but all of the sudden not any more :S
It doesn't even get results out of the database.
I've tried login into the database to check and that works fine...

This is the code:

package gaej.example.contact.server;

import gaej.example.contact.shared.Contact;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

  public class ContactDAOOracle implements ContactDAO {
    private static final String ORACLE_DB_DRIV =
          "oracle.jdbc.driver.OracleDriver";
    private static final String ORACLE_DB_CONN =
          "jdbc:oracle:thin:@145.89.21.30:8521:cursus01";
    private static final String ORACLE_DB_USER = "loginname124";   
    private static final String ORACLE_DB_PASS = "passwordXXX";  

    private long getLastId(Statement stmt) throws SQLException
    {
      long lastid = 0;

      String strQuery = "SELECT MAX(id) as lastID From studenten ";
      ResultSet rs = stmt.executeQuery(strQuery);
      if (rs.next()) {
        lastid = rs.getLong("lastID");
      }

      return lastid;
    }

    public void addContact(Contact contact) {
      try {
        Class.forName(ORACLE_DB_DRIV).newInstance();
        Connection con = DriverManager.getConnection(
            ORACLE_DB_CONN,ORACLE_DB_USER,ORACLE_DB_PASS);

        Contact c = new Contact(contact.getNaam(),contact.getAdres(),contact.getPostcode(),contact.getPlaats(),contact.getGeboren()); 
        PreparedStatement stmt = con.prepareStatement(
        "INSERT INTO STUDENTEN VALUE (?, ?)");
        long lastid = getLastId(stmt);
        stmt.setLong(1, lastid+1);
        stmt.setObject(2, c);
        stmt.executeUpdate();
        stmt.close();
        con.close();

      } catch (Exception e) {
        System.out.println("Cannot insert: " + e.getMessage());
        e.printStackTrace();
      }    
    }


    public List<Contact> listContacts() {
      List<Contact> contactList = new ArrayList<Contact>();
      Contact c;

      try {
        Class.forName(ORACLE_DB_DRIV).newInstance();
        Connection con = DriverManager.getConnection(
            ORACLE_DB_CONN,ORACLE_DB_USER,ORACLE_DB_PASS);

        Statement stmt = con.createStatement();

        String queryTekst = "SELECT * FROM studenten";
        ResultSet rs = stmt.executeQuery(queryTekst);
        while (rs.next()) {
          c = new Contact();
          c.setId(rs.getLong("id"));
          c.setNaam(rs.getString("naam"));
          c.setAdres(rs.getString("adres"));
          c.setPostcode(rs.getString("postcode"));
          c.setPlaats(rs.getString("plaats"));
          c.setGeboren(rs.getString("geboren"));
          contactList.add(c);
        }
        stmt.close();

      }catch (Exception e) {
        System.out.println(e.getMessage());
      }

      return contactList;
    }


    public List<String> metaDataContact(Contact contact) {
          List<String> metaData = new ArrayList<String>();

          metaData.add("Metadata:");

          try {
            Class.forName(ORACLE_DB_DRIV).newInstance();
            Connection con = DriverManager.getConnection(
                ORACLE_DB_CONN,ORACLE_DB_USER,ORACLE_DB_PASS);

            Statement stmt = con.createStatement(  );
            ResultSet rs = stmt.executeQuery("SELECT * FROM STUDENTEN");

            ResultSetMetaData rsmd = rs.getMetaData(  );
            int columnCount = rsmd.getColumnCount(  );
            for(int col = 1; col <= columnCount; col++) {
                metaData.add(rsmd.getColumnLabel(col)+"(" + rsmd.getColumnTypeName(col)+")");
            }

            rs.close();
            stmt.close();
            con.close();

          }catch (Exception e) {
            System.out.println(e.getMessage());
          }

          return metaData;
    }

    public void removeContact(Contact contact) {
      try {
        Class.forName(ORACLE_DB_DRIV).newInstance();
        Connection con = DriverManager.getConnection(
            ORACLE_DB_CONN,ORACLE_DB_USER,ORACLE_DB_PASS);
        Statement stmt = con.createStatement();

        String strQuery = "DELETE FROM studenten " +
          " WHERE id = '" + contact.getId() + "'" ;
        stmt.executeUpdate(strQuery);      
        stmt.close();
        con.close();
      } catch (Exception e) {
        System.out.println(e.getMessage());
      }    
    }

    public void updateContact(Contact contact) {
      try {
        Class.forName(ORACLE_DB_DRIV).newInstance();
        Connection con = DriverManager.getConnection(
            ORACLE_DB_CONN,ORACLE_DB_USER,ORACLE_DB_PASS);

        Contact c = new Contact(contact.getNaam(),contact.getAdres(),contact.getPostcode(),contact.getPlaats(),contact.getGeboren()); 
        PreparedStatement stmt = con.prepareStatement(
        "UPDATE STUDENTEN VALUE (?, ?) WHERE id = '" + contact.getId() + "'");
        stmt.setLong(1, contact.getId());
        stmt.setObject(2, c);
        stmt.executeUpdate();
        stmt.close();
        con.close();

      } catch (Exception e) {
        System.out.println(e.getMessage());
      }    
    }


  }