Wednesday, November 12, 2014

Extracting data from Mysql and creating PDF file using JAVA

//Table name : customer
// Database name: vishal
//PDF name : HelloWorld.pdf

package pdfread;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class CreatePDF
{
public static void main(String[] args) throws ClassNotFoundException, SQLException
{
Document document = new Document();
Connection conn = null;
Statement stmt = null;
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/home/PdfLocation/HelloWorld.pdf"));
document.open();

Class.forName("com.mysql.jdbc.Driver");

// STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = (Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/vishal", "root", "root");

PdfPTable table=new PdfPTable(6);

System.out.println("Creating statement...");
stmt = (Statement) conn.createStatement();
String sql= "SELECT * from customer";
ResultSet rs = stmt.executeQuery(sql);

while (rs.next()) {
table.addCell(rs.getString("TRNID"));
table.addCell(rs.getString("NAME"));
table.addCell(rs.getString("DATE"));
table.addCell(rs.getString("AMOUNT"));
table.addCell(rs.getString("MATCH_FLAG"));
table.addCell(rs.getString("PS"));
}

document.add(new Paragraph("Table customer.\n"));
document.add(table);
document.close();
writer.close();
} catch (DocumentException e)
{
e.printStackTrace();
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
System.out.println("Done");
}
}

Tuesday, November 11, 2014

Using Hbase Hadoop (Steps)

Using Hbase

Hbase is upper layer to HDFS. It shows HDFS data in tablizer form.
After Hbase installation:
         check- localhost:50070
You will see data in table format.

Steps to start Hbase:

1: Format namenode
vishal@localhost:~/hadoop/bin$ ./hadoop namenode -format
2: Start Hadoop
        vishal@localhost:~/hadoop/sbin$ ./start-all.sh      
3: turn off safe mode
        vishal@localhost:~/hadoop/bin$ ./hadoop dfsadmin -safemode leave
4: Start Hbase
        vishal@localhost:~/Hbase/hbase-0.96.1.1-hadoop2/bin$ start-hbase.sh
5: Check java processes
@localhost:~/Hbase/hbase-0.96.1.1-hadoop2/bin$ jps
5523 DataNode
5965 NodeManager
6515 HQuorumPeer
6729 HRegionServer
6806 Jps
6578 HMaster
5837 ResourceManager
5700 SecondaryNameNode
5380 NameNode
6: Enter URL in Browser

        A) localhost:50070

    B) Click on Browse the filesystem



Batch Insert In MySQL using Java.



SQL Injection Safe Batch


import java.sql.Connection;
import java.sql.PreparedStatement;
String sql = "insert into student (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
for (Student stud: studentDB) {
    ps.setString(1, stud.getName());
    ps.setString(2, stud.getCity());
    ps.setString(3, stud.getPhone());
    ps.addBatch();
}
ps.executeBatch();
ps.close();
connection.close();        




Bulk data Insert in Mysql from txt file



Command:

- load data infile 'demo1.txt' into table demo fields terminated by ',' ;

   ...here demo is table name

Txt File- demo.txt

vishal,
ram,
rohit,
smita


Store file at var/lib/mysql/your_DB_Name/demo.txt
---------------------------------------------------



Tuesday, September 16, 2014

How To Change MySQL Root Password


You can change the mySQL root password using mysqladmin command as shown below. Please note that there is no space between -p and currentpassword.
# mysqladmin -u root -pcurrentpassword password 'newpassword'
Once you’ve changed it make sure you can login with your new password successfully as shown below.
# mysql -u root -pnewpassword
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.1.25-rc-community MySQL Community Server (GPL)
mysql>

Monday, September 15, 2014

CodeIgniter Installation

CodeIgniter is installed in four steps:
  1. Download and Unzip the package from http://ellislab.com/codeigniter/user-guide/installation/downloads.html.
  2. Place the extracted folder in /var/www
  3. Open the application/config/config.php file with a text editor and set your base URL (localhost). If you intend to use encryption or sessions, set your encryption key.url
  4. If you intend to use a database, open the application/config/database.php file with a text editor and set your database settings.
  5. databaseNow, in a browser, visit http://localhost/codeigniter
  6. welcumThats all.

Eclipse SQL Connection on Ubuntu

Step 1: Intall JDK-JRE on Root
Step 2: Instell Eclipse
Step 3:

import java.sql.*;
public class Demo{

    public static void main(String[] args) {
        try{
           
            Class.forName("com.mysql.jdbc.driver");
        }catch(ClassNotFoundException e){
            System.out.println(e.getMessage());
        }
       
        try{
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vish","root","vishal123");
            Statement st = con.createStatement();
            ResultSet rs=st.executeQuery("select * from emp");
            while(rs.next())
            {
                int emp_id = rs.getInt("EMPID");
                String emp_name=rs.getString("EMPNAME");
                String emp_dept=rs.getString("EMPDEPT");
                System.out.println("\nEMP ID= "+emp_id+"\nEMP Name= "+emp_name+"\nEMP Department= "+emp_dept);
               
            }
            st.close();
            con.close();
        }
        catch(SQLException e)
        {
            System.out.println(e.getMessage());
           
        }
    }

}

execute this code, you will gate error.
Step 4: https://www.youtube.com/watch?v=Ulp9mOuDWac

LAMP installation on Ubuntu

 

 Easy Steps to install LAMP

1. Install Apache

To install Apache you must install the Metapackage apache2. This can be done by searching for and installing in the Software Centre, or by running the following command.

apt-get update

sudo apt-get install apache2

2. Install MySQL

To install MySQL you must install the Metapackage mysql-server. This can be done by searching for and installing in the Software Centre, or by running the following command.

sudo apt-get install mysql-server

3. Install PHP

To install PHP you must install the Metapackages php5 and libapache2-mod-php5. This can be done by searching for and installing in the Software Centre, or by running the following command.

sudo apt-get install php5 libapache2-mod-php5

4. Restart Server

Your server should restart Apache automatically after the installation of both MySQL and PHP. If it doesn't, execute this command.

sudo /etc/init.d/apache2 restart

5. Check Apache

Open a web browser and navigate to http://localhost/. You should see a message saying It works!


6. Check PHP

You can check your PHP by executing any PHP file from within /var/www/. Alternatively you can execute the following command, which will make PHP run the code without the need for creating a file .

php -r 'echo "\n\nYour PHP installation is working fine.\n\n\n";'