Friday, July 29, 2011

Change postgresql password on Ubuntu


How to change postgresql password on Ubuntu

 
I’m using Postgresql-8.3  on Ubuntu 9.04.

Initially , take pg_hba.conf from /etc/postgresql/8.3/main/
In that you will find an entry like this,
host    all         all         172.16..0/24        md5

Change it to
host    all         all         172.16..0/24        trust

Then restart the postgresql database..
if it is ubuntu use the command /etc/init.d/postgresql restart 

Now issue the command
sudo psql -h -d -U postgres
(it should not ask for postgresql password..). 

It will ask for your unix password
Then give the command
alter user postgres with password 'new password'
to change the password..
Thats it.. 

Tuesday, May 10, 2011

Program to format the decimal display in Java

I need a generalized format (00.00) to display the parameter ‘Speed’ in Java.
For eg:- If the speed is 10.5, I need it to be displayed in the format ‘10.50’.

The DecimalFormat Class can be used to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.

import java.text.DecimalFormat;

public class doubleFormat
{
 public static void main(String[] args)
 {
  DecimalFormat dfSpeedFormatter = new DecimalFormat("00.00");
  String strSpeed;
  double dSpeed = 0.5;
  strSpeed = dfSpeedFormatter.format(dSpeed);
  System.out.println(strSpeed);
 }
}

The output will be displayed as:
00.50



Source:- http://download.oracle.com/javase/tutorial/java/data/numberformat.html

Monday, May 2, 2011

Change file ownership in Linux

How to Change the Ownership of a File in Linux

I listed the file using the command ls -l in terminal.
The command displayed the ownership of the file as my.
-rw-r--r-- 1 my my 420 2011-05-02 11:03 IP.dat


I need to convert the ownership of IP.dat file to root

Type the command,
chown root:root IP.dat
in terminal.

The command has changed the ownership of the file to root
-rw-r--r-- 1 root root 420 2011-05-02 11:03 IP.dat

Thursday, April 7, 2011

Linux command to find the number of processes running in a PC.

Command to find the number of processes running in a PC

For eg:-, if I need to find the number of postgres processes running in my pc, just run the command,
ps -C postgres | wc -l

Monday, April 4, 2011

JSP program to write current date and time to a text file

String str;
java.util.Date currentDate = new java.util.Date(); //gets the current date
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
str = sdf.format(currentDate);

String nameOfTextFile =getServletContext().getRealPath("imp.txt");
try
{
        PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
        pw.println(str);
        //clean up
        pw.close();
}
catch(IOException e)
{
        out.println(e.getMessage());
}

Friday, March 11, 2011

Copy file permissions from one file to another

I need to copy the file permission of FirstFile.jar to SecondFile.jar.

You may know how to change file permissions using chmod but, if you already have a file with the permissions you want to assign to some other files, you can use --reference option in chmod


chmod --reference
As an example, if you have this:

-rwxrwxrwx 1 root root 0 Dec 20 15:35 FirstFile.jar
-rwx------ 1 root root 0 Dec 20 15:35 SecondFile.jar
And you run:

chmod --reference FirstFile.jar SecondFile.jar
You should then have this:

-rwxrwxrwx 1 root root 0 Dec 20 15:35 FirstFile.jar
-rwxrwxrwx 1 root root 0 Dec 20 15:35 SecondFile.jar
Just be sure, you are the owner of the files, you that you have permission to change them.

Friday, January 28, 2011

Java program to insert image as byte array to postgres database

//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);
      }
    }
  }
}