2 Replies Latest reply on May 30, 2005 8:36 AM by waglik

    How to connect to external database by statefull Java Bean??

    waglik

      Hi I write statfull JavaBean that connetc to PostgreSQL but my querry crush down.But when I put querry into simple stand alone java class it works with no problems?
      What is wrong? When I connect the database throu JMX console it alwso works...

        • 1. Re: How to connect to external database by statefull Java Be
          darranl

          Instead of posting the same question again and again you need to step back and see if your question makes any sense. If someone else had made this post and you were reading it for the first time would you really understand what they were trying to do?

          Before adding your database access code were you able to get your session bean to work? Were you able to invoke it without problems from the remote client?

          How are you obtaining the connection to the database? Have you deployed a datasource?

          What does the bean code look like, what error are you getting and at what point do you get the error?

          Also what does 'When I connect the database throu JMX console it alwso works' mean?

          • 2. Re: How to connect to external database by statefull Java Be
            waglik

            Well my session bean is working becouse I am able to use other methods from it. I get no error when I deploy it in Jboss. In my EJB I obtain conection to database like this :
            Class.forName("org.postgresql.Driver");
            DriverManager.getConnection("jdbc:postgresql://localhost:5432/Wybory",
            "Mateusz",
            "Mateusz");

            Database is working on my localhost non stop.
            Datasource is deployed


            I I get an error when I invoke a bean method :
            public int userVeryfication(String login,String haslo,String haslo2) throws SQLException {

            try {
            Class.forName("org.postgresql.Driver");
            } catch (ClassNotFoundException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }

            try {
            con =
            DriverManager.getConnection("jdbc:postgresql://localhost:5432/Wybory",
            "Mateusz",
            "Mateusz");
            } catch (SQLException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }

            try {
            Statement stmt = con.createStatement();
            } catch (SQLException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
            }



            String sql = "SELECT * FROM urzytkownicy";
            ResultSet rs = stmt.executeQuery(sql);

            /* while(rs.next()){

            int no = rs.getInt("id");

            String id = rs.getString("login");
            id.trim();



            String pass = rs.getString("haslo1");
            pass.trim();

            String pass2 = rs.getString("haslo2");
            pass2.trim();

            int stan = rs.getInt("stan");


            System.out.println(no + " " + id);

            if((haslo == pass) && (login == id) && (haslo2 == pass2) &&(stan==0) ) {
            return 1;
            }else
            return 0 ;
            }*///end while


            return 0;

            }

            And when saying I can get to my database throu JMX Console I mean that I can connect with database throu JMX console and exetute every query I want