Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

MS Access to SQLite Converter

Last week, i had to finish a simple code. So, i have a mdb access file. And, it must convert to sqite file. I found a code on google. Regenerate it, then i used it. I want to share how to use it.


I use these method to use it,
> first of all, i downloaded sqlite plugin of netbeans from http://plugins.netbeans.org/PluginPortal/
> secondly, installed into netbeans.
> used it as plugins.

> also, mdb-sqlite libs should be inserted as library into librarymanager.

 
Then, project properties like this.

Finally, be careful to compile project. Because, it gives mainclassnot found exception error. You choice your main class before compiling process.


If you run project,

D:\MdbSqlite\dist>java -jar "MdbSqlite.jar" test.mdb test.db


j2me BouncyBall


















package com.boball.demo;

import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.*;
/**
 * @see J2ME & Threading Task
 */
public class BounceBall extends MIDlet implements CommandListener {
    /**
     * First of all, we create a display and command objects.
     * @see Display display, Command Exit
     */
    private Display theDisplay;
    private final static Command _cExit = new Command("Exit", Command.EXIT, 1);
    private final static Command _cClear = new Command("Clear", Command.SCREEN, 1);
    /**
     * Then, we will create a class to move ball on canvas
     * @see BounceCanvas class.
     */
    private BounceCanvas canvas;
    public BounceBall() {
        theDisplay = Display.getDisplay(this);
        canvas = new BounceCanvas();
        canvas.addCommand(_cExit);
        canvas.addCommand(_cClear);
        canvas.setCommandListener(this);
    }

    protected void destroyApp(boolean unconditional) {
        canvas.erase();
    }

    protected void pauseApp() {
    }

    protected void startApp() {
        theDisplay.setCurrent(canvas);
    }

    public void commandAction(Command c, Displayable d) {
        try {
            if (c == _cClear) {
                canvas.erase();
            } else if (c == _cExit) {
                destroyApp(false);
                notifyDestroyed();
            }
        } catch (Exception e) {
            debugStr(e);
        }
    }

    public void debugStr(Exception ex) {
        System.out.println("Error: " + ex.toString());
    }

    class BounceCanvas extends Canvas{
        private Random numberGen = new Random();
        public Vector balls = new Vector();

        class Ball extends Thread {
            // current & former positions:
            private int xc, yc, oldxc, oldyc;
            // x & y speeds:
            private int dx = 3, dy = 3;
            // halting machinery:
            private boolean halt = false;

            public void stop() {
                halt = true;
            }

            public Ball() {
                try {
                    xc = Math.abs(numberGen.nextInt()) % getWidth();
                    yc = Math.abs(numberGen.nextInt()) % getHeight();
                } catch (Exception e) {
                    debugStr(e);
                }
            }

            private void move() {
                oldxc = xc;
                oldyc = yc;
                xc += dx;
                yc += dy;
                if (xc <= 0 || getWidth() <= xc) {
                    dx = -dx;
                }
                if (yc <= 0 || getHeight() <= yc) {
                    dy = -dy;
                }
            }

            public void run() {
                while (!halt) {
                    move();
                    repaint(oldxc - 5, oldyc - 5, 10, 10);
                    // sleep for 10 msecs to be cooperative:
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        debugStr(e);
                    }
                }
            }

            public void paint(Graphics g) {
                try {
                    Image image = Image.createImage("/images/miniapple.jpg");
                    g.setColor(0xffffff);
                    // hata var //g.fillRect(xc, yc, getWidth(), getHeight());
                    g.drawImage(image, xc, yc, Graphics.HCENTER | Graphics.VCENTER);
                } catch (Exception ex) {
                    debugStr(ex);
                }
            }
        }

        public void erase() {
            try {
                for (int i = 0; i < balls.size(); i++) {
                    Ball b = (Ball) balls.elementAt(i);
                    b.stop();
                }
                balls.removeAllElements();
                repaint();
            } catch (Exception e) {
                debugStr(e);
            }
        }

