Tuesday, March 12, 2013

LEFT JOIN in database

How to join at least two tables from database?

First create tables:

1. Table with cars:
CREATE TABLE `cars` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `make` varchar(50) DEFAULT NULL,
  `model` varchar(50) DEFAULT NULL,
  `year` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2. Table with users:
CREATE TABLE `users` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `cars_id` int(10) DEFAULT NULL,
  `name` varchar(40) DEFAULT NULL,
  `surname` varchar(40) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Now insert some rows with data to our tables.

There is foreign key - cars_id - in table users.
We will use foreign key to join cars with users.

INSERT INTO
        cars
        (
            make,
            model,
            year
        )
    VALUES
        (
            "Opel",
            "Vectra",
            "2012-05-25"
        );

INSERT INTO
        users
        (
            cars_id,
            name,
            surname
        )
    VALUES
        (
            1,
            "John",
            "Smith"
        );
And now you can join our tables:
SELECT
  *
FROM
  cars
LEFT JOIN users ON users.cars_id = cars.id

How to DELETE data from database


Deleting data from database is easy.

If you have a table with data from previous example (here is example) you can try our next code:


DELETE
    FROM
        cars
    WHERE
        id = 3;

If you don't have row with id = 3 just chane it to other number.
That is all.

How to UPDATE data in database

How to update data in database system?

Try it using previous example from here because we need table with data.

To update data in database (MySQL, PostgreSQ etc.) use below code:


UPDATE cars
    SET        
        model = "Vectra 2"
    WHERE
        id = 3;
Remember that you need to have row with id = 3 in your database.
Otherwise just change "id" to other number in the above example.

How to SELECT data from database - MySQL example

How to select data from mysql?

Short example in MySQL.

First: create new table in MySQL.


CREATE TABLE `cars` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `make` varchar(50) DEFAULT NULL,
  `model` varchar(50) DEFAULT NULL,
  `year` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

After that insert few rows of data to the table.
Below is example how to do it.
INSERT INTO
        cars
        (
            make,
            model,
            year
        )
    VALUES
        (
            "Opel",
            "Vectra",
            "2012-05-25"
        );
Now we can retrive data from MySQL:

SELECT
    id,
    make
FROM
    cars;
or (not good - because slower)
SELECT
    *
FROM
    cars;

Monday, March 11, 2013

Load content to jQuery dialog on open

How to load content to jQuery dialog on open from other file?

It is very easy.

Just use my code on your page. Put code between <body></body> tags.

1. First create new file: index.html




Open my dialog



2. Create second file: cars.html
  • Audi
  • Ford
  • Honda
  • Toyota
That is all. Now you can load content from other file when jQuery dialog is opening.

Styling jQuery dialog


Dialog is like it is.
But if we want to make it better we can restyle dialog a little ;)

Few things to restylish jQuery dialog. Looks better in each browser (IE 9+) .

1. Put my code into <body> </body> tags on your page and play with jQuery dialog styling it.



Open my dialog


jQuery dialog

Dialog is a window where we can put some hidden details on our page and make it visible on user requests.
How to create and use jQuery dialog?

1. You need jQuery library included between <head> </head> tags.
You can download it from official site.
ex.:

<head><script src="http://code.jquery.com/jquery-1.9.1.min.js"></script></head>


2. Put my code between <body> </body> tags.


Open my dialog


That is all. Have fun!

Redirect page with JavaScript - jQuery

How to redirect page with jQuery?
Create first file and paste code between <body></body> tags.

1. File name: index.html



Redirect my page to second file
My first page: index.html
Now create second file and paste code between <body> </body>  tags.

2. File name: secondFile.html


Redirect my page to index
My second page: secondFile.html
Redirecting page with jQuery is easy and fast to do.

jQuery / Refresh page


1. jQuery. How to refresh page with JavaScript using jQuery?

Paste code between <body></body> tags.
Now you can refresh page. That's all;)

If you dont believe me( ;) ) that page was reloaded just click on "Insert new text" link and you will see that text have changed in "placeToInsertText" div.
Now click "Refresh my page" link and page will be refreshed.



Refresh my page
Insert new text
My text number 1.

Java / Write to file

How to write text to file?
Construct a FileWriter object associated with a file descriptor.
Create a buffered character-output stream that uses a default-sized output buffer.
Write a String to file.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile 
{
 public static void main(String[] args) 
 {
  File file = new File( "readme.txt" );  
  try 
  {
   FileWriter fw = new FileWriter( file );
   BufferedWriter bw = new BufferedWriter( fw );
   bw.write( "New text to write to file" );
   
   bw.close();
   
   System.out.println( "Writing ended successfuly" );
  } 
  catch (IOException e) 
  {
   e.printStackTrace();
  }
 }
}


