For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2006 > help in jdbc/jndi/java class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author help in jdbc/jndi/java class
Manoj Jain

2006-05-23, 4:13 am

hi
i've created some simple java beans, when i do log in, i forward it to
servlet where i create LoginDTO object from username and password. in
the same class i validate the username and password with the system.
but it returns null value to connection object.


Login.jsp <|------------Loginform(action=LoginServlet)

in LoginServlet, loginDTO is created
loginDTO=new
LoginDTO(req.getParameter("userName"),req.getParameter("password"));

Database class is meant for database operations like connection,
disconnection, insert/validation/deletion.

I do connectivity through Database.connect()

public final class Database{
//declaration of member variables
public static void connect()
{
try
{
ic= new InitialContext();
DataSource
ds=(DataSource)ic.lookup("jdbc/proformaInvoice");
conn=ds.getConnection();
System.out.println("Hello World"); //to check whether
it comes to this line or not.
}
catch(SQLException se){}
catch(NamingException ne){}
}

//this method validates user name and password with the system.
public static boolean validate(LoginDTO loginDTO)
{
boolean bnFlag;
authMap=new HashMap<String,String>();
try
{
stmt=conn.createStatement();
res=stmt.executeQuery("Select User, Password from
UserMaster");
while(res.next())
{

authMap.put(res.getString("user").toUpperCase(),res.getString("password"));
}
if(authMap.containsKey(loginDTO.getName()) &&
authMap.get(loginDTO.getName()).equals(loginDTO.getPassword()))
{
bnFlag=true;
}
else
{
bnFlag=true;
}
}
catch(SQLException se){bnFlag=false;}
return bnFlag;
}


jdbc/proformaInvoice is configured in Java AppServer 9 as jndi name to
derby driver and database name. in App server there is ping facility
that checks whether the configuration done is correct or not, and in my
database, it is pinging, but still it gives null to connection.
Is it because of i'm using datasource in java class context? Do i've
to use normal Class.forName() way to load drivers in memory?

Juha Laiho

2006-05-25, 7:06 pm

"Manoj Jain" <Manoj.S.J@gmail.com> said:
> i've created some simple java beans, when i do log in, i forward it to
>servlet where i create LoginDTO object from username and password. in
>the same class i validate the username and password with the system.
>but it returns null value to connection object.

....
>Database class is meant for database operations like connection,
>disconnection, insert/validation/deletion.


Be careful that you don't make the database handling a congestion point
in your application (may happen, if you just create a single Database
object during servlet init, and then access that single object from
all the different threads of execution).

>public static void connect()
> {
> try
> {
> ic= new InitialContext();
> DataSource
>ds=(DataSource)ic.lookup("jdbc/proformaInvoice");
> conn=ds.getConnection();
> System.out.println("Hello World"); //to check whether
>it comes to this line or not.
> }
> catch(SQLException se){}
> catch(NamingException ne){}
> }


>jdbc/proformaInvoice is configured in Java AppServer 9 as jndi name to
>derby driver and database name. in App server there is ping facility
>that checks whether the configuration done is correct or not, and in my
>database, it is pinging, but still it gives null to connection.


Well, in the above you're explicitly throwing away any exception
information you may be getting. At least print out the exception stacks
in the catch {} blocks.

> Is it because of i'm using datasource in java class context? Do i've
>to use normal Class.forName() way to load drivers in memory?


When you're getting the connection references via JNDI, you must not
explicitly load the database classes yourself. Pretty big part of the
reason to use the JNDI abstraction is to allow you to write code that
is independent of the database implementation you use - and thus, loading
a specific implementation code in your own application code is a big no-no.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Juha Laiho

2006-05-25, 7:06 pm

Juha Laiho <Juha.Laiho@iki.fi> said:
>"Manoj Jain" <Manoj.S.J@gmail.com> said:
....[color=darkred]
>
>
>Well, in the above you're explicitly throwing away any exception
>information you may be getting. At least print out the exception stacks
>in the catch {} blocks.


[don't you hate it when you realize just after posting a message that there's
still a bit more to say..]

So, adding to the previous response; if the exception data doesn't provide
any additional help, then write a simple, plain servlet which does a very
simple database transaction:

- fetch JNDI initial context
- fetch data source through the context
- get connection from the data source
- make db query through the connection
- close connection

.... and have the servlet print out (to the browser) the results of each
and every operation - and also details of each and every exception. Once
you get this working, you can start to change it towards your desired
abstraction model.
--
Wolf a.k.a. Juha Laiho Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com