Tuesday 22 September 2015

Inode in python

import os,sys
print 'Enter file name'
name=raw_input()
fd=os.open(name,os.O_RDONLY)
info=os.fstat(fd)
print "The file Information is as follows:"
print "File inode number:",info.st_ino
print "Device number:",info.st_dev
print "Number of link:",info.st_nlink
print "UID of the file:%d" %info.st_uid
print "GID of the file:%d" %info.st_gid
print "Size:",info.st_size
print "Access time:",info.st_atime
print "Modify time:",info.st_mtime
print "Change time:",info.st_ctime
print "Protection:",info.st_mode
print "Number of blocks allocated:",info.st_blocks
print "Size of blocks:",info.st_blksize

os.close(fd)

Jdbc Connection test

import java.sql.*;

class Ass1
{
    public static void main(String args[])
    {
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data1","user1","user1");
        System.out.println("Connection Success");
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
   
    }


}

JDBC-EAsy codes Ass1 -PL1

//content is wrapped so actual see that below comments must remain comments
import java.sql.*;

class Ass1_final
{
    public static void main(String args[])
    {
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/data1","user1","user1");
        System.out.println("Connection Success");
        PreparedStatement ps = conn.prepareStatement("CREATE TABLE COMP (NAME VARCHAR(20),QTY INT(5),PRICE DOUBLE(10,4))");//CREATE TABLE table_name (column_name column_type);
        ps.executeUpdate();
        System.out.println("Tbale created and data is inserting ...");
        PreparedStatement ps1 = conn.prepareStatement("INSERT INTO COMP VALUES(?,?,?)");//ps1
        //Set the values that will replace quesetion marks
        String name="DELL";
        int qty =1;
        double price= 45000.45;
        // now replace ?'s of PS1
        ps1.setString(1,name);
        ps1.setInt(2,qty);
        ps1.setDouble(3,price);
        ps1.executeUpdate();
        System.out.println("values are inserted ");
       
        PreparedStatement ps2 = conn.prepareStatement("UPDATE  COMP SET QTY=QTY+1 WHERE NAME='DELL' ");//update
        ps2.executeUpdate();
        System.out.println("Qty updated by 1 ...");
       
        PreparedStatement ps3 = conn.prepareStatement("SELECT * FROM COMP");//display......
        ResultSet rs=ps3.executeQuery();
       
        while(rs.next())
        {
                System.out.print("NAME" + "\t"+rs.getString("NAME"));
                System.out.print("QTY" + "\t"+rs.getInt("QTY"));
                System.out.println("NAME" +"\t"+ rs.getDouble("PRICE"));
        }
       
        PreparedStatement ps4 = conn.prepareStatement("UPDATE  COMP SET QTY=QTY+1 WHERE NAME='DELL' ");//update
        ps4.executeUpdate();
       
        PreparedStatement ps5 = conn.prepareStatement("DROP TABLE COMP ");//update
        ps5.executeUpdate();
       
        System.out.println("table dropped or deleted..");
       
       
        rs.close();
        conn.close();
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
   
    }


}

Saturday 5 September 2015

Register -how to add a entry to a database using a jsp form and a servlet


watch the video


newjsp2.jsp

<%--
    Document   : newjsp
    Created on : 2 Sep, 2015, 10:25:39 PM
    Author     : harsh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Login page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <h2>Register</h2>
            <form action="NewServlet1" method="post">
                Email:<input type="email" name="email"><br>
                Name :<input type="text" name="name"><br>
                PASS :<input type="password" name="pass"><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

NewServlet1.java

// a pure processing servlet to authenticate the user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class NewServlet1 extends HttpServlet
{
   /// private static final String url = "jdbc:mysql://localhost/vusers";

    //    private static final String user = "root";

      //  private static final String pw = "mysql";
           
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/users","hall","bed");
            PreparedStatement ps = conn.prepareStatement("INSERT INTO USERNAME VALUES (?,?,?)");
            // get data of form
                        String email=req.getParameter("email");
            String username = req.getParameter("name");
            String password = req.getParameter("pass");
            // set the values for parameters
            ps.setString(1,username);
            ps.setString(2,password);
                        ps.setString(3, email);
            ps.executeUpdate();//database is updated
                //HttpSession session = req.getSession(true);
                                //session.setAttribute("Welcome",username);
                res.sendRedirect("newjsp.jsp");  // Wel is
               
       
               
        }
        catch (Exception e)
        {
                    res.sendRedirect("newjsp1.jsp");
            System.out.println(e.getMessage());
        }
    }
}
       


       

JSP Servlet Databse LoginPage

 Watch this video
newjsp.jsp

<%--
    Document   : newjsp
    Created on : 2 Sep, 2015, 10:25:39 PM
    Author     : harsh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Login page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <form action="log" method="post">
                Name :<input type="text" name="name"><br>
                PASS :<input type="password" name="pass"><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

NewServlet.java

// a pure processing servlet to authenticate the user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class NewServlet extends HttpServlet
{
   /// private static final String url = "jdbc:mysql://localhost/vusers";

    //    private static final String user = "root";

      //  private static final String pw = "mysql";
           
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        try
        {
            Class.forName("org.apache.derby.jdbc.ClientDriver");
            Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/users","hall","bed");
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM USERNAME WHERE username=? AND password=?");
            // get data of form
                       
            String username = req.getParameter("name");
            String password = req.getParameter("pass");
            // set the values for parameters
            ps.setString(1,username);
            ps.setString(2,password);
            ResultSet rs = ps.executeQuery();
            if (rs.next())
            {
                HttpSession session = req.getSession(true);
                                session.setAttribute("Welcome",username);
                res.sendRedirect("index.html");  // Wel is
               
            }
            else
                            res.sendRedirect("newjsp1.jsp");
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}
       


       

Wednesday 2 September 2015

Jsp Login Example

copy paste the code below

newjsp.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>Login page</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <div>
            <form action="log" method="post">
                Name :<input type="text" name="name"><br>
                PASS :<input type="password" name="pass"><br>
                <input type="submit" name="submit" value="submit">
            </form>
        </div>
    </body>
</html>

NewServlet.java


// a pure processing servlet to authenticate the user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import java.sql.*;

public class NewServlet extends HttpServlet
{
           
    public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
    {
        try
        {
            // get data of form
                       
            String username = req.getParameter("name");
            String password = req.getParameter("pass");
  
          
            if (username.equals("student") && password.equals("student"))
            {
              
                res.sendRedirect("index.html");                 
            }
            else
                            res.sendRedirect("newjsp1.jsp");
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}