Category Archives: Java

Opening eclipse with different JVM version

Sometimes we have a requirement that the eclipse has to be started by a different java version. So in order to do that we have to do that we have to edit the configuration file in eclipse.

Step 1 :

Open eclipse config file eclipse.ini in your Eclipse folder.

 

  • Add -vm yourPath\Java\jre6\bin\javaw.exe like in the screenshot below
  • Eclipse-ini-file-edit

Thank You

 

 

Advertisements

Why do we use Class.forName to register Drivers

This code is forcing the class representing the MySQL driver to load and initialize. In Java, a class is not loaded unless it is necessary that the class gets loaded. Since JDBC code usually never directly references the driver, it wouldn’t get loaded without Class.forName (or some other equivalent alternatives).

Note that it is necessary to both load and initialize the class, which are 2 different things.

static {
    try {
        java.sql.DriverManager.registerDriver(new Driver());
    } catch (SQLException E) {
        throw new RuntimeException("Can't register driver!");
    }
}

Also, note that it is not necessary to call .newInstance() — the static initializer of the Driver already registers itself as a JDBC driver.

Finally, note that with the Service Loader API it is usually not necessary to call Class.forName() to load the driver: it can be loaded automatically.

 

 

Thank you for Reading

Validating Email, Mobile, Name in Java using Regex

This program validates Email, Mobile, Name in Java using Regex. We have to import Matcher, Pattern in order to use the classes. Regex is very simple and easy way to validate.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Class1 {
public Class1() {
}
static Pattern pattern=null;
static Matcher matcher=null;
public static void main(String[] args) {
System.out.println(Class1.emailValidation(“gowtham.raam@gmail.com”));
System.out.println(Class1.phonenNumberValidation(“944-3284989”));
System.out.println(Class1.nameValidation(“Gowtham Raam”));
// System.out.println(Class1.addressValidation(“213, Tecci Park, First Floor, Sholinganallur, chennai “));

Class1 class1 = new Class1();

}
public static String emailValidation(String email)
{
String patternvalidation=”^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@”+”[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$”;
pattern = Pattern.compile(patternvalidation);
matcher = pattern.matcher(“gowthamraam@gmail.com”);
if(matcher.matches())
{
return (“Pattern is Matched”+email);
}
else
{
return (“Pattern is Not Matched”+email);
}

}
public static String nameValidation(String name)
{
String patternvalidation=”[A-Z][a-z]+( [A-Z][a-z]+)”;// This pattern Validates like “Gowtham Raam” First Letter to be in InitCaps
pattern = Pattern.compile(patternvalidation);
matcher = pattern.matcher(name);
if(matcher.matches())
{
return (“Pattern is Matched”+name);
}
else
{
return (“Pattern is Not Matched”+name);
}

}
public static String phonenNumberValidation(String phonenumber)
{
String patternvalidation=”\\d{3}-\\d{7}”;
//If pattern should be without (-) symbol replace the String by “\\d{10}”
pattern = Pattern.compile(patternvalidation);

matcher = pattern.matcher(phonenumber);
if(matcher.matches())
{
return (“Pattern is Matched”+phonenumber);
}
else
{
return (“Pattern is Not Matched”+phonenumber);
}

}

// public static String addressValidation(String phonenumber)
// {
// String patternvalidation=”\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)”;
//
// pattern = Pattern.compile(patternvalidation);
//
// matcher = pattern.matcher(phonenumber);
// if(matcher.matches())
// {
// return (“Pattern is Matched”+phonenumber);
// }
// else
// {
// return (“Pattern is Not Matched”+phonenumber);
// }
//
// }
}

Some Fascinating Programs in Java

 

 

Program for Eliminating and Sorting Separatly using Collections

 

 

 

/**

* This is a program for Eliminating and Sorting Separatly using Collections

*/

 

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashSet;

import java.util.Iterator;

public class ElimatingDuplicatesInArrayAndSorting

{

public static void main(String[] args)

{

int[] a={5, 5, 6, 7 ,8, 8, 9, 3, 2, 4, 2, 3, 2, 4, 9};

HashSet h=new HashSet();

ArrayList al=new ArrayList();

for(int i=0;i<=14;i++ )

{

Integer o=new Integer(a[i]);

h.add(o);

}

 

Iterator itr=h.iterator();

while(itr.hasNext())

{

String w=itr.next().toString();

//int u=Integer.parseInt(w);

System.out.println(“Results in Set”+w);

Integer q=new Integer(w);

al.add(q);

}

Iterator iter=al.iterator();

Collections.sort(al);

while(iter.hasNext())

{

System.out.println(“Results after sorting :”+iter.next());

//al.add(iter.next());

}

}

}

 

 

 

Prime Number and number of Prime Number

 

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

//import java.

/**

* This is a program for Checking Whether the number is Prime number and if its not a Prime number the program generates the prime number till that number

* @author Gowtham Raam

*

*/

public class PrimeNumberOrOtherPrimeNos{

public static void main(String[] args)throws Exception {

BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

String r=in.readLine();

 

int z=Integer.parseInt(r);

if(PrimeNumberOrOtherPrimeNos.Prim1(z)==1)

{

System.out.println(“This si a Prime number” +z);

}

else

{

PrimeNumberOrOtherPrimeNos.wow(z);

 

}

}

static void wow(int q)

 

{

for(int i=2;i<=q;i++)

{

int count=0;

for(int j=2;j<=i/2;j++)

{

if(i%j==0)

{

count++;

}

}

if(count==0)

{

System.out.println(“This si a Prime number” + i);

}

 

}

}

static int Prim1(int o)

{

int count=0;

for(int i=2;i<=o/2;i++)

{

if(o%i==0)

{

count++;

}

}

if(count==0)

return 1;

else

return 0;

}

}

 

 

 

 

Eliminating Duplicates by using Loop

 

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class UserInputEliminatingDuplicates

{

public static void main(String args[]) throws Exception

{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println(“Enter a  number”);

String a[]=new String[6];

for(int i=0; i<=5;i++)

{

int count=0;

String s=br.readLine();

for(int j=0;j<=i;j++)

{

if(s.equals(a[j]))

{

count++;

}

}

if(count==0)

{

a[i]=s;

}

}

for(int k=0;k<=5;k++)

{

if(a[k]==null){

System.out.println();

}

else

System.out.println(a[k]);

}

}

}

 

 

 

Oracle Java Intro Program JDBC Implementation 

 

import java.sql.*;

 

/**

* JdbcRetrieve.java – Demonstrates how to Retrieve data from Oracle

*                    database using Java JDBC.

*/

/**

* Add your JDBC drivers In order to work your Program

*/

class AnothJDBC {

public static void main (String[] args) {

try {

Class.forName(“oracle.jdbc.driver.OracleDriver”).newInstance();

String url = “jdbc:oracle:thin:@192.168.100.107:1521:XE”;

Connection conn = DriverManager.getConnection(url,”hr”,”hr”);

Statement st = conn.createStatement();

String sql;

sql = “SELECT * FROM employees”;

ResultSet rs = st.executeQuery(sql);

while(rs.next()){

//int custId = rs.getInt(“EMPLOYEE_ID”);

//String custName = rs.getString(“LAST_NAME”);

System.out.println(rs.getString(“LAST_NAME”)+”          “+rs.getString(“Employee_id”));

//System.out.println(“…CUSTOMER NAME :…”+custName);

}

conn.close();

} catch (Exception e) {

System.err.println(“Got an exception! “);

System.err.println(e.getMessage());

}

}

 

}

Interview Java Programs

Floyds Triangle

import java.io.*;
class FloydsTriangle
{
public static void main(String args[]) throws Exception
{
int num,i,j,k,s=40;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter number “);
num=Integer.parseInt(br.readLine());
for(i=1;i<=num;i++)
{
for(j=1;j<=s;j++)
System.out.print(” “);
for(k=1;k<=i;k++)
System.out.print(k);
for(k=i-1;k>0;k–)
System.out.print(k);
System.out.print(“\n”);
s–;
}
}
}

