Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
import org.joget.apps.form.model.*;
import org.joget.apps.form.service.*;
import java.sql.*;
import org.apache.commons.collections.SequencedHashMap;
import java.util.*;
import org.joget.commons.util.LogUtil;
import org.joget.apps.app.service.AppUtil;

Class.forName("com.mysql.jdbc.Driver").newInstance();
try {
    con = DriverManager.getConnection("jdbc:mysql://localhost:3307/jwdb?characterEncoding=UTF-8", "root", "");
    if(!con.isClosed()){
        String sql = "INSERT INTO table(col1,col2) VALUES(?,?)";
        PreparedStatement ps = con.prepareStatement(sql);
        ps.setString(1, "coffee");
        ps.setString(2, "coffee");
        ps.executeUpdate();
        // Loggin the info message that record has been inserted
        LogUtil.info("","("+AppUtil.getCurrentAppDefinition().getAppId()+") - Record Insert Successfully.");
    } else {
       // Loggin the warning message the cant connect to database
        LogUtil.warn("","("+AppUtil.getCurrentAppDefinition().getAppId()+") - Datebase connection is not open");
    }
} catch (InstantiationException ex) {
    LogUtil.error("",ex,"("+AppUtil.getCurrentAppDefinition().getAppId()+") - can not create the instance of database driver.");
} catch (IllegalAccessException ex) {
    LogUtil.error("",ex,"("+AppUtil.getCurrentAppDefinition().getAppId()+") - IllegalAccessException OR Custom Exception message");
} catch (ClassNotFoundException ex) {
    LogUtil.error("",ex,"("+AppUtil.getCurrentAppDefinition().getAppId()+") - ClassNotFoundException - Database Driver not found");
} catch (SQLException ex) {
    LogUtil.error("",ex,"("+AppUtil.getCurrentAppDefinition().getAppId()+") - SQL Exception :- "+ex.toString());
}

You can refer [here|https://github.com/jogetworkflow/jogetworkflow/blob/master/wflow-commons/src/main/java/org/joget/commons/util/LogUtil.java] also