on 11-06-2006 5:02 PM
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.
Usually the DataSource name is:
"jdbc/SAP" + <SID> + "DB"
<SID> equals system ID
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
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.
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
/*
Created on 3-nov-2006
*
To change the template for this generated file go to
Window>Preferences>Java>Code Generation>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>Preferences>Java>Code Generation>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;
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.