[resolved] Connect to database in JAVA.

One Star

[resolved] Connect to database in JAVA.

Hello my friends, I Need to make a routine in JAVA that makes a procedure and fills a global variable with a delete select.
But i cant connect to a data base in routine in java... I Mean, I Cant load the driver in the java code .. I Tried two ways:
- Put a TJava component in a job
- Create a JAVA Routine... 
in both case I Tried:
System.out.println("Connecting");
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("DRIVER LOADED");
conexao = DriverManager.getConnection(url, user, pwd);
System.out.println("Conected")

but it don't work, says "Connection Cannot be resolved to a type"
and 'drivermanager cannot be solved'

Is there anyway to get de db Connection and make a query in it or something like this ?

thank you very much.

Accepted Solutions
Community Manager

Re: [resolved] Connect to database in JAVA.

I see....I think. You want to reuse an existing connection created by Talend. To do that, you need to look at the generated code. If you clikc on the "Code" tab in the bottom left of the Talend workspace, you will see the generated code. Search for something similar to ...

globalMap.get("conn_ 


.... in your source code. You need to get the connection variable name. You can then use it in code like below....

java.sql.Connection myConn = (java.sql.Connection)globalMap.get("conn_tOracleConnection_1");

 
I've tried this with one of my jobs and it appears to work.

All Replies
Community Manager

Re: [resolved] Connect to database in JAVA.

Why are you not using the existing Talend components to do this? It sounds like you are reinventing the wheel. From what you have described there is absolutely no reason to build a connection to a database in code from a routine in Talend. What you should be doing is using the routine to alter the data and moving that data to a DB using the Talend components. Of course, it is possible to do this, but you are simply writing a Java application if you do this. In which case, why are you using Talend?
Don't get me wrong, it is a great idea to learn Java in order to work with Talend. But if you bought a car would you get rid of the wheels and fashion new ones out of wood with a penknife?
But, if you really do want to create this in a routine, this link might help....
http://www.java2novice.com/jdbc/create-connection/
One Star

Re: [resolved] Connect to database in JAVA.

Hello rhall_2.0, no problem my friend, whats you said makes total sense. Maybe i didn't express myself ok. RS... The connection with the database i know how to do and actualy i already did it in the open talend. BUt i think I Didn't express myself ok. Here we go:
I Want to make a routine in java, using Code/Routines, ok ? 
So i already made it. But in this routine i need to connect to database, my wish is to get the existing connection that i created in Metadata/DbConnections, ok ?? 
I Have a connection there that is ok. But in my routine in java i don't know how to get that connection, understand ? Smiley Tongue....
is there some java command to do that ?  
Thank you my friend for you helps
Community Manager

Re: [resolved] Connect to database in JAVA.

I see....I think. You want to reuse an existing connection created by Talend. To do that, you need to look at the generated code. If you clikc on the "Code" tab in the bottom left of the Talend workspace, you will see the generated code. Search for something similar to ...

globalMap.get("conn_ 


.... in your source code. You need to get the connection variable name. You can then use it in code like below....

java.sql.Connection myConn = (java.sql.Connection)globalMap.get("conn_tOracleConnection_1");

 
I've tried this with one of my jobs and it appears to work.
One Star

Re: [resolved] Connect to database in JAVA.

Rhall, i tried the solution you passed and i worked friend Smiley Very Happy ... 
Thank you very much for your attention again.
Have a great day.
Community Manager

Re: [resolved] Connect to database in JAVA.

No problem. Glad I could help :-)
One Star

Re: [resolved] Connect to database in JAVA.

Hi,
I am have similar requirement. But i am getting error near globalmap in my java routine. How can i access global map inside the java routine. Please help.
Four Stars

Re: [resolved] Connect to database in JAVA.

hi,

where to write this connection code.  i am writing in  java component but its throwing exception.

 

java.sql.Connection conn =( java.sql.Connection )resourceMap.get(conn_tMSSqlOutput_1);


java.sql.Statement stmt = conn.createStatement();


String query="select * from stud_details";

java.sql.ResultSet rs = stmt.executeQuery(query);


while(rs.next())
System.out.println(rs.getInt(1));

Four Stars

Re: [resolved] Connect to database in JAVA.

Hi !

Try to do it with tDBConnection component.

It works for me.

YosraSaadallah.jpg 

 

What’s New for Talend Spring ’19

Watch the recorded webinar!

Watch Now

Agile Data lakes & Analytics

Accelerate your data lake projects with an agile approach

Watch

Definitive Guide to Data Quality

Create systems and workflow to manage clean data ingestion and data transformation.

Download

Tutorial

Introduction to Talend Open Studio for Data Integration.

Watch