cancel
Showing results for 
Search instead for 
Did you mean: 

Reg:JDBC Connection

Former Member
0 Kudos

Hi,

I have created an application, where i have a separate java file for connecting JDBC. I am trying to retrieve a value from a table (with a simple select statement). I have written that code in Component Controller. The value is not getting retrieved in the context. What could be the problem?

ConnectionHolder objHolder = new ConnectionHolder();

Connection con = objHolder.getConnection();

PreparedStatement pstt = con.prepareStatement("select name from emp where id = 18");

ResultSet rs;

String strName = null;

rs = pstt.executeQuery();

while (rs.next()) {

strName = rs.getString(2);

}

wdContext.currentContextElement().setEmpId(strName);

Kindly help me out. Is there any wrong in my code?

Thanks & Regards,

Dhana

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

Before doing this,check the connection is it working fine or not.

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

Connection conn= java.sql.DriverManager.getConnection("jdbc:microsoft:sqlserver://Server:Host;user=UserName;password=Password;database=DBUser");

Otherwise You can Create a Data Source alias name and connect the Database

InitialContext initialContext = new InitialContext();

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

java.sql.Connection conn = dataSource.getConnection();

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("SELECT a, b FROM TABLE2");

String strName = null;

while (rs.next()) {

strName = rs.getString(2);

WdComp[onentAPI.getMessageManager().reportSuccess(strName);

}

Disply first the str and make it map to context.

Try like this.

It might helps you.

Thanks,

Lohi.

Former Member
0 Kudos

Hi,

Please let us know the exception.

First check the mappings from componentController to your view.

try configuring the datasource in JDBC services of visualadministrator and use the following

try{

Context ctx = new InitialContext();

DataSource ds = (DataSource) ctx.lookup("jdbc/Anil");

con = ds.getConnection();

PreparedStatement pstt = con.prepareStatement("select name from emp where id = 18");

ResultSet rs;

String strName = null;

rs = pstt.executeQuery();

while (rs.next()) {

strName = rs.getString(2);

}catch(Exception e){

}

Regards, Anilkumar

roberto_tagliento
Active Contributor
0 Kudos

Look:

Maybe you are wrong on how get the connection.