cancel
Showing results for 
Search instead for 
Did you mean: 

how to trace the error, JDBC connection from a Java Class to WebDynPro app

paras_arora3
Participant
0 Kudos

Hi,

I am using a JavaBean as a model in my webdynpro app, the Java bean is a simple java class with getters and setters and methods like addUser( ) that does and insert into the SQL server database, I do the initial context lookup using the database alias in the bean constructor and get the connection using the datasource in a method getConnection( ). This bean has been imported as a jar file to import a java bean model. I have this Jar file in my application's build path as well, now in the view controller when I do a modelObject.addUser( ) the applications exits without entering any data and without any error message. Indicating that JDBC connection could not be established and the Insert failed. Where can I see the printStacktrace( ) that I have in my Java bean ? where would they be getting logged ?? and why is the webdynpro view controller after calling modelObject.addUser( ) is as well ducking the exception that takes place in the Javabean class while trying to establish a connection.

Suggest me a way out!!

the key code in question

public class UserInfoBean

{

private String empId;

private String name;

private String primarySkill;

private String designation;

private String emailId;

private String deskPhone;

private String mobile;

private String workLocation;

private String photoPath;

private Connection connection;

//

private ResultSet rs;

static DataSource dataSource;

public UserInfoBean()

{

try

{

//code to look up the datasource

InitialContext initialContext = new InitialContext();

dataSource = (DataSource)initialContext.lookup("jdbc/aliasforradar");

}

catch (NamingException e)

{

e.getMessage();

}

}

public void execute()

{

addUser();

}

public void addUser()

{

Connection connection = null;

try

{

connection = getConnection();

PreparedStatement ps = connection.prepareStatement("insert into User (Employee_Id,Name,Phone1,Phone2,"+

"Location,Designation,Technology,Email_Id,Photo_Path)"+

"values(?,?,?,?,?,?,?,?,?)");

try

{

ps.setString(1,"100292");

ps.setString(2,"Paras");

ps.setString(3,"41062");

ps.setString(4,"9886664");

ps.setString(5,"BAN");

ps.setString(6," ");

ps.setString(7,"SAP Netweaver EP");

ps.setString(8,"");

ps.setString(9,"sdf");

ps.executeUpdate();

}

catch(SQLException se)

{

se.printStackTrace();

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

ps.close();

}

}

catch(Exception e)

{

e.printStackTrace();

}

}

public static Connection getConnection() throws SQLException, ClassNotFoundException

{ /*

Connection connection = null;

Class.forName(IConstants.SQL_DRIVER);

connection = DriverManager.getConnection(IConstants.SQL_URL,IConstants.SQL_UN,IConstants.SQL_PWD);

return connection; */

java.sql.Connection connection = null ;

try

{

connection = dataSource.getConnection();

}

catch (SQLException e)

{

System.out.println(e.getMessage());

}

return connection;

}

I call the add user from the view Controller as

public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionSave(ServerEvent)

IWDMessageManager msgMgr = wdComponentAPI.getMessageManager();

try

{

wdContext.currentUserInfoBeanElement().modelObject().addUser();

//msgMgr.reportSuccess("Congratulations,User Added to the Database tables..inside try");

}

catch(Exception e)

{

msgMgr.reportSuccess("Congratulations,User Added to the Database tables...Caught by exception");

msgMgr.reportException(e.getLocalizedMessage(),true);

}

msgMgr.reportSuccess("Something Written");

//@@end

}

-paras

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Paras,

You can modify your bean so that addUser() method will throw an excption.

You can catch the exception in webDybpro view and show the message using MessageManager.

Or you can goto server logs. C:\usr\sap\<ID>\<Instance>\j2ee\cluster\server0\log

and you can check the complete stacktrace.

You are calling getConnection() in addUSer() method so its throwing an error there.

Check the JDBC datasourcein visualadmin onceagain.

Regards, Anilkumar

paras_arora3
Participant
0 Kudos

Hi,

I did as you prescribed now a portion of UserInfoBean code looks like this