Output is:

Enter number
5
1
121
12321
1234321
123454321

 

//Reversing of String program 1
class ReversingStr1
{
public static void main(String args[])
{
String st=args[0];
String reversed=ReversingStr1.reverseMe(st);
System.out.println(“”+reversed);

}
static String reverseMe(String re)
{
StringBuilder sb=new StringBuilder();
for(int i=re.length()-1; i>=0;–i)
{
sb.append(re.charAt(i));
}
return sb.toString();
}
}

//Reversing of String program 2

class RevesingStr2
{
public static void main(String args[])
{
String st=args[0];
String reversed=RevesingStr2.reverseMe(st);
System.out.println(“”+reversed);
}
public static String reverseMe(String s)
{
if(s.length()==0)
return “”;
return s.charAt(s.length()-1)+reverseMe(s.substring(0, s.length()-1));
}

Tower of Hanoi

import java.io.*;
class TowerOfHanoi
{
public static void main(String args[])throws IOException
{
hanoi obj=new hanoi();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter number of rings”);
int N=Integer.parseInt(br.readLine());
obj.ha(‘A’,’B’,’C’,N);
}
}

class hanoi
{
void ha(char a,char b,char c,int n)
{
if(n==0)
System.out.println(“WRONG INPUT !!!”);
if(n==1)
System.out.println(“Move from “+a+” to “+c);
if(n>1)
{
ha(a,c,b,n-1);
ha(a,b,c,1);
ha(b,a,c,n-1);
}
}
}

Output

Enter number of rings
3
Move from A to C
Move from A to B
Move from C to B
Move from A to C
Move from B to A
Move from B to C
Move from A to C

 
}

 

Connecting java and mysql database

For connecting java application with the mysql database, you need to
follow 5 steps to perform database connectivity.

In this example we are using MySql as the database. So we need to know
following informations for the mysql database:

1. *Driver class: * The driver class for the mysql database is
*com.mysql.jdbc.Driver*.
2. *Connection URL: * The connection URL for the mysql database is
*jdbc:mysql://localhost:3306/test* where jdbc is the API, mysql is
the database, localhost is the server name on which mysql is
running, we may also use IP address, 3306 is the port number and
sonoo is the database name. We may use any database, in such case,
you need to replace the test with your database name.
3. *Username: * The default username for the mysql database is *root*.
4. *Password: * Password is given by the user at the time of installing
the mysql database. In this example, we are going to use root as the
password.

Let’s first create a table in the mysql database, but before creating
table, we need to create database first.

create database test;
use test;
create table emp(id int(10),name varchar(40),age int(3));

Here’s the code

import java.sql.*;
class MysqlCon
{
public static void main(String args[]){
try{
Class.forName(“com.mysql.jdbc.Driver”);

Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”,”root”,”root”);

//here test is database name, root is username and password

Statement stmt=con.createStatement();

ResultSet rs=stmt.executeQuery(“select * from emp”);

while(rs.next())
System.out.println(rs.getInt(1)+”  “+rs.getString(2)+”  “+rs.getString(3));

con.close();

}catch(Exception e){ System.out.println(e);}

}
}

To connect java application with the mysql database mysqlconnector.jar file is required to be loaded.

We have to manually download the mysqlconnector.jar from the internet and paste the mysqlconnector.jar file in jre/lib/ext folder

Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste

I use sabayon 10 and path in my os is /etc/java-config-2/current-system-vm/jre/lib/ext/mysqlconnector.jar

Now compile the code and run it.

Thank You for reading

Various methods or types to read input from Console (keyboard) in Java

The below Java program illustrates the types or methods that we can receive inputs from a user

import java.util.Scanner;
import java.io.*;
class ConsoleReadingDemo {
static String name;
    public static void main(String[] args) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(“Please enter user name : “);
        
