Community Tip - Learn all about PTC Community Badges. Engage with PTC and see how many you can earn! X
Hello,
I have created a new custom table in SQL server to which I should populate certain data through code. I have written the sql query in code for that, I need to know how I can execute the query so that I can see the entry getting reflected in the database.
Please let me know how I should proceed.
Thanks and Regards,
Hari R
Hi,
I had to delete links from the dB and the only way to get the results we needed was to write the Java code to make our own JDBC (Java Database Connection). This is the way Windchill or any Java based app reads and writes to a dB.
Once the connection is made you can run your sql statement to do whatever.
WARNING: ADD A CHECK IN YOUR CODE TO MAKE SURE WHAT YOU ARE ADDING IS NOT GOING TO HARM THE dB. BEFORE YOU RUN YOUR COMMIT STATEMENT.
I have personally only done this to delete links between objects. In my case I got the id number of the link and then ran something like:
delete from <the link’s table> where ida2a2=<the id number>
In your case you’re adding to a table but it’s pretty much the same thing. Once you’ve established the JDBC. You should be good to go.
I can tell you the code I wrote to connect read the dB.properties file do I get the dB user and anything else required from there.
This allowed me to run the exact same code test and production systems.
The dB user password is encrypted in the dB properties file. So I had to hardcode that. I used a simple if statement to get the right password.
if (production) {
dBPassword=“xxxxx”;
} else {
dBPassword = “yyyyy”;
}
Hope this help.
David
If the code is running in a Windchill Session, you can get the SQL connection by
((WTConnection) MethodContext.getContext().getConnection()).getConnection();
Afterwards you can use the normal jdbc calls to get and update the data.