Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
Write to db
superbeda
<p>Hi at all,</p><p>is there any way to write some information to db?</p><p>I want store the execution query with its parameters.</p>
Find more posts tagged with
Comments
kclark
<p>There are a couple of different ways to do this depending on what db you're connecting to. Since you can use Java a BIRT Report inside of the script tag, the easiest way might be to include your db jars and handle the db write as you would if you were writing a class. Another way would be to write your own custom class that takes just your data as the input parameter and allow that class to do all the writing. Then you'd just need to call SomeClass.writeToDB("some data") from your script.</p>
SailRCG
<p>I've done this using script tag at footer of a report. The script tag contained spaces for free text input; nothing fancy. You might also search the forum for "write back" report designs. </p>
superbeda
<p>Hi <span style="font-size:14px;">Kristopher,</span></p><p><span style="font-size:14px;">I didn't understand. Can I have one example how configure the report to perform this?</span></p>
kclark
<p>I've attached an example for you to take a look at. Basiclly, I created a class that handles the SQL write back that looks like this.</p><pre class="_prettyXprint _lang-">package com.actuate.developer;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class WriteBackSQL { // User properties private String username; private String password; private String database; private String hostname; private int port; public WriteBackSQL(String username, String password, String database, String hostname, int port) { this.username = username; this.password = password; this.database = database; this.hostname = hostname; this.port = port; } public void executeQuery(String query) { try { Class.forName("com.mysql.jdbc.Driver"); // Create the URL, connection, and statement String url = "jdbc:mysql://" + hostname + ":" + port + "/" + database; Connection conn = DriverManager.getConnection(url, username, password); Statement stmt = conn.createStatement(); // Execute the query stmt.executeUpdate(query); // Close the connection conn.close(); }catch(Exception ex) { ex.printStackTrace(); } } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } public void setDatabase(String database) { this.database = database; } public void setHostname(String hostname) { this.hostname = hostname; } public void setPort(int port) { this.port = port; } public String getUsername() { return username; } public String getDatabase() { return database; } public String getHostname() { return hostname; } public int getPort() { return port; }}</pre><p>The constructor takes parameters username, password, database, hostname, and port. You'd need to take this and your SQL driver and place them both in the resources tab from the property editor > properties > resources > jar files. Then to write to the db you'd use some script like this in yoru report.</p><p> </p><div><pre class="_prettyXprint">importPackage(Packages.com.actuate.developer);//username, password, database, hostname, porttest = new WriteBackSQL("root", "root", "test", "localhost", 3306);test.executeQuery("INSERT INTO test values('Name', 5);");</pre></div>
superbeda
<p>Hi <span style="font-size:14px;">Kristopher,</span></p><p><span style="font-size:14px;">thank you for the solution. It is very usefull.</span></p>
kclark
<p>You're welcome! I'm sure my Java could use some optimization for your project, I just threw it together as an example. Let us know if you have anymore questions!</p>
superbeda
<p>My problem was how use my java class in the report. now I can do it
</p>
superbeda
<p>Hi Kristopher,</p><p>I have another question: I created and I tested my report with successfull, but when I publish into iHub2 it works only if I publish it under "Resources" folder. If I put it under another folder, with the .jar file, it doesn't work. Do you know why?</p>
superbeda
<p>I resolved. I had to restart the iHub server and now it works</p>
Max Bo
<p>Could someone please guide me through the process on how to create the JAR file from this class? I can offer either Netbeans or Eclipse as IDE.</p>
superbeda
<p>If you create a java project with NetBeans and then you click the "clean and build" button, in the dist folder of your project there is the .jar file</p>
Max Bo
<p>Got it working - nice!
Thanks for the help!</p>
superbeda
<p>I'm happy to have helped you</p>
BIRT_REPORT1
<p>Thank you <span style="font-size:14px;">Kristopher. It works like a charm on my MySQL database. Would I be able to use this with other type of database connections?</span></p>