0

I have java class having very simple method for database connection. Connection is established while using this code is jsp file but when i call this method from .java class it gives exception. the code i use to call that method is as fallow

  <% db d = new db();
d.db1("testing", "dspace", "dspace");
  %>

while the code is .java class is as fallow

 public boolean  db1 (String db, String username, String pwd) throws SQLException, ClassNotFoundException
                       {
            Connection connection; 

            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection(
                "jdbc:postgresql:5432//127.0.0.1:5432/testing", username,
                pwd);
            if(connection!= null)
                                   {
          System.out.println("done");
            }          
    return false;
    }

there error is

    org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP
Bram Luyten
  • 1,034
  • 7
  • 18
Nauman Khalid
  • 331
  • 3
  • 10

5 Answers5

1

Try this

<% 
db d ;
try{
d = new db();
d.db1("testing", "dspace", "dspace");
}catch(Exception e){
 //do something
 }
  %>

As your code is not handling the runtime exceptions.

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
1

Shouldn't the DB URL be

"jdbc:postgresql://127.0.0.1:5432/testing"

instead of

"jdbc:postgresql:5432//127.0.0.1:5432/testing"

?

TechSpellBound
  • 2,505
  • 6
  • 25
  • 36
0

You are making a jar mistake i think but please show complete errors list Like below:

org.apache.jasper.JasperException: PWC6033: Error in Javac compilation for JSP

PWC6197: An error occurred at line: 9 in the jsp file: /index.jsp
PWC6199: Generated servlet error:
';' expected

More detail will ease us to solve your problem

vicky
  • 561
  • 1
  • 7
  • 17
0

Try

 <% db d = new db();
    try{
       d.db1("testing", "dspace", "dspace");
    }catch(Exception e){
       //wtf?
    }
 %>

Even there are some cases not specified.

  • What if the connection is broken
  • What if the connection is closed
  • What if the connection is timedout
  • What if the connection is dirty
  • What if the database not available anymore
  • What if the database removed
  • What if the databaseserver not available
  • What if the databaseserver timed out
  • What if the user not available
  • What if the user has no right
  • ...

Hey you can get rid of the db.java using jstl/sql and jndi.

Grim
  • 1,938
  • 10
  • 56
  • 123
-3

I don't think so that you used Class.forname in JSP please try encapsulate

kennedy
  • 23
  • 1
  • 1
  • 4