cancel
Showing results for 
Search instead for 
Did you mean: 

data dictionary for jdbc

Former Member
0 Kudos

hi guyz

I need some help!!

I have created a table in a data dictionary project.

Please let me know how to deploy it. Do you have to deploy it through an ear file or so?

Next, since its on the portal database i heard that we need to use the default datasource.

What is the name of the datasource? Has any setting to be done to use it and please show me 2-3 lines of code on how to use it in order to establish a JDBC connection.

Thanks.

Accepted Solutions (0)

Answers (2)

Answers (2)

roberto_tagliento
Active Contributor
0 Kudos

Usually the DataSource name is:

"jdbc/SAP" + <SID> + "DB"

<SID> equals system ID

Former Member
0 Kudos

Hi Roberto

thanks a lot for your reply.

Ill try this out tomorrow.

Do you know of any portal table where i can select rows from to display in a select statement?

I want to try this before I deploy my own table.

Thanks.

Former Member
0 Kudos

My code:


			Context ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/SAPJ2EDB");
			Connection con = ds.getConnection();
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("Select * from tab");

is throwing an exception:

Exception:  Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

If I am ujsing the default data source I read I have to edit some datasource alias xml file. But in a java project where do I put it?

Will this work say in an abstract portal component?

roberto_tagliento
Active Contributor
0 Kudos

Did you look my first post?

.....

...

......

datasource_name = "jdbc/" + datasource_name; // + "DB";

String contextUrl = "";

String contextFactory = "com.sap.engine.services.jndi.InitialContextFactoryImpl";

properties = new Properties();

properties.put("java.naming.factory.initial", contextFactory);

ctx = new InitialContext(properties);

ds = (DataSource)ctx.lookup(datasource_name);

con = ds.getConnection();

....

...

In particular:

ctx = new InitialContext(properties);

You can get the datasource name from SDM, you must have enabled the JDBC service into SERVER0, and look the properties and alias.

Bye.

Former Member
0 Kudos

sorry i did not see that properly.

if you dont mind please take a look here:

String contextFactory = "com.sap.engine.services.jndi.InitialContextFactoryImpl";
			properties = new Properties();
			properties.put("java.naming.factory.initial", contextFactory);
			Context ctx = new InitialContext(properties);
			DataSource ds = (DataSource) ctx.lookup("jdbc/SAPJ2EDB");
			Connection con = ds.getConnection();
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery("Select * from tab");
			String res = null;
			while(rs.next())
			{
				res = rs.getString(1);	
				System.out.println("n"+res);
			}

I put this in the main method and run the program in nwds. Is that OK?

I get this exception now:

Exception: Cannot instantiate class: com.sap.engine.services.jndi.InitialContextFactoryImpl

Im sorry to ask why do we need to make a properties file and make a context of it to use in the look up. Im a bit new to this. And any help with my error.

I really need to connect to portal database badly. Hope I dont have to make any more setting in any xml file or so.

Please help.

Thanks

roberto_tagliento
Active Contributor
0 Kudos

Control if the service if available.

Launch Visual Adminiistrator, JDBC connector must be active with at least one ALIAS.

JDBC services works like a web service (it is), so it must exist.

roberto_tagliento
Active Contributor
0 Kudos

/*

  • Created on 3-nov-2006

*

  • To change the template for this generated file go to

  • Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

*/

package tmp.userslist.comp.model.userslist;

import java.sql.Connection;

import java.util.Properties;

import javax.naming.InitialContext;

import javax.sql.DataSource;

/**

  • @author rtagliento

*

  • To change the template for this generated type comment go to

  • Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments

*/

public class MyJDBC {

private boolean hasConnection;

private Connection con;

private DataSource ds;

private InitialContext ctx;

private Properties properties;

public MyJDBC(){

hasConnection = false;

con = null;

ds = null;

ctx = null;

}

public boolean startDBCon(){

hasConnection = true;

try {

String datasource_name =

"jdbc/SAP" +

(String) System.getProperties().get("SAPSYSTEMNAME")

+ "DB";

hasConnection = this.startDBCon(datasource_name);

} catch (Exception e) {

// e.printStackTrace();

hasConnection = false;

}

return hasConnection;

}

public boolean startDBCon(String datasource_name){

hasConnection = true;

try {

datasource_name = "jdbc/" + datasource_name; // + "DB";

String contextUrl = "";

String contextFactory = "com.sap.engine.services.jndi.InitialContextFactoryImpl";

properties = new Properties();

properties.put("java.naming.factory.initial", contextFactory);

ctx = new InitialContext(properties);

ds = (DataSource)ctx.lookup(datasource_name);

con = ds.getConnection();

} catch (Exception e) {

// e.printStackTrace();

hasConnection = false;

}

return hasConnection;

}

public Connection getConnection (){

if (hasConnection)

return con;

else

return null;

}

}