Pages

Wednesday, December 26, 2012

Connect Java Applications to Mysql Database in Ubuntu Using Netbeans IDE

Ubuntu Terminal

  1. Install Mysql Server and Mysql Client from Ubuntu Software Center
  2. Install JDBC Connector by typing in the terminal
    sudo apt-get install libmysql-java
  3. Check if can connect to mysql by typing
    mysql -u root -p
  4. Create required database and tables. For ex: create a database called newyear and table called party with fields id and name

Netbeans IDE

  1. Create a Java project/application in Netbeans
  2. In the main .java file add lines similar to this and save
    
    package javaapplication6;
    import java.sql.*;
    
    public class JavaApplication6 {
        
        private static final String url = "jdbc:mysql://localhost/newyear";
        private static final String user = "root";
        private static final String password = "";
            
        public static void main(String[] args) {
            System.out.println("Hello world!!");
            Statement stmt = null;
            try {
                    Connection con = DriverManager.getConnection(url, user, password);
                    System.out.println("Success");
                    stmt = con.createStatement();
                    String sql = "INSERT INTO party " + "VALUES (1, 'joe')";
                    stmt.executeUpdate(sql);
    
            } catch (Exception e) {
                    e.printStackTrace();
            }        
            
        }
    }
    
  3. Then right click on the project/application in the project explorer and click on Properties
  4. In the project properties window, select libraries tab. Then click on Add Jar/Folder button
  5. Select mysql.jar from /usr/share/java directory and then click on Ok
  6. Now save and run the project/application and check for output in the panel

Ubuntu Terminal

  1. To check if the record is inserted, type:
    mysql -u root -p
    show databases;
    use newyear;
    show tables;
    select * from party;
    

Note: Just hit an enter if no password is to be set when prompted.

Install Oracle Java in Ubuntu

These steps are for installing java on a 32 bit ubuntu/linux OS.
  1. Download jdk-7u10-linux-i586.tar.gz from http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html
  2. Download jre-7u10-linux-i586.tar.gz from http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html
  3. The downloaded files may be stored in /home/"your_user_name"/Downloads directory
  4. Remove any oracle jdk/jre binaries if any already installed
    sudo apt-get purge openjdk-\*
  5. Create directory to hold Oracle Java JDK and JRE binaries
    sudo mkdir -p /usr/local/java
  6. Go to the directory where the downloaded files are stored
    cd /home/"your_user_name"/Downloads
  7. Copy the downloaded files into /usr/local/java and go into the directory and change the permissions
    sudo -s cp -r jdk-7u10-linux-i586.tar.gz /usr/local/java
    sudo -s cp -r jre-7u10-linux-i586.tar.gz /usr/local/java
    cd /usr/local/java
    sudo -s chmod a+x jdk-7u10-linux-i586.tar.gz
    sudo -s chmod a+x jre-7u10-linux-i586.tar.gz
    
  8. Extract the compressed binaries
    sudo -s tar xvzf jdk-7u10-linux-i586.tar.gz
    sudo -s tar xvzf jre-7u10-linux-i586.tar.gz
    
  9. Now the listing of the extracted files can be viewed by:
    ls -a
  10. Edit the system PATH file /etc/profile and add the following system variables to the system path
    sudo gedit /etc/profile
  11. Add the following lines to the end of /etc/profile file
    
    JAVA_HOME=/usr/local/java/jdk1.7.0_10
    PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
    JRE_HOME=/usr/local/java/jre1.7.0_10
    PATH=$PATH:$HOME/bin:$JRE_HOME/bin
    export JAVA_HOME
    export JRE_HOME
    export PATH
    
    
  12. Save the /etc/profile file and exit
  13. Inform your Ubuntu Linux system where your Oracle Java JDK/JRE is located. This will tell the system that the new Oracle Java version is available for use.
    sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jre1.7.0_10/bin/java" 1
    sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_10/bin/javac" 1 
    sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jre1.7.0_10/bin/javaws" 1 
    
  14. Inform your Ubuntu Linux system that Oracle Java JDK/JRE must be the default Java.
    sudo update-alternatives --set java /usr/local/java/jre1.7.0_10/bin/java
    sudo update-alternatives --set javac /usr/local/java/jdk1.7.0_10/bin/javac 
    sudo update-alternatives --set javaws /usr/local/java/jre1.7.0_10/bin/javaws 
    
  15. Reload your system wide PATH /etc/profile by typing the following command:
    . /etc/profile
  16. Note your system-wide PATH /etc/profile file will reload after reboot of your Ubuntu Linux system
  17. Test to see if Oracle Java was installed correctly on your system. Run the following commands and note the version of Java:
    java -version
    javac -version
    

Tuesday, December 25, 2012

