Use the following Java class to manipulate Sonarqube database in postgresql.
ManipulateSonarPost.java
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestPost { private static Connection connection = null; public static void main(String[] args) throws SQLException, java.lang.ClassNotFoundException { String url = "jdbc:postgresql://localhost/sonar"; String user = "sonar"; String password = "sonar"; String driver = "org.postgresql.Driver";
Class.forName(driver); connection = DriverManager.getConnection(url+"?user="+user+"&password="+password); if ((connection == null) || (connection.isClosed())) return; System.out.println("Connection is: "+connection);
listTables(); String table = "projects"; printTable(table); connection.close();
}
private static void listTables() throws SQLException { DatabaseMetaData md = connection.getMetaData(); ResultSet rs = md.getTables(null, null, "%", null); while (rs.next()) { System.out.println(rs.getString(3)); } rs.close();
}
private static void printTable(String tableName) throws SQLException { Statement st = connection.createStatement(); ResultSet rs = st.executeQuery("SELECT * FROM " + tableName); while (rs.next()) { System.out.println("Column 1: " + rs.getString(1)); System.out.println("Column 2: " + rs.getString(2)); System.out.println("Column 3: " + rs.getString(3)); } rs.close(); st.close(); } }
Compile and Run
$ javac -cp ~/apps/sonar-3.6.1/extensions/jdbc-driver/postgresql/postgresql-9.1-901-1.jdbc4.jar ManipulateSonarPost.java
$ java -cp ~/apps/sonar-3.6.1/extensions/jdbc-driver/postgresql/postgresql-9.1-901-1.jdbc4.jar:. ManipulateSonarPost