Java / Read from file

How to read from a file?
There are few ways to read from file in Java. I will show you one of the most common used method.
Create file descriptor - use File class to do this.
Create new FileReader.
Create new BufferedReader.
Read line by line.

1. Create file "readme.txt" in your project workspace and add few lines.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadFromFile 
{
 public static void main(String[] args) 
 {
  String line = null;
  File file = new File( "readme.txt" );
  
  FileReader fr = null;
  try 
  {
   fr = new FileReader( file );
  } 
  catch (FileNotFoundException e) 
  {  
   System.out.println( "File doesn't exists" );
   e.printStackTrace();
  }
  BufferedReader br = new BufferedReader( fr );
  
  try 
  {
   while( (line = br.readLine()) != null )
   {
    System.out.println( line );
   }
  } 
  catch (IOException e) 
  {
   e.printStackTrace();
  }
 }
}


Java / Create new directory

How to create new directory?
Use File class and mkdir() method.
This method returns true if directory was created otherwise false


import java.io.File;

public class MakeDir 
{
 public static void main(String[] args) 
 {
  File file = new File( "ReadmeTest" );
  boolean result = file.mkdir();
  
  if( result == true )
   System.out.println( "Directory was created" );
  else
   System.out.println( "Directory wasn't created or already exists" );
 }
}

Java / Check if is directory

How to check if given file name is directory?
Use File class and isDirectory() method.
This method returns true if directory exists otherwise false

import java.io.File;

public class IsDirecory 
{
 public static void main(String[] args) 
 {
  File file = new File("readme.txt");
  boolean result = file.isDirectory();
  
  if( result == true )
   System.out.println( "It is a direcory" );
  else
   System.out.println( "Directory doesn't exists or isn't a directory" );
 }
}

Java / Check if file is file or not

How to check if file is a file or not?
Use File class and isFile() method.
This method returns true if file is a file otherwise false


import java.io.File;

public class IsFile 
{
 public static void main(String[] args) 
 {
  File file = new File("readme.txt");
  boolean result = file.isFile();
  
  if( result == true )
   System.out.println( "It is a file" );
  else
   System.out.println( "File doesn't exists or isn't a file" );
 }
}

Java / Check if file or directory exists



How to check if file exists?
Use File class and exists() method
If file or directory exists it will return true otherwise false


import java.io.File;

public class ExistsFile 
{
 public static void main(String[] args) 
 {
  File file = new File("readme.txt");
  boolean result = file.exists();
  
  if( result == true )
   System.out.println( "File exists" );
  else
   System.out.println( "File doesn't exists" );
 }
}

Java / Delete file, directory


How to delete file or direcory in Java?
Use File class and delete() method.
This method deletes file or directory if exists and returning true on success otherwise false.

import java.io.File;

public class DeleteFile 
{
 public static void main(String[] args) 
 {
  File file = new File( "readme.txt" );
  boolean result = file.delete();
  
  if( result == true )
   System.out.println( "File deleted" );
  else
   System.out.println( "File doesn't exists" );
 }
}

Java / Create new file


How to create new file in Java?
This method creates file only if file doesn't exists.
We have to use File class and createNewFile() method.
File will be created in your workspace -> project directory.

import java.io.File;
import java.io.IOException;

public class CreateFile 
{
 public static void main(String[] args) 
 {
  File file = new File( "readme.txt" );
  
  try 
  {
   boolean result = file.createNewFile();
   
   if( result == true )
    System.out.println( "File created" );
   else
    System.out.println( "File already exists" );
  } 
  catch (IOException e) 
  {   
   e.printStackTrace();
  }
 }
}

Sunday, March 10, 2013

My first jQuery script


If you downloaded jQuery library (official site) you can start wiriting your first script.

Create new index.html page and paste code between <body> </body> tag.
When page ends loading html code jQuery will start. First you will see JavaScript alert with "This is my first jQuery code" message and after that "Hello World" text will be inserted into "myText" div.


 

Is it simple?
Remember to put jQuery (JavaScript) code in document.ready function because jQuery code have to be started after DOM loads end.

What is jQuery?

"Query is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers."
 In addition, a great advantage is that a jQuery is small library.

Now you can write JavaScript code faster. To do it just download library from official site and put path to library between<head></head> tag.




    Exaple of jQuery use
    




Java - JDBC. Connect to database.

The Java Database Connectivity (JDBC) allows you to connect to the database.
Below are examples of how to connect to the database ( MySQL and PostgreSQL) using JDBC.

Quick link:

Connect to MySQL via JDBC connector

Connect to MySQL and PostgreSQL via JDBC connector