public void addUser() throws Exception

{

Connection connection = null;

System.err.println("In AddUser");

try

{

connection = getConnection();

System.err.println("After getConnection");

PreparedStatement ps = connection.prepareStatement("insert into User (Employee_Id,Name,Phone1,Phone2,"+

"Location,Designation,Technology,Email_Id,Photo_Path)"+

"values(?,?,?,?,?,?,?,?,?)");

try

{

ps.setString(1,"10030292");

ps.setString(2,"Paras");

ps.setString(3,"41861062");

ps.setString(4,"9886623664");

ps.setString(5,"BANG 4");

ps.setString(6,"SE");

ps.setString(7,"SAP Netweaver EP");

ps.setString(8,"paras.arora@accenture.com");

ps.setString(9,"sdf");

System.err.println("After setting in the values");

ps.executeUpdate();

System.err.println("After insertion into tables");

}

catch(SQLException se)

{

se.printStackTrace();

}

catch(Exception e)

{

e.printStackTrace();

}

finally

{

ps.close();

}

}

catch(Exception e)

{

System.err.print("Error Message"+e.getMessage());

}

}

***********************************************************************************************

and on the webDynpro front while calling the method, the code looks like this

public void onActionSave(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionSave(ServerEvent)

IWDMessageManager msgMgr = wdComponentAPI.getMessageManager();

try

{

wdContext.currentUserInfoBeanElement().modelObject().addUser();

//msgMgr.reportSuccess("Congratulations,User Added to the Database tables..inside try");

}

catch(Exception wde)

{

msgMgr.reportSuccess("Congratulations,User Added to the Database tables...Caught by exception");

msgMgr.reportException(wde.getLocalizedMessage(),true);

msgMgr.reportException(wde.getMessage(),true);

}

msgMgr.reportSuccess("Something Written");

//@@end

}

the problem is now whatever error is there it doesnt seems to be getting caught i dont know how, but after I press the save button on the AddUser page I get this code msgMgr.reportSuccess("Something Written"); executed with the corresponding output!!

what to do!! ??

-paras

Former Member
0 Kudos

Your method is not throwing any exception.Modify as follows

try

{

ps.setString(1,"100292");

ps.setString(2,"Paras");

ps.setString(3,"41062");

ps.setString(4,"9886664");

ps.setString(5,"BAN");

ps.setString(6," ");

ps.setString(7,"SAP Netweaver EP");

ps.setString(8,"");

ps.setString(9,"sdf");

ps.executeUpdate();

}

catch(SQLException se)

{

se.printStackTrace();

throw se;

}

catch(Exception e)

{

e.printStackTrace();

throw e;

}

paras_arora3
Participant
0 Kudos

yes!! was able to catch the exception, User table which I was inserting data into is a keyoword in Microsoft SQL server. So, chose to change my database schema and works fine .

Thanks

-paras

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi Paras,

Please throw the exception in the CATCH blocks

catch(SQLException se)

{

throw se;

}

Please remove the last CATCH statement in the addUser method.

Even THROW the exception from the getConnection() method

catch (SQLException e)

{

throw e;

}

Please check now ! ! !

Former Member
0 Kudos

Hi Paras,

declare the method adduser() with the THROWS keyword specifying the exceptions that can be thrown and in the catch blocks of the exception throw the exception using the THROW keyword instead of only printing the stack trace.

"public void addUser() Throws SQLException"

catch(SQLException se)

{

throw se;

}

Even THROW the exception from the getConnection() method

catch (SQLException e)

{

throw e;

}

In the View controller, it would catch the exception and will get printed appropriately.

Lemme know if it works.

Best Regards,

-Prashant.

Message was edited by:

Prashant Patalay

paras_arora3
Participant
0 Kudos

Still not getting anything logged

Former Member
0 Kudos

Are you throwing the exception in your method?

Check this thread on enable logging

https://forums.sdn.sap.com/click.jspa?searchID=478960&messageID=319144

Regards,ANilkumar