        public void paint(Graphics g) {
            try {
                /* drawind text part*/
                g.setColor(0x00ffffff);
                g.fillRect(0, 0, getWidth(), getHeight());
                int w = getWidth();
                int h = getHeight();
                g.setColor(0xffffff);
                g.fillRect(0, 0, w, h);
                g.setColor(0x000000);
                Font mFont = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_LARGE);
                String s = "Please Push The 5 to Start";
                int stringWidth = mFont.stringWidth(s);
                int stringHeight = mFont.getHeight();
                int x = (w - stringWidth) / 2;
                int y = h / 2;
                Image image = Image.createImage("/images/bigapple.jpg");
                g.drawImage(image, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
                /* drawind image part*/
                g.setFont(mFont);
                g.drawString(s, x, y, Graphics.TOP | Graphics.LEFT);
                g.drawRect(x, y, stringWidth, stringHeight);

                for (int i = 0; i < balls.size(); i++) {
                    Ball next = (Ball) balls.elementAt(i);
                    next.paint(g);
                }
            } catch (Exception e) {
                debugStr(e);
            }
        }

        public void keyPressed(int keyCode) {
            try {
                if (keyCode == Canvas.KEY_NUM5) {
                    Ball b = new Ball();
                    balls.addElement(b);
                    b.start();
                }
            } catch (Exception e) {
                debugStr(e);
            }
        }
    }
}

KSoap in Blackberry

in blackberry, you can use ksoap api to connect .net web service. :)


public boolean LoginProcess(String userStr, String passStr) {
        boolean status = false;
        // new Thread(new Runnable() {
        // public void run() {
        String serviceUrl = getHTTPAdd();
        String serviceNamespace = "http://tempuri.org/";
        String soapAction = "http://tempuri.org/SecureLogin";
        SoapObject  rpc = new SoapObject(serviceNamespace, "SecureLogin");
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.bodyOut  = rpc;
        envelope.dotNet  = true;
        envelope.encodingStyle = SoapSerializationEnvelope.XSD;
        rpc.addProperty("customerid", userStr);
        rpc.addProperty("password", passStr);
        String payHelpString = ";interface=wifi";
        HttpTransport ht = new HttpTransport(serviceUrl + payHelpString);
        ht.debug = true;
        String result;
        try {
            ht.call(soapAction, envelope);
            result = (envelope.getResponse()).toString();
            resultOfWebCall = result;
            userParseData = toStringStr(parseSXml(result));
            checkVal = parseSXml(result)[1];
            userStrVal = parseSXml(result)[2]; // :)
            userStrAccVall = userStr;
            System.out.println("|||||||||||| |||||||||||| checkVal: " + checkVal);
            if (checkVal.compareTo("OK") == 0) {
                status = true;
            }
        } catch (Exception ex) {
            result = ex.toString();
            System.out.println(">>> inline error: "+result);
        }
        return status;
    }

Sms Encryption - Decryption

A few years ago, i tried to do a mobile sms cryption applicatio with bouncycastle.


public byte[] smsEncodedMessage ( byte[] smsEncContent , byte[] smsEncPKey ) throws Exception {
        try {
            cipherEngine = new PaddedBufferedBlockCipher ( createEngine () );
            cipherEngine.init ( true , new KeyParameter ( smsEncPKey ) );
            cipherText = new byte[ cipherEngine.getOutputSize ( smsEncContent.length ) ];
            cipherTextLength = cipherEngine.processBytes ( smsEncContent , 0 , smsEncContent.length , cipherText , 0 );
            cipherEngine.doFinal ( cipherText , cipherTextLength );
            out = new ByteArrayOutputStream ();
            dout = new DataOutputStream ( out );
            dout.writeShort ( cipherText.length );
            out.write ( cipherText );
        } catch ( Exception e ) {
            main.errorMessage ( "<ERROR BLOCK - inLine < 1 > >: " + e.getMessage () );
        }

        return out.toByteArray ();
    }

    public String smsDecodedMessage ( byte smsDecContent[] , byte[] smsDecPKey ) throws Exception {
        try {
            in = new ByteArrayInputStream ( smsDecContent );
            din = new DataInputStream ( in );
            cipherTextLength = din.readShort ();
            cipherText = new byte[ cipherTextLength ];
            in.read ( cipherText , 0 , cipherTextLength );
            cipherEngine = new PaddedBufferedBlockCipher ( createEngine () );
            cipherEngine.init ( false , new KeyParameter ( smsDecPKey ) );
            plainText = new byte[ cipherEngine.getOutputSize ( cipherTextLength ) ];
            size = cipherEngine.processBytes ( cipherText , 0 , cipherTextLength , plainText , 0 );
            cipherEngine.doFinal ( plainText , size );
            resultText = ( new String ( plainText ) ).trim ();
        } catch ( Exception e ) {
            main.errorMessage ( "<ERROR BLOCK - inLine < 2 > >: " + e.getMessage () );
        }
        return resultText;
    }