//Program to insert an image into postgres database as byte array
import java.sql.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class insertImage
{
  public static void main(String[] args)
  {
    PreparedStatement statement = null;
    {
      FileInputStream fin = null;
      try
      {
        System.out.println("Insert Image Example!");
        Class.forName("org.postgresql.Driver");
        String url = "jdbc:postgresql://x.x.x.x:5432/MYDB";
        Connection oConnection = DriverManager.getConnection(url, "username", "password");
        System.out.println("Sucessfully connected to Postgres Database");
        File imgfile = new File("myimage.jpg");
        fin = new FileInputStream(imgfile);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        for (int readNum; (readNum = fin.read(buf)) != -1;)
        {
          bos.write(buf, 0, readNum);
          byte[] bytes = bos.toByteArray();
          String sql = "INSERT INTO my_table (byte_array) VALUES (?)";
          statement = oConnection.prepareStatement(sql);
          statement.setBytes(1, bytes);
          statement.executeUpdate();
          System.out.println("Image inserted into database!");
        }
        statement.close();
        oConnection.close();
      }
      catch (Exception ex)
      {
        System.out.println("Exception:- " + ex);
      }
      try
      {
        fin.close();
      }
      catch (IOException ex)
      {
        System.out.println(" IOException:- " + ex);
      }
    }
  }
}
Friday, January 28, 2011
Monday, January 10, 2011
Java program to send and read response from a serial to usb converter (without using modem)
//Program to send and read response from a serial to usb converter
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.*;
import java.util.*;
public class PortWriter implements Runnable,SerialPortEventListener
{
  static Enumeration ports;
  static CommPortIdentifier pID;
  static OutputStream outStream;
  static SerialPort serPort;
  static InputStream is;
  static PrintStream os;
  int i = 0;
  byte[] readBuffer = new byte[10];
  int numBytes = 0;
  public PortWriter() throws Exception
  {
    try
      {
        serPort = (SerialPort) pID.open("COM25", 2000);
        System.out.println("\ngetDataBits"+serPort.getDataBits());
        System.out.println("\ngetStopBits"+serPort.getStopBits());
        System.out.println("\ngetParity"+serPort.getParity());
        System.out.println("\ngetBaudRate"+serPort.getBaudRate());
        serPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        outStream = serPort.getOutputStream();
        serPort.addEventListener(this);
        serPort.notifyOnDataAvailable(true);
        is = serPort.getInputStream();
      }
      catch (Exception e)
      {
        System.out.println("PortInUseException : " + e);
      }
    }
    public static void main(String[] args) throws Exception
    {
      ports = CommPortIdentifier.getPortIdentifiers();
      while (ports.hasMoreElements())
      {
        pID = (CommPortIdentifier) ports.nextElement();
        if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
        {
          if (pID.getName().equals("COM25"))
          {
            PortWriterThread pWriter = new PortWriterThread();
            System.out.println("USB found");
            try
            {
              String smsMessage = "TEST DATA";
              send(smsMessage);
              if (is != null)
              {
                is.close();
//System.out.println("Closing input stream..");
              }
              if (outStream != null)
              {
                outStream.close();
// System.out.println("Closing output stream..");
              }
              if (serPort != null)
                {
                serPort.close();
//System.out.println("Closing serial Port..");
              }
            }
            catch (Exception e)
            {
              System.out.println("could not write to outputstream:");
                System.out.println(e.toString());
              }
            }
          }
        }
      }
      public static void send(String cmd)
      {
        System.out.println("Sending...");
        try
        {
          outStream.write(cmd.getBytes());
          System.out.println(cmd+" send SUCCESS");
        }
        catch (Exception e)
        {
          System.out.println("Exception in send:- " + e);
        }
      }
/**
* Method declaration
*
*
* @see
*/
      public void run()
      {
        try
        {
          Thread.sleep(1000);
        }
        catch (Exception e)
        {
          System.out.println("Exception in run:-"+e);
        }
      }
/**
* Method declaration
*
*
* @param event
*
* @see
*/
      public void serialEvent(SerialPortEvent event)
      {
        switch (event.getEventType())
        {
          case SerialPortEvent.BI:
          case SerialPortEvent.OE:
          case SerialPortEvent.FE:
          case SerialPortEvent.PE:
          case SerialPortEvent.CD:
          case SerialPortEvent.CTS:
          case SerialPortEvent.DSR:
          case SerialPortEvent.RI:
          case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
          break;
          case SerialPortEvent.DATA_AVAILABLE:
          byte[] readBuffer = new byte[20];
          try
          {
            while (is.available() > 0)
            {
              int numBytes = is.read(readBuffer);
            }
            System.out.print("Reading Data>>>"+new String(readBuffer));
          }
          catch (Exception e)
          {
            System.out.println("Exception in reading from input stream:-" + e);
          }
          break;
        }
      }
    }
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.io.*;
import java.util.*;
public class PortWriter implements Runnable,SerialPortEventListener
{
  static Enumeration ports;
  static CommPortIdentifier pID;
  static OutputStream outStream;
  static SerialPort serPort;
  static InputStream is;
  static PrintStream os;
  int i = 0;
  byte[] readBuffer = new byte[10];
  int numBytes = 0;
  public PortWriter() throws Exception
  {
    try
      {
        serPort = (SerialPort) pID.open("COM25", 2000);
        System.out.println("\ngetDataBits"+serPort.getDataBits());
        System.out.println("\ngetStopBits"+serPort.getStopBits());
        System.out.println("\ngetParity"+serPort.getParity());
        System.out.println("\ngetBaudRate"+serPort.getBaudRate());
        serPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        outStream = serPort.getOutputStream();
        serPort.addEventListener(this);
        serPort.notifyOnDataAvailable(true);
        is = serPort.getInputStream();
      }
      catch (Exception e)
      {
        System.out.println("PortInUseException : " + e);
      }
    }
    public static void main(String[] args) throws Exception
    {
      ports = CommPortIdentifier.getPortIdentifiers();
      while (ports.hasMoreElements())
      {
        pID = (CommPortIdentifier) ports.nextElement();
        if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
        {
          if (pID.getName().equals("COM25"))
          {
            PortWriterThread pWriter = new PortWriterThread();
            System.out.println("USB found");
            try
            {
              String smsMessage = "TEST DATA";
              send(smsMessage);
              if (is != null)
              {
                is.close();
//System.out.println("Closing input stream..");
              }
              if (outStream != null)
              {
                outStream.close();
// System.out.println("Closing output stream..");
              }
              if (serPort != null)
                {
                serPort.close();
//System.out.println("Closing serial Port..");
              }
            }
            catch (Exception e)
            {
              System.out.println("could not write to outputstream:");
                System.out.println(e.toString());
              }
            }
          }
        }
      }
      public static void send(String cmd)
      {
        System.out.println("Sending...");
        try
        {
          outStream.write(cmd.getBytes());
          System.out.println(cmd+" send SUCCESS");
        }
        catch (Exception e)
        {
          System.out.println("Exception in send:- " + e);
        }
      }
/**
* Method declaration
*
*
* @see
*/
      public void run()
      {
        try
        {
          Thread.sleep(1000);
        }
        catch (Exception e)
        {
          System.out.println("Exception in run:-"+e);
        }
      }
/**
* Method declaration
*
*
* @param event
*
* @see
*/
      public void serialEvent(SerialPortEvent event)
      {
        switch (event.getEventType())
        {
          case SerialPortEvent.BI:
          case SerialPortEvent.OE:
          case SerialPortEvent.FE:
          case SerialPortEvent.PE:
          case SerialPortEvent.CD:
          case SerialPortEvent.CTS:
          case SerialPortEvent.DSR:
          case SerialPortEvent.RI:
          case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
          break;
          case SerialPortEvent.DATA_AVAILABLE:
          byte[] readBuffer = new byte[20];
          try
          {
            while (is.available() > 0)
            {
              int numBytes = is.read(readBuffer);
            }
            System.out.print("Reading Data>>>"+new String(readBuffer));
          }
          catch (Exception e)
          {
            System.out.println("Exception in reading from input stream:-" + e);
          }
          break;
        }
      }
    }
Tuesday, January 4, 2011
Command to find out the ports opened for an IP address
Enter the command in terminal
nmap -v x.x.x.x
This will display the ports opened for that IP address.
nmap -v x.x.x.x
This will display the ports opened for that IP address.
Subscribe to:
Posts (Atom)