Tuesday, June 28, 2016

MySQL JDBC jar File on OpenSUSE

First Method:
1. sudo zypper install mysql-connector-java (or go to Software Management and search for jdbc, you will find mysql-connector-java package).
2. Once it's installed, you'll have the file /usr/share/java/mysql-connector-java.jar (/usr/share/java/mysql.jar), which is an indirect symlink to the actual jar file.
3. in Eclipse, do this as: Project -> Properties -> Java Build Path -> Libraries -> Add External JARs -> select /usr/share/java/mysql-connector-java.jar


Or, Second Method:

Download the MySQL JDBC connector from http://www.mysql.com/downloads/connector/j/5.1.html.
[Platform Independent (Architecture Independent), Compressed TAR Archive]
Extract the JDBC driver JAR file from the downloaded file; for example:
tar zxvf mysql-connector-java-5.1.39.tar.gz 

Add the JDBC driver, renamed, to the relevant server; for example:
$ sudo cp mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar /usr/share/java/mysql-connector-java.jar
If the target directory does not yet exist on this host, you can create it before copying the .jar file; for example:
$ sudo mkdir -p /usr/share/java/
$ sudo cp mysql-connector-java-5.1.39/mysql-connector-java-5.1.39-bin.jar /usr/share/java/mysql-connector-java.jar

I did the first method.

******************************
Test if JDBC Connection is Suffessful:

import java.sql.*;  
class JdbcTest{  
public static void main(String[] args) throws Exception {  
System.out.println("Connecting database...");

Connection connection = null;
String url = "jdbc:mysql://localhost:3306/cs_project";
String username = "root";
String password = "vijaya";
try {
Class.forName("com.mysql.jdbc.Driver");  
connection = DriverManager.getConnection(url, username, password);
System.out.println("Connection Successful.");
// Success.
} catch (SQLException e) {
// Fail.
System.out.println("Connection Failed."+e);
} finally {
if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}
}  
}  

Tuesday, June 21, 2016

Install Sublime Text on OpenSUSE

Download Sublime Text tarball from the following link:
http://www.sublimetext.com/3
(Ubuntu 64 bit - also available as a tarball for other Linux distributions.)

Extract the downloaded file sublime_text_3_build_3114_x64.tar.bz2

Once unpacked, you will get a directory called “sublime_text_3″, inside this directory you will see a list files that required by Sublime Text to run.
For example, go to the folder sublime_text_3 and open new terminal window and type ./sublime_text, sublime text editor will open. But everytime, doing this way to open a sublime text editor is not a good idea.

For security reason, we move this folder under “/opt” location. For this enter the following command:
sudo mv sublime_text_3 /opt/ 
sudo mv Sublime\ Text\ 3 /opt/ (if folder name is Sublime Text 3)

Next create a symbolic link to call “Sublime Text” from the command line as “sublime”. To do, create a symbolic link under “/usr/bin” as shown below.
sudo ln -s /opt/sublime_text_3/sublime_text /usr/bin/sublime

Now, you are ready to use sublime text. Just type sublime in terminal, sublime text editor will open.

Regular Expression in Java for Email Validation

class RegexTest {
   public static void main(String[] args) {

      String my_regex = "^[a-z0-9._]+@[a-z0-9.-]+\\.[a-z]{2,3}$";
     
      String my_email = "tekendra587@gmail.com";
      Boolean b = my_email.matches(my_regex);
      System.out.println("Email address "+my_email+" is: " + b);
     
      String my_another_email = "user^domain.co.in";
      b = my_another_email.matches(my_regex);
      System.out.println("Email address "+my_another_email+" is: " + b);
   }
}

Sunday, June 19, 2016

Changing Extension to Multiple Files on OpenSUSE

To change extension of multiple files (suppose all .txt files to .jpg), open terminal and execute the following command:

rename txt jpg *.txt

Explanation: here, it replaces the every occurrence of txt to jpg in all file matching "*.txt"

To change back to .txt, execute the following command:

rename jpg txt *.jpg

Friday, June 17, 2016

Print Object in Java

To print objects in Java, we can use toString() method.
toString() method belongs to object class.

class Student{
String name;
int id;
String city;

Student(String name, int id, String city){
this.name = name;
this.id = id;
this.city = city;
}
public String toString(){
return " Name: "+name + " id: "+id + " city: "+city;
}

public static void main(String[] args){
Student S1 = new Student("Dj", 111, "Irving");
Student S2 = new Student("Rabi", 222, "Dallas");
System.out.println(S1);
System.out.println(S2);
}
}

Now, the output will be:
               Name: Dj id: 111 city: Irving
Name: Rabi id: 222 city: Dallas

If we don't use the toString() method and directly print the object, we get some garbage values that we cannot understand.

Thursday, June 16, 2016

ArrayList Concept in Java [2D ArrayList Java]:

import java.util.*;
class TestArrayList {
 public static void main(String args[]) {
  Scanner input = new Scanner(System.in);

  ArrayList al = new ArrayList();
  for (int i = 0; i < 2; i++) {
   System.out.print("Enter food :"); //example: Momo, 2
   String user_input = input.nextLine();
   String formatted_input[] = user_input.split("\\,"); //split into 1 D array
   al.add(formatted_input); //append it to a 2D list
  }

  System.out.println("******************\nMethod 1: to print 2D array list:");
  for (int i = 0; i < al.size(); i++) { //if it is arraylist, we need to use size() instead of .length
   String newarr[] = new String[2];
   newarr = al.get(i); //get subarray in newarr
   System.out.print(newarr[0] + " " + newarr[1] + "\n"); //get 0th and 1th element 
   //if the subarray is large, we need to use another for loop instead of just using 0 and 1
  }

  System.out.println("******************\nMethod 2: to print 2D array list: ");
  for (String[] ob : al) {
   System.out.println(ob[0] + " " + ob[1]);
  }
 }
}