Archive for December, 2008

Export:

mql export Table BOMAnalysisReport xml into file file.xml;

Import: First show what would be imported and then import:

mql import Table * overwrite from file file.xml;
mql import list Table * overwrite from file file.xml;

Tagged with , .

Often it’s necessary to get the number of table rows from a database:

1
2
3
4
5
6
7
8
String sql = "SELECT COUNT(*) AS rowcount FROM table";
int anzahl = 0;
Statement s = con.createStatement();
ResultSet r = s.executeQuery(sql);
r.next();
anzahl = r.getInt("rowcount");
r.close();
s.close();

Tagged with , , .

It is useful to use prepared Statements instead of normal Statements if there are similar queries against the database.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Database connection
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
 
// Normal Statement
Statement statement = con.createStatement();
ResultSet rs1 = statement.executeQuery("select * from customer");
while (rs1.next()) {
  String customerName = rs1.getString("customerName");
}
rs1.close();
statement.close();
 
// Prepared Statement
PreparedStatement preparedStatement = con.prepareStatement("select * from customer where customerName = ?);
preparedStatement.setString(1, "MyCustomer");
ResultSet rs2 = preparedStatement.executeQuery();
while (rs2.next()) {
  String customerName = rs2.getString("customerName");
}
rs2.close();
preparedStatement.close();
con.close();

Tagged with , , .

It’s possible to tell the Garbage Collector that it’s now time to start and clean the house.

This is done by

1
System.gc();

But this will only advise the Garbage Collector to run now, it won’t force the run.

May be useful after a big data import or something like that.

Tagged with , .

Logging in Java with log4j (0)

December 10th, 2008 by Frank Niedermann, under Java.

Instead of System.out.println() and System.err.println() it is a good thing to use a logger, like log4j.

This is an example how it can be done:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// creating a new Logger
private static final Logger logger = Logger.getLogger(MyLogger.class);
 
// configuration from XML-file
DOMConfigurator.configureAndWatch("logConfiguration.xml");
 
// log something
logger.info("Start");
 
// change log level
logger.setLevel(Level.ALL);
 
// log something more
logger.debug("Yep I was started");
logger.error("There is nothing to do!");

The configuration file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="console" class="org.apache.log4j.ConsoleAppender"> 
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/> 
    </layout> 
  </appender> 
  <appender name="logfile" class="org.apache.log4j.FileAppender"> 
    <param name="file" value="MccImporter.log"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.PatternLayout"> 
      <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/> 
    </layout> 
  </appender> 
  <root> 
    <!-- ALL | DEBUG | INFO | WARN | ERROR | FATAL | OFF -->
    <priority value ="info" /> 
    <appender-ref ref="console" /> 
    <appender-ref ref="logfile" /> 
  </root>
</log4j:configuration>

I’m using two appenders, one for console output and one for a logfile. The logfile gets overwritten every time the Java application starts (Append=false).

If the log content is a very time consuming process to evaluate it could be better to first check if logging is enabled on the according level before evaluating the expressen:

1
2
3
if (logger.isEnabledFor(Level.DEBUG)) {
  logger.debug(...);
}

Tagged with , , .

First Post! (1)

December 8th, 2008 by Frank Niedermann, under Stuff.

This is the first post!

public class Blog {
  public static void main(String[] args) {
    System.out.println("Hello Blog!");
  }
}