        try {
            name = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(“You entered : ” + name);

        // ===== In Java 5, Java.util,Scanner is used for this purpose.
        Scanner in = new Scanner(System.in);
        System.out.print(“Please enter user name : “);
        name = in.nextLine();      
        System.out.println(“You entered : ” + name);
 
 
        // ====== This feature from Java 6
        Console console = System.console();
        name= console.readLine(“Please enter user name : “);   
        System.out.println(“You entered : ” + name);

    }
}

Thank you for reading

MySQL Database configuration on linux terminal tutorial

Start the database:

Start the database: /etc/rc.d/init.d/mysqld start
(The script will run mysql_install_db to create a default database in /var/lib/mysql/mysql/ if the mysql init script has never been run before. The install script will not be run again as long as the default database directory exists.)
The database executes as user mysqld and group mysqld.

Notes:

  • One may manually initialize the database with the command: /usr/bin/mysql_install_db
    Creates system tables in /var/lib/mysql/mysql/
    Only execute the first time MySQL is installed.
  • Databases located in: /var/lib/mysql/
  • Default config file installed by RPM: /etc/my.cnf
    (Ubuntu: /etc/mysql/my.cnf)

    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    
    [mysql.server]
    user=mysql
    basedir=/var/lib
    
    [safe_mysqld]
    err-log=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid
    

 

Post installation:

  1. Admin user id: root
    Default password: blankThe first task is to assign a password:

    [prompt]$ mysqladmin -u root password 'new-password'

    Note: the following SQL commands will also work:

    mysql> USE mysql;
    mysql> UPDATE user SET Password=PASSWORD('new-password') WHERE user='root';
    mysql> FLUSH PRIVILEGES;
  2. Create a database: (Creates directory /var/lib/mysql/bedrock)
    [prompt]$ mysqladmin -h localhost -u root -ppassword create bedrock
    

    (or use SQL command: CREATE DATABASE bedrock;)
    Show all mysql databases: mysqlshow -u root -ppassword

     

  3. Add tables, data, etc:
    Connect to database and issue the following SQL commands:

    [prompt]$ mysql -h localhost -u root -ppassword
    ...
    mysql> show databases;             -- List all databases in MySQL.
    +----------+
    | Database |
    +----------+
    | bedrock  |
    | mysql    |
    | test     |
    +----------+
    mysql> use bedrock;     -- Specify database to connect to. Also refers to path: /var/lib/mysql/bedrock
    mysql> create table employee (Name char(20),Dept char(20),jobTitle char(20));
    mysql> DESCRIBE employee;       -- View table just created. Same as "show columns from employee;"
    +----------+----------+------+-----+---------+-------+
    | Field    | Type     | Null | Key | Default | Extra |
    +----------+----------+------+-----+---------+-------+
    | Name     | char(20) | YES  |     | NULL    |       |
    | Dept     | char(20) | YES  |     | NULL    |       |
    | jobTitle | char(20) | YES  |     | NULL    |       |
    +----------+----------+------+-----+---------+-------+
    3 rows in set (0.03 sec)
    
    mysql> show tables;
    +-------------------+
    | Tables_in_bedrock |
    +-------------------+
    | employee          |
    +-------------------+
    
    mysql> INSERT INTO employee VALUES ('Fred Flinstone','Quarry Worker','Rock Digger');
    mysql> INSERT INTO employee VALUES ('Wilma Flinstone','Finance','Analyst');
    mysql> INSERT into employee values ('Barney Rubble','Sales','Neighbor');
    mysql> INSERT INTO employee VALUES ('Betty Rubble','IT','Neighbor');
    

    Note: Data type used was CHAR. Other data types include:

    • CHAR(M) : Fixed length string. Always stores M characters whether it is holding 2 or 20 characters. Where M can range 1 to 255 characters.
    • VARCHAR(M) : Variable length. Stores only the string. If M is defined to be 200 but the string is 20 characters long, only 20 characters are stored. Slower than CHAR.
    • INT : Ranging from -2147483648 to 2147483647 or unsigned 0 to 4294967295
    • FLOAT(M,N) : FLOAT(4,2) – Four digits total of which 2 are after the decimal. i.e. 12.34 Values are rounded to fit format if they are too large.
    • DATE, TEXT, BLOB, SET, ENUM

     

  4. Add a user. Use the MySQL SQL console to enter SQL commands. The command mysql with the correct login/password will connect you to the database. The admin tables are stored in the database “mysql”.
    [prompt]$ mysql -h localhost -u root -ppassword
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 1 to server version: 3.23.41
    
    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    
    mysql> USE mysql;
    mysql> SHOW TABLES;
    +-----------------+
    | Tables_in_mysql |
    +-----------------+
    | columns_priv    |
    | db              |
    | func            |
    | host            |
    | tables_priv     |
    | user            |
    +-----------------+
    mysql> INSERT INTO user (Host, User, Password, Select_priv) 
                  VALUES ('', 'Dude1', password('supersecret'), 'Y');
    mysql> FLUSH PRIVILEGES;   -- Required each time one makes a change to the GRANT table
    mysql> GRANT ALL PRIVILEGES ON bedrock.* TO Dude1;
    mysql> FLUSH PRIVILEGES;   -- Required each time one makes a change to the GRANT table
    mysql> quit

    Note:

    • There is NO space between the -p and the password! You can omit the password and you will be prompted for it.
    • The SQL flush command is equivalent to issuing the command:
      [prompt]$ mysqladmin reload
      

     

  5. Test the database:
    mysql> SELECT * from employee;
    +-----------------+---------------+-------------+
    | Name            | Dept          | jobTitle    |
    +-----------------+---------------+-------------+
    | Fred Flinstone  | Quarry Worker | Rock Digger |
    | Wilma Flinstone | Finance       | Analyst     |
    | Barney Rubble   | Sales         | Neighbor    |
    | Betty Rubble    | IT            | Neighbor    |
    +-----------------+---------------+-------------+
    1 row in set (0.00 sec)
    
    mysql> SELECT name FROM employee WHERE dept='Sales';
    +---------------+
    | name          |
    +---------------+
    | Barney Rubble |
    +---------------+
    1 row in set (0.00 sec)

     

  6. Quit from the SQL shell:
    [prompt]$ quit
    

     

  7. Shutting down the database:
    [prompt]$ mysqladmin -u root -ppassword shutdown       - PREFERRED
    OR
    [prompt]$ /etc/rc.d/init.d/mysqld stop
    OR
    [prompt]$ service mysqld stop
    

