JDBC connection to Virtuoso under LOG4JDBC

In the following I describe two ways to connect. You must have already checked this page for more information. http://docs.openlinksw.com/virtuoso/VirtuosoDriverJDBC.html The JDBC driver is available under the folder /usr/local/src/virtuoso-opensource/libsrc/JDBCDriverType4 assuming you have already cloned the git repository from here https://github.com/openlink/virtuoso-opensource inside a path like /usr/local/src/ Next, you have to register the library in your CLASSPATH, see this figure a) The simple way public class Database { static String username = "dba"; static String password = "dba"; public static Connection getConnection() throws SQLException { Connection conn = DriverManager.getConnection("jdbc:virtuoso://localhost:1111/", username, password); return conn; } } Next, you can call connect to Virtuoso thus, for example: import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Test { public static void main(String[] args) throws SQLException { Connection conn = Database.getConnection(); String sql = "SELECT S FROM DB.DBA.RDF_QUAD"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1)); } } } b) The way using LOG4JDBC to log SQL queries Download the library log4jdbc4-1.2.jar from https://code.google.com/archive/p/log4jdbc/downloads to a path like /Users/myuser/Documents/workspace/Evolvement/lib Next, you have to register the library in your CLASSPATH, see attachment. "workspace" is the folder where Eclipse saves my Java projects. public class Database { static String username = "dba"; static String password = "dba"; public static ConnectionSpy getConnectionSpy() throws SQLException { DriverSpy driverSpy = new DriverSpy(); Properties props = new Properties(); props.setProperty("user", username); props.setProperty("password", password); ConnectionSpy conn = (ConnectionSpy) driverSpy.connect("jdbc:log4jdbc:virtuoso://localhost:1112/", props); return conn; } } import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Test { public static void main(String[] args) throws SQLException { Connection conn = Database.getConnectionSpy(); String sql = "SELECT S FROM DB.DBA.RDF_QUAD"; Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1)); } } } Here, this is my pom.xml just in case 4.0.0 virtuoso virtuoso 0.0.1-SNAPSHOT src maven-compiler-plugin 3.1 org.apache.jena apache-jena-libs pom 2.13.0 slf4j-log4j12 org.slf4j org.apache.commons commons-collections4 4.0 org.apache.commons commons-lang3 3.3.2 org.apache.commons commons-math3 3.4.1 org.apache.commons commons-csv 1.1 org.slf4j slf4j-log4j12 1.7.16 org.slf4j slf4j-api 1.7.6 commons-io commons-io 2.4 and also my log4j.properties that must be located inside a folder called "resources" like this /Users/myuser/Documents/workspace/Evolvement/resources # Root logger option log4j.rootLogger=INFO, stdout # Direct log messages to stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # log4j.logger.jdbc.sqlonly=INFO # log4j.logger.jdbc.sqltiming=INFO log4j.logger.jdbc.sqlonly=OFF log4j.logger.jdbc.sqltiming=OFF log4j.logger.jdbc.audit=OFF log4j.logger.jdbc.resultset=ERROR log4j.logger.jdbc.connection=ERROR log4j.logger.jdbc.resultsettable=ON References http://www.mkyong.com/java/java-properties-file-examples/
Imagen
Attachment Size
log4jdbc4-1.2.jar63.4 KB 63.4 KB
virtjdbc4_1.jar299.42 KB 299.42 KB