dcsimg
Centralized database connection by using JNDI style
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Kunthar_Theminder
Posted On:   Thursday, June 30, 2005 04:17 PM

Hi all, I want to use JNDI data source application wide. For this, i want to create a code piece only get a connection . Then when i want to call this getConnectionFromJNDI method to execute any prepared or not prepared sql in my application. Code seems like this public void dbinit() { /* Init shared resources */ try { Context initCtx = new InitialContext(); DataSource ds = (DataSource) initCtx.lookup("env/jdbc/faktoring"); Connection dbCon = ds.getConnection(); } catch (javax.naming.NamingException e) { System.out.println( "A problem occurred while retrieving a DataSource object"); Sys   More>>

Hi all,



I want to use JNDI data source application wide.
For this, i want to create a code piece only get a connection . Then when i want to call this getConnectionFromJNDI method to execute any prepared or not prepared sql in my application.



Code seems like this


			
public void dbinit() {
/* Init shared resources */
try {
Context initCtx = new InitialContext();
DataSource ds = (DataSource) initCtx.lookup("env/jdbc/faktoring");
Connection dbCon = ds.getConnection();
}
catch (javax.naming.NamingException e) {
System.out.println(
"A problem occurred while retrieving a DataSource object");
System.out.println(e.toString());
}
catch (java.sql.SQLException e) {
System.out.println(
"A problem occurred while connecting to the database.");
System.out.println(e.toString());
}
}


But the problem is what the caller function should like?


			
public static Deneme() {

try {
com.hede.hode.Test.dbinit();
Statement stmt = createStatement();
String strSql = " select * from cek1";
ResultSet rs = stmt.executeQuery(strSql);

while(rs.next()){
System.out.println(rs.getString(1));
System.out.println(rs.getString(2)+"
");
}
} catch(Exception ex){

ex.printStackTrace();
throw new SQLException("cannot get Connection pool.");
}



This piece of code is not working.
Any comments on this??
Take care all,    <<Less
About | Sitemap | Contact