Documentation in /usr/share/doc/mysql-3.23.41/ (local file)

 


Users and Security:

 

Create a database user:

     mysql> CREATE USER david@'localhost' IDENTIFIED BY 'password';

 

or generate a user by adding them to the user table directly:

     [prompt]$ mysql -h localhost -u root -p 
     mysql> use mysql;
     mysql> INSERT INTO user (Host,User,Password) VALUES('localhost','david',PASSWORD('supersecret'));

Note that the user is defined in the “user” mysql table.

 

Assign user privileges:

Security and database access is controlled by the GRANT tables. Access to connect to the database and access to process the transaction (table and column access, etc.) are both required. Privileges are searched in the following order:

  1. user table
  2. db and host table
  3. tables_priv
  4. columns_priv

Use the user table to grant connection privileges to database by a user (host, user name and password). Grant database and table access for transaction access. i.e. grant “SELECT”, “UPDATE”, “CREATE”, “DELETE”, “ALTER” etc. permission for database, table, field (columns) or database server access.

Access can be granted by network permissions: GRANT ALL PRIVILEGES on bedrock.* to david@'192.168.10.0/255.255.255.0';
This grants access from nodes 192.168.10.0 – 192.168.10.255. Or the network definitions can reference resolvable names: ‘%.domain.com‘. The host definition of ‘%‘ or ” (null) refers to any host. (..according to the documentation. My experience is that in the mysql.user table use only ‘%’ for “Host” to refer to any host.)

     mysql> GRANT ALL PRIVILEGES on bedrock.* to david@'%';
     mysql> FLUSH PRIVILEGES;

or (be specific)

     mysql> GRANT SELECT,INSERT,UPDATE,DELETE on bedrock.* to david@'%' identified by 'david';
     mysql> FLUSH PRIVILEGES;

or (more promiscuous – global privileges rather than database specific)

     mysql> GRANT ALL PRIVILEGES on *.* to david@'%' identified by 'david';
     mysql> FLUSH PRIVILEGES;