ERROR 1044 (42000): Access denied for user ' '@'localhost' to database 'test'

This error may arise when running mysql from ubuntu terminal. To fix this, type:
mysql -u root -p
in the bash instead of mysql terminal.

Friday, December 21, 2012

Delete temp files in windows


  1. Click on Start
  2. Go to run
  3. Type %temp% in the open field and click on Ok
  4. Delete all the files and folders inside the Temp directory

Run PHP file from windows command line

  1. Install WAMP in D drive
  2. In the D://wamp/www folder create a file called test.php and enter the following and save:
    
    <!--php
    echo 'Yipee! Welcome to PHP';
    ?-->
    
  3. Open the cmd (command prompt) in windows and change the directory path to: D:\wamp\bin\php\php5.4.3>
  4. Now type
    php d:\wamp\www\test.php

Thursday, December 20, 2012

Complile C program in Ubuntu


  1. In the terminal, type:
    
    sudo apt-get install build-essential
    
    
  2. Create a main directory called cprog and a sub directory called helloworld to hold the C programs
    
    mkdir -p cprog/helloworld
    
    
  3. Go into that directory
    
    cd cprog/helloworld
    
    
  4. Open a file called main.c in gedit editor
    
    gedit main.c
    
    
  5. Type the following in main.c
    
    #include
    #include
    int main()
    {
    printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");
    return(0);
    }
    
    
  6. Save and exit
  7. To compile the program, go into the respective directory, as described in step3
  8. Then type:
    
    gcc -Wall -W -Werror main.c -o helloworldC
    
  9. To execute type:
    
    ./helloworldC
    

Wednesday, December 19, 2012

Install software in ubuntu using .deb file

To install software in ubuntu using .deb file, double click on it so that it links to the ubuntu software center and installs from there.

Tuesday, December 18, 2012

Display date time in php based on country


date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
echo $date;

Thursday, December 13, 2012

CKEditor custom toolbar

In the ckeditor/config.js write the following

CKEDITOR.editorConfig = function( config )
{
 config.toolbar = 'MyToolbar';
 
 config.toolbar_MyToolbar =
 [
  { name: 'document', items : [ 'NewPage','Preview' ] },
  { name: 'clipboard', items : [ 'Cut','Copy','Paste','PasteText','PasteFromWord','-','Undo','Redo' ] },
  { name: 'editing', items : [ 'Find','Replace','-','SelectAll','-','Scayt' ] },
  { name: 'insert', items : [ 'Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'
                 ,'Iframe' ] },
                '/',
  { name: 'styles', items : [ 'Styles','Format' ] },
  { name: 'basicstyles', items : [ 'Bold','Italic','Strike','-','RemoveFormat' ] },
  { name: 'paragraph', items : [ 'NumberedList','BulletedList','-','Outdent','Indent','-','Blockquote' ] },
  { name: 'links', items : [ 'Link','Unlink','Anchor' ] },
  { name: 'tools', items : [ 'Maximize','-','About' ] }
 ];
};

Monday, December 10, 2012

Execute PHP5 script from command line in ubuntu

  1. To execute a PHP script from the command line in Ubuntu you must have php5-cli installed.
  2. sudo apt-get install php5-cli
  3. After cli is installed, go to the folder where the script resides. For ex: 
  4. cd /var/www/test
    php5 index.php
    

Thursday, December 6, 2012

Set netbeans as default editor in ubuntu

  1. Install ubuntu tweak
    
    sudo apt-add-repository ppa:tualatrix/ppa
    sudo apt-get update
    sudo apt-get install ubuntu-tweak
    
    
  2. Find the MIME description for your file extension
    1. Right click on the file and click on properties
    2. In the properties window, note the text shown after Type:
    3. This is the mime description we are looking for. for ex: PHP script
  3. Add a custom program to open your filetype
    1. Start Ubuntu Tweak from the Dash, and click on the Admins tab on top; then click on the File Type Manager entry on the bottom:
    2. After the File Type Manager opens, click on All in the left sidebar, and uncheck the Only show filetypes... box at the bottom:
    3. Select any filetype on the right side, and begin typing the first few letters of the MIME description from Step 2 to automatically search and select your filetype:
    4. Double-click on your filetype, which is now selected, to edit its associated commands.
      1. Click on Add, and in the Add Application window, expand the Custom Command option on the bottom
      2. Type the command/program you want or use the Browse button to navigate to it and select it; here I have selected the Netbeans editor from my /opt folder:
        
        /opt/netbeans-7.2.1/bin/netbeans
        
        
      3. Click on Add, so the new command is now the default, and then click Close:
  4. Now right click on file and click on properties. Click on 'open with' tab. The default application should be netbeans. Close this window and open the file. It should open in Netbeans