how use datasource

hi master

sir i import this file

import javax.naming.Context;

import com.sun.rave.web.ui.component.Button;

and

in button event i use this code for using datasource

Context ctx = null;

DataSource ds = null;this line give error

try {

ctx = new InitialContext();this line give error

ds = (DataSource) ctx.lookup("jdbc/travel");this line give error

} catch (NamingException ex) {

ex.printStackTrace();

}

Connection conn = ds.getConnection(); this line give error

that line give me error

please give me idea how i use datasource

thank's

aamir

[660 byte] By [mfa786a] at [2007-11-27 7:02:55]
# 1

It looks like you're missing some import statements. You also need to import:

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.SqlException;

Just the way you caught the NamingException when creating an InitialContext and DataSource, you will have to catch SqlException when you call ds.getConnection() which can throw SqlException.

Futeleufu_Johna at 2007-7-12 18:54:02 > top of Java-index,Development Tools,Java Tools...
# 2

thak's for your reply

sir i use your code that give me right resposce but

sir this line give me error

Connection conn = ds.getConnection();

i try

ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Travel") ;

see my code

Context ctx = null;

DataSource ds = null;

try {

ctx = new InitialContext();

ds = (DataSource) ctx.lookup("java:comp/env/jdbc/Travel") ;

} catch (NamingException ex) {

ex.printStackTrace();

}

Connection conn = ds.getConnection();

return null;

}

only this line give me error

Connection conn = ds.getConnection();

please give me idea how i get right grsult

thank's

aamir

mfa786a at 2007-7-12 18:54:02 > top of Java-index,Development Tools,Java Tools...
# 3

sir sorry i again dist. you

sir when i use this code

Connection conn = null;

Statement sqlStatement = null;

javax.naming.Context ctx = new javax.naming.InitialContext();

DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Trabel") ;

conn = ds.getConnection()

also

this line give me error

conn = ds.getConnection()

please give me idea what problem with

conn = ds.getConnection() this line

thank's

aamir

mfa786a at 2007-7-12 18:54:02 > top of Java-index,Development Tools,Java Tools...
# 4

Are you catching SqlException? Try this:

try {

conn = ds.getConnection();

}

catch (SqlException se) {

System.out.println("SqlException: " + se.getMessage());

se.printStackTrace();

}

Note that you can have multiple catch blocks. For example

try {

Code that throws more than one type of exception

}

catch (Exception1 e1) {

Code that handles first type of exception

}

catch (Exception2 e2) {

Code that handles second type of exception

}

When you get a compiler error, examine the description of the error. In the case you described conn = ds.getConnection()

the error message should tell you that the statment throws an uncaught exception.

In the future, use the Javadocs to examine the methods you want to call. If a method throws one or more exceptions, it will tell you.

Futeleufu_Johna at 2007-7-12 18:54:02 > top of Java-index,Development Tools,Java Tools...