or (be specific by direct assignment in the mysql “Db” table:)

     mysql> use mysql;
     mysql> INSERT INTO db (Host,Db,User,Select_priv,Insert_priv) VALUES('localhost','bedrock','david','Y,'Y');
     mysql> FLUSH PRIVILEGES;

Note that database specific privileges (eg. Select_priv, Insert_priv, etc) are defined in the “db” mysql table. The mysql “user” table can assign the same (Select_priv, Insert_priv, etc) but global privileges (usually all default to ‘N’).Show privileges: SHOW GRANTS FOR Dude2@'%';

Network security: Use firewall rules (ipchains or iptables) to block internet access to port 3306. (default port used by MySQL)

Note: I have found that when adding access from “anywhere” (‘%’), the MySQL database table ‘user’ requires two entries, ‘localhost’ and ‘%’. Also, it is typically safer to allow more privileges to those with ‘localhost’ access than to users from ‘%’ (“anywhere”).

 

Passwords and connecting to the databse:

  • Connect: [prompt]$ mysql -h host_name -u user_name -ppassword
  • Using default blank password: [prompt]$ mysql -h localhost -u root -p
    If a password is required, you will be prompted. Note, blank passwords are a security hole which has already lead to one mySQL internet worm. Change any default blank passwords.
  • Delete null/blank users: DELETE FROM user WHERE User = '';
  • Beware of open access permissions from hosts '%': SELECT * FROM db WHERE Host = '%';
  • Change a password:
        [prompt]$ mysqladmin -u root -p password new-password

    You will be prompted to enter the old root password to complete this command.
    or:

        [prompt]$ mysqladmin -u root -pold-password password new-password

    or:

        mysql> SET PASSWORD FOR root@'localhost' = PASSWORD('supersecret');
        mysql> FLUSH PRIVILEGES;
    
  • As an added security precaution it is wise to delete any user id not used. i.e. any defaults generated for demonstration purposes.
  • Note that the default port used by MySQL is 3306. This can be protected with firewall rules. See the YoLinux IpTables tutorial.

Debian/Ubuntu upgrades: Note that the Debian/Ubuntu distribution will have an additional file /etc/mysql/debian.conf. This file holds a password for the user “debian-sys-maint” which is used by the install tool dpkg to perform database upgrades. This can also be used in emergencies if you forget the root password. It is also a security hole if the file is available to others.

[Potential Pitfall]: It is very easy to make mistakes which get entered into important tables. If you enter the command twice you may have one incorrect and one correct entry. Look at the table data after a mistake to see what happened in case it needs to be fixed.
Example:

mysql> USE mysql;
mysql> SELECT User,Password,Host from user;
+-------+------------------+------------+
| User  | Password         | Host       |
+-------+------------------+------------+
| root  | 99a1544eb571ad63 | localhost  |
|       |                  | localhost  |
| Dude1 | 81a10dba5f6f2144 |            |
| Dude1 |                  |            |
| Dude2 | 92b10dba6f7f3155 | %          |
+-------+------------------+------------+
5 rows in set (0.00 sec)
mysql> DELETE FROM user WHERE User='' AND Host='localhost';
mysql> DELETE FROM user WHERE User='Dude1' AND Password='';
mysql> FLUSH PRIVILEGES;
mysql> QUIT

User entries may also be found in the table mysql.db.

mysql> DELETE FROM db WHERE User='Dude3' AND Host='localhost';

[Potential Pitfall]: Any changes (UPDATE) to the user table will require a “FLUSH PRIVILEGES” before the changes will be effective.

mysql> UPDATE user SET Host='%' WHERE User='Dude2';
mysql> FLUSH PRIVILEGES;

This will allow a connection with mysql client from any host:
[prompt]$ mysql -u Dude2 -ppassword -h node.your-domain.com

 


MySQL root password recovery:

  1. As Linux system root user stop the database process: /etc/init.d/mysql stop
    (or: service mysql stop)
  2. Start MySQL in safe mode and skip the use of the “grant tables”: /usr/bin/mysqld_safe --user=mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --datadir=/var/lib/mysql --skip-grant-tables --skip-networking &
  3. Reset the MySQL root password: mysqladmin -u root flush-privileges password newpassword
  4. Stop MySQL running in safe mode: kill `cat /var/run/mysqld/mysqld.pid`
  5. Start MySQL: /etc/init.d/mysql start
  6. The new MySQL root password can now be used: mysql -u root -p
    Respond with the password: newpassword

 


Disabling networking:

If your configuration is a web server interacting with a mySQL database running on the same “localhost” then one may turn off network access to tighten security. Edit shell script:

  • /usr/bin/safe_mysqld (Fedora Core 3)
  • /usr/bin/mysqld_safe (Red Hat Enterprise Linux 4 – MySQL 5.0)
..
...
  if test -z "$args"
  then
    $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION \
                      --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file \
                      --skip-networking --skip-locking >> $err_log 2>&1
  else
    eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION \
                      --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file \
                      --skip-networking --skip-locking $args >> $err_log 2>&1"
  fi
...
..

Add the flag “--skip-networking” marked in bold.

Mysql 5.0 configuration: Networking is disabled by default on the default Red Hat and Ubuntu installation.

Red Hat/CentOS: To enable remote database access, add the “bind-address” with the public IP address to the file: /etc/my.cnf. To force local access only without remote access, set the “bind-address” to 127.0.0.1

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
bind-address=127.0.0.1

Restart the database after making changes.

Ubuntu: To enable remote database access, comment out (or remove) the following line with a “#” in the file: /etc/mysql/my.cnf

...
...

bind-address           = 127.0.0.1

...
...

Restart the database after making changes.

 

A firewall rule can further restrict access to a single server (eg web server at 192.168.1.13):

/sbin/iptables -A INPUT -i eth0 -s 192.168.1.13 -p tcp --destination-port 3306 -j ACCEPT

or LAN only access:

/sbin/iptables -A INPUT -i eth0 -s 192.168.1.0/24 -p tcp --destination-port 3306 -j ACCEPT

 


MySQL Admin Commands:

 

  • Statistics: [prompt]$ mysqladmin version
  • List database environment: [prompt]$ mysqladmin variables
  • Show if database is running: [prompt]$ mysqladmin ping
  • Show databases available:
    [prompt]$ mysqlshow
    
    +-----------+
    | Databases |
    +-----------+
    | bedrock   |
    | mysql     |
    | test      |
    +-----------+

    OR

    mysql> SHOW DATABASES;
  • Delete database: mysql> drop database bedrock;
  • Show list of active threads in server:
    [prompt]$ mysqladmin -h localhost -u root -p processlist
    
    +----+------+-----------+----+---------+------+-------+------------------+
    | Id | User | Host      | db | Command | Time | State | Info             |
    +----+------+-----------+----+---------+------+-------+------------------+
    | 15 | root | localhost |    | Query   | 0    |       | show processlist |
    +----+------+-----------+----+---------+------+-------+------------------+
  • Delete a database: [prompt]$ mysqladmin drop database-name
  • Execute SQL from Linux command line interface:
    [prompt]$ mysql -h localhost -u root -p -e "select host,db,user from db" mysql
  • Execute SQL command file from Linux command line interface:
    [prompt]$ mysql -h localhost -u root -p database-name < text-file-with-sql-statements.sql
    
  • Loadtest (benchmark) the system:
    [prompt]$ cd sql-bench
    [prompt]$ run-all-tests
    or
    [prompt]$ mysql -vvf test < ./tests/auto_increment.tst

 


Sample SQL:

SQL requests are either administrative or data-related. The following are sample SQL segments and are not necessarily pertinent to the previous example:

Create and use a new database named “bedrock”:

mysql> CREATE DATABASE bedrock;   -- Comments follow a double dash
mysql> USE bedrock;

Create and populate table with data:

mysql> CREATE TABLE retired_employee (
                    Name char(20) DEFAULT '' NOT NULL,
                    Dept char(10) DEFAULT '' NOT NULL,
                    JobTitle char(20),
                    UNIQUE name_dept (Name,Dept)
                    );
mysql> CREATE UNIQUE index name_dept on employee (name,dept); -- avoids duplicate keys
mysql> INSERT INTO employee VALUES ("Jane Smith","Sales","Customer Rep");
mysql> INSERT INTO employee VALUES ('Jane Smith','Sales','Account Manager');
mysql> INSERT INTO employee VALUES ('Jane Smith','Engineerin','Manager');
mysql> UPDATE employee SET dept='HR' WHERE name='Jane Smith';
mysql> CREATE TABLE pet (
                 name VARCHAR(20), 
                 owner VARCHAR(20), 
                 species VARCHAR(20), 
                 sex CHAR(1), 
                 birth DATE, 
                 death DATE);

Add constraints to a table:

-- Use "auto_increment" integer column:
mysql> ALTER TABLE employee ADD EmpId INT NOT NULL AUTO_INCREMENT PRIMARY KEY; 
mysql> ALTER TABLE employee DROP INDEX name_dept;  -- get rid of index
mysql>

Interrogate an existing database:

mysql> SHOW DATABASES;
mysql> USE bedrock;
mysql> SELECT DATABASE();  -- returns current database. eg. bedrock
mysql> SELECT VERSION();
mysql> SELECT NOW();
mysql> SELECT USER();
mysql> SHOW TABLES;
mysql> DESC employee;
mysql> SHOW CREATE TABLE employee;  -- show command used to generate table
mysql> SHOW INDEX FROM employee;
mysql> SELECT DISTINCT dept FROM  bedrock;
mysql> SELECT * FROM  bedrock WHERE Name  LIKE "B%y";  -- "%" match any char: Gives Betty and Barney
mysql> SELECT * FROM  bedrock WHERE Name LIKE "B___y";  -- "_" match space: Gives Betty but not Barney
mysql> SELECT * FROM  bedrock WHERE Name RLIKE "^Betty$";  -- "^" match beginning. "$" to denote end of string
mysql> SELECT COUNT(*) FROM employee;  -- Number of records returned
mysql> SELECT Name, COUNT(*) FROM employee WHERE Name LIKE "B%y";  -- Return Names and number of records returned
mysql> SELECT * FROM pet WHERE species = "snake" OR species = "bird";
mysql> SELECT * FROM pet WHERE species = "dog" AND sex = "f";
mysql> SELECT * FROM pet WHERE birth >= "1998-1-1";
mysql> SELECT name, birth FROM pet ORDER BY birth DESC;
mysql> SELECT * FROM pet WHERE name LIKE "b%";
mysql> SELECT * FROM pet WHERE name REGEXP "^b";
mysql> SELECT species, COUNT(*) FROM pet GROUP BY species;
mysql> SELECT MAX(article) AS article FROM shop;
mysql> SELECT * FROM employee WHERE name LIKE "%Sm%";
mysql> SELECT * FROM employee WHERE name REGEXP "^Ja";

Database cleanup:

mysql> DROP TABLE employee;
mysql> DROP DATABASE bedrock;

See section 3 of MySQL manual for more examples.

Tip: Execute a shell command from the MySQL client interface, use either option:

  • system ls -l
    OR
  • \! ls -l

Example: execute the “ls” command to list files from the MySQL client.

 


Loading Data into the MySQL database:

Loading a SQL file into MySQL:

Import SQL file from MySQL client command line:

  • mysql> source file.sql
    OR
  • mysql> \. file.sql

The SQL file may have schema generation statements like CREATE TABLE ... or data load statements like INSERT INTO ... . The statements in the SQL file will be executed as if they were being specified at the MySQL client command line interface.

 


One may import data into the MySQL database from SQL files or “load” data from CSV or tab delimited files using the LOAD command:

Loading CSV or tab delimeted files into MySQL:

LOAD DATA LOCAL INFILE” vs “LOAD DATA INFILE“: The term “LOCAL” pertains to whether the file is local to the MySQL client. Without the keyword “LOCAL“, the datafile must reside on the same computer as the database server. The location of the client in this case would be irrelevant. The “LOAD DATA INFILE” has many file permission pitfalls and is thus trickey. In fact I have never been sucessful using this method with a user directory.

 

Load a tab delimited file into the database:

Command: LOAD DATA LOCAL INFILE 'file.dat' INTO TABLE employer;

Input tab delimited file: file.dat

Fred Flinstone  Quarry Worker   Rock Digger
Wilma Flinstone Finance Analyst 
Barney Rubble   Sales   Neighbor
Betty Rubble    IT      Neighbor

Note:

  • The number of tab delimeted fields MUST match the number and order of fields in the database.

 

Load a comma delimited file (CSV) into the database:

Command: LOAD DATA LOCAL INFILE "/tmp/TableData.csv" INTO TABLE employer FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY "\r\n" (Name, Dept, jobTitle);

 

Note:

  • MS/Windows generated files will have lines terminated by “\r\n”.
  • Linux/Unix generated files will have lines terminated by “\n”.
  • File locations on database server must be absolute path names, relative path or relative to the mysqld process owner’s home directory (typically /var/lib/mysql/). File locations on the client may be fully qualified or relative to the current mysql client directory.
    • Fully qualified: /tmp/TableData.csv
    • Relative to current mysql client directory: ./TableData.csv
      (Verify current directory: mysql> \! pwd)
    • Database process owner home directory: TableData.csv
      (Actual: /var/lib/mysql/TableData.csv)
  • Text strings often are encapsulated by quotes so that the strings may contain a comma without representing a new field.

[Potential Pitfalls]:

  • ERROR 13 (HY000): Can't get stat of '/tmp/TableData.csv' (Errcode: 13)
    The fils is local and you have not specified the “LOCAL” directive.
  • ERROR 29 (HY000): File '/var/lib/mysql/test/TableData.csv' not found (Errcode: 2)
    Error from command LOAD DATA INFILE 'TableData.csv' INTO ... where the file is assumed to be read from the /database-process-home-directory/mysql-database-name/TableData.csv
    (Note: Database name “test” is being used.)
  • ERROR 1045 (28000): Access denied for user 'user1'@'%' (using password: YES)
    OR
    ERROR 2 (HY000): File '/tmp/TableData.csv' not found (Errcode: 2)
    Error from command LOAD DATA INFILE '/tmp/TableData.csv' INTO .... This is a common pitfall, trying to load a file located on the database server remotely from a client. Getting the file permissions correct is difficult. Avoid this method. Use the LOAD DATA LOCAL INFILE instead of the LOAD DATA INFILE method (it is so much easier).

Also look at the mysqlimport command.


Dump/Backup/Transfer Database:

The mysqldump command will read the mySQL database and generate a SQL command text file. This allows data to be migrated to other versions of mySQL (i.e. upgrade from typical Red Hat (RH7.x to FC3) mySQL release 3.23.58 to a more advanced mySQL 4.1 or 5.0) or to other SQL databases. SQL command file generated can create tables, insert data, ….

 

Option Description
-A
–all-databases
Dump all the databases.
-B
–databases
Dump the specified databases.
-h
–host=
Specify host to connect to.
-p
–password=
Specify password. If you do not specify a password, then you will be queried.
-u
–user=
Specify user. Defaults to current user logged in.
–opt Same as: –add-drop-table –add-locks –all –extended-insert –quick –lock-tables
–add-drop-table Add a “drop table” SQL statement before each “create” SQL statement.
–add-locks Add “lock” SQL statements around “insert” SQL statements.
-a
–all
Include all mySQL specific SQL “create” options.
-e
–extended-insert
Allows utilization of the new, much faster INSERT syntax. Database you are migrating to must support this notation.
-q
–quick
Don’t buffer query, dump directly to stdout.
-l
–lock-tables
Lock all tables for read.
-?
–help
Display command line options.

Examples:

  • Dump database to a file:
    • Dump specified database:
      mysqldump --opt database > db-dump-file.sql
    • Dump specified table in database:
      mysqldump --opt database table-name > db-dump-file.sql
    • Dump multiple databases:
      mysqldump --opt --databases database1 database2 database3 > db-dump-file.sql
    • Dump everything:
      mysqldump --opt --all-databases > total-db-dump-file.sql
      mysqldump -u user-id -h host-name --opt --all-databases > total-db-dump-file.sql

    [Potential Pitfall]: If you experience the following error:

    mysqldump: Got error: 1016: Can't open file: 'Database-Name' (errno: 145) when using LOCK TABLES

    Fix with the following command: mysqlcheck -r -u root -p Database-Name

  • Import dumped file:
    mysql database < db-dump-file.sql
  • Export from one database and import to another:
    • Transfer specifed database from one database to another:
      mysqldump --opt database | mysql --host=host-name -C database

Man Page:

Upgrading to 4.1:

 


Restore MySql Database:

Restore using dump generated by mysqldump above:

  • mysql -h host-name -u user-id -psupersecretpassword < total-db-dump-file.sql
  • mysql database-name -h host-name -u user-id -psupersecretpassword < db-dump-file.sql

 


System Notes:

[Potential Pitfall]: Ubuntu mysql 5.0 database migration – When migrating the mysql database by copying files from /var/lib/mysql/... and /etc/mysql/... from one system running Ubuntu 6.11 to 8.04, I got nebulous error message in /var/log/syslog. The root cause of the problem was apparmor. If turing off apparmor (/etc/init.d/apparmor stop) allows your database to start and function properly, then go fix your apparmor security rules in /etc/apparmor.d/usr.sbin.mysqld. Also note that you must use the newer script /etc/mysql/debian-start from release 8.04 after copying /etc/mysql/....

Note: Debian and Ubuntu distributions manage mysql package upgrades using a mysql user debian-sys-maint which has its information located in /etc/mysql/debian.cnf. If you ever forget your mysql root password, you can always get back into the mysql database using the user debian-sys-maint and its password held in /etc/mysql/debian.cnf.

 


Building MySql from source: (on Linux)

Prerequisites:

  • C compiler: 2.95.2 or later. (Check with the command: rpm -q gcc)

Compile and install: (as root)

  • Downloaded source from http://dev.mysql.com/downloads/mysql/4.1.html
  • Expand tar file: tar xzf mysql-4.1.16.tar.gz
  • cd mysql-4.1.16
  • ./configure --prefix=/opt/mysql --sysconfdir=/opt/etc --localstatedir=/opt/var/mysql --with-unix-socket-path=/opt/tmp/mysql.sock
    (Use the command ./configure --help to see all options.)
    This should create an installation which will not clobber an existing RPM mySQL installation.
  • make
  • make install
  • Create mysql config file: cp support-files/my-medium.cnf /opt/var/my.cnf
  • Create user/group mysql
    • Test if user/group mysql already exists: groups mysql
    • Create group: groupadd mysql
    • Create user: useradd -g mysql -M -r -d /opt/lib/mysql -s /sbin/nologin -c "MySQL Server" mysql
  • chown -R mysql:mysql /opt/var/mysql

Configure:

  • Install default database: /opt/mysql/bin/mysql_install_db --user=mysql
    Since this command is run as root, specify the --user option to operate command as user mysql.
    Creates help database with SQL script: /opt/mysql/share/mysql/fill_help_tables.sql
  • Start mySQL database: /opt/mysql/bin/mysqld_safe --user=mysql &
  • /opt/mysql/bin/mysqladmin -u root password 'new-password'
  • /opt/mysql/bin/mysqladmin -u root -h yoserver2 password 'new-password'
  • See tutorial above for use and administration.
  • Check defaults: (Defaults from config file: /opt/var/my.cnf)
    • /opt/mysql/bin/my_print_defaults --config-file=my client mysql
      --password=supersecret
      --port=3306
      --socket=/opt/tmp/mysql.sock
      --no-auto-rehash
    • /opt/mysql/bin/my_print_defaults --config-file=my client mysql mysql_install_db
      --datadir=/var/lib/mysql
      --socket=/var/lib/mysql/mysql.sock
      --password=supersecret
      --port=3306
      --socket=/opt/tmp/mysql.sock
      --port=3306
      --socket=/opt/tmp/mysql.sock
      --skip-locking
      --key_buffer=16M
      --max_allowed_packet=1M
      --table_cache=64
      --sort_buffer_size=512K
      --net_buffer_length=8K
      --read_buffer_size=256K
      --read_rnd_buffer_size=512K
      --myisam_sort_buffer_size=8M
      --log-bin
      --server-id=1

 


Commands/Man pages:

 

  • isamchk – Check and repair of ISAM tables.
  • isamlog – Write info about whats in a nisam log file.
  • msql2mysql
  • my_print_defaults
  • myisamchk
  • myisamlog
  • myisampack
  • mysql – text-based client for mysqld, a SQL-based relational database daemon
  • mysql_config
  • mysql_convert_table_format
  • mysql_find_rows
  • mysql_fix_privilege_tables
  • mysql_install_db
  • mysql_setpermission
  • mysql_zap – a perl script used to kill processes
  • mysqlaccess – Create new users to mysql.
  • mysqlbinlog
  • mysqlbug
  • mysqlcheck
  • mysqld_multi – Used for managing several mysqld processes running in different UNIX sockets and TCP/IP ports.
  • mysqldump – text-based client for dumping or backing up mysql databases , tables and or data.
  • mysqldumpslow
  • mysqlhotcopy
  • mysqlimport
  • mysqlshow – Shows the structure of a mysql database (databases,tables and columns)
  • mysqltest
  • pack_isam
  • perror – used to display a description for a system error code, or an MyISAM/ISAM table handler error code.
  • replace – A utility program to replace changes strings in place in files or on the standard input.
  • resolve_stack_dump
  • resolveip

Server:

  • mysqladmin – A utility for performing administrative operations
  • safe_mysqld – The recommended way to start a mysqld daemon on Unix.

 


Admin GUI Tools:

 

 

Interfaces in JAVA

  • The interface is a concept by which we can achieve full abstraction in java.
  • Abstraction is a process of hiding the implementation details and showing only functionality to the user. In other words it shows only important things to the user and hides the internal details
  • By means of interface we are able to achieve multiple inheritance.
  • Interface should be declared with interface keyword and interface name

Ex: interface PrintableInt

  • We can nest Interface inside an Interface.
  • We can have abstract methods in the interface.
  • Interface can extend another Interface.
  • Inside a interface class we can only have abstract methods.
  • Interface takes the method declared as public abstract method by default
  • Interface takes the data members as public,final static by default.

Why do we need Interface

Imagine that you have a project for a Pet animal shop. Here you have an animal class a dog class and a cat class. Now you can create object for Dog and Cat class which means it has a behavior. For example dog it should bark sound and cat should give Meaw sound. But what does animal give. Does this make sense. No so in order to overcome this problem we go for this abstract class where the compiler will not allow us to create object. So in this case Abstract will help

But you have categories as Pet Animal shop, Animal shop and Robot Animal Toy shop. Here if you have animal class and Pet class that have same methods. In your project you want to have multiple inheritance but compiler will not allow you to do that. If compiler allows you to do that, your program will suffer from Death Diamond Problem. In order have multiple inheritance and full abstraction we go for this Interface concept.

Deadly Diamond of Death problem

Consider a example that you have a sub class as ComboDrive now we have got both run method in both super class. By this compiler gets confused which superclass to call. But we have a requirement that we need multiple inheritance. In order to overcome our requirement there is interface concept to help us

A simple program with interface and abstract class

interface Pet

{

abstract void wow();

}

abstract class Animal

{

public static void main(String args[])

{

System.out.println(“First Interface “);

}

}

This is a basic program for just interface syntax

A simple program with interface,multiple inheritance and abstract class

interface PetAnimal
{
abstract void grow();
}
interface Animal extends PetAnimal
{
abstract void grow();
abstract void move();
}

abstract class Dog implements Animal, PetAnimal
{
public void grow()
{
System.out.println(“Ceasar is my pet and it grows”);
}
public void move()
{
System.out.println(“Ceasar will walk around and Caesar moves quick”);
}
}
class InterSecond extends Dog
{
public void grow()
{
System.out.println(“Ceasar is my pet and it grows”);
}
public void move()
{
System.out.println(“Ceasar will walk around and Caesar moves quick”);
}

public static void main(String args[])
{
InterSecond t=new InterSecond();
t.grow();
t.move();
System.out.println(“First Interface “);
}
}

A simple program with interface,multiple inheritance and abstract class tagged Interface or marker Interface

interface PetAnimal

{

abstract void grow();

}

interface Geanny//tagged Interface for serializable, cloneable, Remote

{

}

interface Animal extends PetAnimal

{

int i=10;

void grow();

abstract void move();

}

abstract class AnimalQualities implements PetAnimal, Geanny, Animal

{

public void grow()

{

System.out.println(“Animal will grow “);

}

public void move()

{

System.out.println(“Animal moves”);

}

}

class Dog extends AnimalQualities

{

public void grow()

{

System.out.println(“Ceasar is my pet and it grows”);

}

public void move()

{

System.out.println(“Ceasar will walk around and Caesar moves quick”);

}

}

class InterThiIntInsideInt

{

public static void main(String args[])

{

Dog dogObj=new Dog();

dogObj.grow();

dogObj.move();

System.out.println(“First Interface “);

}

}

Abstract classes in JAVA

Abstract

We can identify Abstract method or Abstract class by declaration of keyword abstract

Abstract class cannot be instantiated

Object cannot be created for abstract class

Abstract class can be extended

Abstract class

Abstract class cannot be instantiated and has a abstract keyword.

Concrete Class is that what be basically define with class keyword followed by class name and can be extended to a abstract class.

Abstract class can be extended.

Why Abstract class cannot be instantiated?

Imagine that you have a project for a Pet animal shop. Here you have a animal class a dog class and a cat class. Now you can create object for Dog and Cat class which means it has a behavior. For example dog it should bark sound and cat should give Meaw sound. But what does animal give. Does this make sense. No so in order to overcome this problem we go for this abstract class where the compiler will not allow us to create object.

Abstract Method

Abstract method is a method that is declared with abstract keyword and does not have implementation

Abstract method should not have a body and it should end with a semicolon

Abstract methods can be overridden. Most of the cases it will be Over Ridden

Ex : abstract void disp();

Abstract With main method

abstract class AbsMainInAbs

{

public static void main(String args[])

{

System.out.println(“Thats Wow”);

}

}

Abstract class with Inheritance to concrete class and Abstract method

class AbsBasicTry1 extends AbsMethod

{

void disp()

{//contents here

}

public void gearType()

{

System.out.println(“GearType method and AbsBasicTry1 called”);

}

void run()

{

System.out.println(“void run called”);

}

}

class AbsBasicTry

{

public static void main(String args[])

{

AbsBasicTry1 t=new AbsBasicTry1();

t.disp();

t.run();

t.gearType();

}

}

abstract class AbsMethod

{

void disp()

{

System.out.println(“this is better”);

}

abstract void run();

abstract void gearType();

}