Sunday, February 13, 2011

HAJDBC Example


Create a Java Project in Eclipse
Place the below jar files in Eclipse in lib folder or the build path

ha-jdbc-2.0.16-rc-1-jdk1.6.jar
jcl-over-slf4j-1.5.7.jar
jgroups-2.6.10.jar
jibx-run-1.2.1.jar
log4j-1.2.15.jar
quartz-1.6.5.jar

Run the below example


Example 1

package com.example;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class SimpleExample {

  public static void main(String args[]) {
    Connection con = null;
    Properties properties = new Properties(); // Create Properties object
    properties.put("user", "lportal");         // Set user ID for connection
    properties.put("password", "lportal");     // Set password for connection

    System.setProperty("ha-jdbc.configuration", "ha-jdbc{0}.xml");
    try {
      Class.forName("net.sf.hajdbc.sql.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:ha-jdbc:cluster1","lportal","lportal");
      String query="insert into test1 values ('tacobell1')";
      String query1="select 1 from test1";

      if(!con.isClosed())
      {
          DatabaseMetaData dm = con.getMetaData();
          System.out.println(dm.getDriverName());
        System.out.println("Successfully connected to " +
          "MySQL server using TCP/IP...");
      }
      Statement st = con.createStatement();
      while(true){
          st.executeQuery(query1);
      }
      /*while(true){
       * 
      st.executeUpdate(query);
      st.executeUpdate(query);
      st.executeUpdate(query);
      st.executeUpdate(query);}*/
    } catch(Exception e) {
      System.err.println("Exception: " + e.getMessage());
    } finally {
      try {
        if(con != null)
          con.close();
      } catch(SQLException e) {}
    }
  }
}

Example 2

package com.example;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class SimpleExample2 {

  public static void main(String args[]) {
    Connection con = null;
    Properties properties = new Properties(); // Create Properties object
    properties.put("user", "lportal");         // Set user ID for connection
    properties.put("password", "lportal");     // Set password for connection

    System.setProperty("ha-jdbc.configuration", "ha-jdbc{0}.xml");
    try {
      Class.forName("net.sf.hajdbc.sql.Driver").newInstance();
      con = DriverManager.getConnection("jdbc:ha-jdbc:cluster1","lportal","lportal");
      String query="insert into test1 values ('jango13')";

      if(!con.isClosed())
      {
          DatabaseMetaData dm = con.getMetaData();
          System.out.println(dm.getDriverName());
        System.out.println("Successfully connected to " +
          "MySQL server using TCP/IP...");
      }
      Statement st = con.createStatement();
      st.executeUpdate(query);
     /* while(true){
      st.executeUpdate(query);
      st.executeUpdate(query);
      st.executeUpdate(query);
      st.executeUpdate(query);}*/
    } catch(Exception e) {
      System.err.println("Exception: " + e.getMessage());
    } finally {
      try {
        if(con != null)
          con.close();
      } catch(SQLException e) {}
    }
  }
}

3 comments: