Welcome to my blog, hope you enjoy reading
RSS

Tuesday, 18 June 2013

how to insert auto-increment varchar value in mysql

mysql insert a concat with convert (varchar to int)+1

problem:

In the mysql DB, there is a VARCHAR field with values something like this
AB123
AB124
AB999
AB1234

I strip off the "AB, convert to integer and add one.
I really want the max value if the resulting integer, and this works so far.
It returns the number 1235.

SELECT MAX( CONVERT( REPLACE( myfield, 'AB', '' ) , UNSIGNED ) ) +1 FROM mytable

Now, what I really want to do is to INSERT back into mytable the result with the "AB" at the start of the result.
For the above, I want to insert myfield= 'AB1235' into mytable.

Solution:

Sub-selects can only go in one place -- immediately after the list of fields. So, this would need to be written something like (untested):
  • INSERT INTO mytable (myfield) SELECT concat('AB',MAX( CONVERT( REPLACE( myfield, 'AB', '' ) , UNSIGNED ) ) +1) FROM mytable
the sub-select can only go immediately after the list of fields. In particular, it can't appear within another expression. In other words, you can only have "INSERT INTO ... VALUES ..." or "INSERT INTO ... SELECT ...", but not some combination of the two. However, you can select a constant:
  • INSERT INTO mytable (myfield1,myfield2) SELECT concat('AB',MAX( CONVERT( REPLACE(myfield1, 'AB', '' ),UNSIGNED))+1), 'sdfgr45t78ahlu' FROM mytable;
Share

Sunday, 16 June 2013

How to get MAX value of numeric values in varchar column in mysql

How to get MAX value of numeric values in varchar column in mysql

Lets say that I have a table which contains a column for invoice number, the data type is VARCHAR with mixed string/int values like:

invoice_number
**************
    HKL1
    HKL2
    HKL3
    .....
    HKL12
    HKL13
    HKL14
    HKL15

I tried to select max of it, but it returns with "HKL9", not the highest value "HKL15".

SELECT MAX( invoice_number )
FROM `invoice_header`

solution:

SELECT MAX(CAST(SUBSTRING(invoice_number, 4, length(invoice_number)-3) AS UNSIGNED)) FROM table
Share

Tuesday, 11 June 2013

How To Add Password Protection To PDF Using IText In Java

How To Add Password Protection To PDF Using IText In Java

iText is very powerful library. It is used extensively to create PDF files in Java applications. Although not free, iText is the de-fecto Java library for working with PDF files. It has been extensively used in different Java related products for generating and manipulating PDF files.
Let us see how iText can be used to set password protection (encryption) to a PDF file. Also how to read an encrypted PDF file in Java.
Share

How to create random string with random characters?

How to create random string with random characters?



package com.javanotes2all.java.randomgenerator;

import java.util.Date;
import java.util.Random;

public class RandomGenerator 
{
private static Random random = new Random((new Date()).getTime());
public static String generateRandomString(int length) 
  {
      char[] values = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'};

      String out = "";

      for (int i=0;i<length;i++) {
          int idx=random.nextInt(values.length);
        out += values[idx];
      }

      return out;
    }
    public static void main(String[] args) 
     {
      RandomGenerator generator=new RandomGenerator();
      System.out.println(generator.generateRandomString(10));
      System.out.println(generator.generateRandomString(10));
      System.out.println(generator.generateRandomString(10));
      System.out.println(generator.generateRandomString(10));
      System.out.println(generator.generateRandomString(10));
    }
}

OUTPUT:
qmpebje5dw
zxvpoeq59l
6y57vehsyk
j0g89kd4pa

3uz82j5gkp
Share

Monday, 10 June 2013

Convert ArrayList To Arrays In Java

Convert ArrayList To Arrays In Java

Convert ArrayList to Arrays
A lot of time I have to convert ArrayList to Arrays in my Java program. Although this is a simple task, many people don’t know how to do this and end up in iterating the java.util.ArrayList to convert it into arrays. I saw such code in one of my friends work and I thought to share this so that people don’t end up writing easy thing in complicated way.
ArrayList class has a method called toArray() that we are using in our example to convert it into Arrays.
Following is simple code snippet that converts an array list of countries into string array.
Share

Wednesday, 5 June 2013

Changing the Font Size in Eclipse

Changing the Font Size in Eclipse

Within Eclipse click Window > Preferences. This will open up the Preferences dialog with gives you about a million configuration settings to tweak.

At the top of the list on the left you will see General. Click to open this. Next click Appearances. Finally, select Colors and Fonts from under Appearances.

This is where it gets confusing. Eclipse shows a tree of what appear to be languages or plugins or something. Underneath each one are many options for specific types of text. There are many choices in this dialog with no clearly obvious one.

Well, the one you’re looking for is “Text Font” under the “Basic” node. Select this and click the “Change…” button that appears. From here, it’s just a matter of selecting the new font size and applying it.

Oh, and when you’re ready to go back to the old font size just click the Reset button to go back to your old settings.
Share

Thursday, 16 May 2013

how to submit datebox inside form-GWT

how to submit datebox inside form-GWT

The issue is DateBox doesn't have the "name" attribute:

You can set name attribute to DateBox, since it has TextBox:

                DateBox dateBox = new DateBox();
                dateBox.getTextBox().setName(name);


Share

Tuesday, 7 May 2013

JDBC Versions


JDBC Versions

1). The JDBC 1.0 API.
2). The JDBC 1.2 API.
3). The JDBC 2.0 Optional Package API.
4). The JDBC 2.1 core API.
5) The JDBC 3.0 API.
6) The JDBC 4.0 API.

Share

Monday, 6 May 2013

Java Versions,Features and History

Java Versions, Features and History

Java Released dates and name with Features.

JDK Version 1.0
          Code named Oak and released on January 23, 1996.

Share

SENDING FREE SMS USING JAVA

SENDING FREE SMS USING JAVA


PRE-REQUISTIES

To leverage one of these services, you need to create an account with them. For the purpose of this blog we will be using fullonsms. Create an account into the website. 
For demo purpose we will assume that mobile number /username of account is DEMO_USER and password is DEMO_PASSWORD.

Share