Forum Stats

  • 3,814,125 Users
  • 2,258,821 Discussions
  • 7,892,571 Comments

Discussions

can anybody explain me folloing code...for my acdmic proj(authenticated mearest neighbor search)

995374
995374 Member Posts: 17
edited Sep 27, 2013 4:47AM in SQL & PL/SQL

package com.multistep.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import com.multistep.db.util.ConnectionFactory;

import com.multistep.form.LocationForm;

public class LocationDao {

    Connection con=null;

public LocationDao()

{

     con=ConnectionFactory.getConnection();

}

    /*public Map<Integer,String> getDistance(String srcloc,String destloc,int distance)

    {

        int x=0;

        int y1=0;

        int y=0;

        Map<Integer,String> list1=new HashMap<Integer,String>();

        if(distance==10)

        {

       

   

       

       

        Map<Integer,String> list=new HashMap<Integer, String>();

       

        String sql="select distance from loc where location='"+srcloc+"'";

        String sql1="select location,distance from loc";

       

        try {

            PreparedStatement ps=con.prepareStatement(sql);

            ResultSet rs=ps.executeQuery();

            while(rs.next()){

            x=rs.getInt(1);

            System.out.println(x+"x");

            }

            PreparedStatement ps1=con.prepareStatement(sql1);

            ResultSet rs1=ps1.executeQuery();

            while(rs1.next())

            {

            destloc=rs1.getString(1);

            y=rs1.getInt(2);

            System.out.println(y+"y");

    list.put(y, destloc);

            }

            System.out.println(list.size());

            String value=String.valueOf(x);

            System.out.println(value);

            String value1=String.valueOf(y);

            System.out.println(value1);

            System.out.println();

            Iterator iterator=list.entrySet().iterator();

            while(iterator.hasNext())

                //Map inner class -Entry

            {

                Entry thisEntry = (Entry) iterator.next();

            Integer i=    (Integer)thisEntry.getKey();

         y1=i.intValue();

         System.out.println(y1);

         String string=(String)thisEntry.getValue();

         System.out.println(string+"String");

         if(x>=y1)

            {

                int x1=x-y1;

                if(x1<=10)

                {

                System.out.println(x1+"xxxxxxxxx");

                list1.put(x1,string);

                }

           

            }

           

            }

            System.out.println(list1.size()+"list1.size()");

       

       

           

           

           

           

           

           

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        }

        if(distance==20)

        {

       

   

       

       

       

        Map<Integer,String> list=new HashMap<Integer,String>();

       

        String sql="select distance from loc where location='"+srcloc+"'";

        String sql1="select location,distance from loc";

       

        try {

            PreparedStatement ps=con.prepareStatement(sql);

            ResultSet rs=ps.executeQuery();

            while(rs.next()){

            x=rs.getInt(1);

            System.out.println(x+"x");

            }

            PreparedStatement ps1=con.prepareStatement(sql1);

            ResultSet rs1=ps1.executeQuery();

            while(rs1.next())

            {

               

                destloc=rs1.getString(1);

                System.out.println(destloc+"destloc");

            y=rs1.getInt(2);

            System.out.println(y+"y");

            list.put(y, destloc);

            }

            System.out.println(list.size());

            String value=String.valueOf(x);

            System.out.println(value);

            String value1=String.valueOf(y);

            System.out.println(value1);

            System.out.println();

            Iterator iterator=list.entrySet().iterator();

            while(iterator.hasNext())

            {

                Entry entry=(Entry)iterator.next();

            Integer i=    (Integer)entry.getKey();

         y1=i.intValue();

         System.out.println(y1);

         String string=(String)entry.getValue();

         if(x>=y1)

            {

                int x1=x-y1;

                if(x1>10&&x1<=20)

                {

                System.out.println(x1+"xxxxxxxxx");

                list1.put(x1,string);

                }

           

            }

           

            }

            System.out.println(list1.size()+"list1.size()");

       

       

           

           

           

           

           

           

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        }

        if(distance==30)

        {

       

   

       

       

        Map<Integer,String> list=new HashMap<Integer, String>();

       

        String sql="select distance from loc where location='"+srcloc+"'";

        String sql1="select location,distance from loc";

       

        try {

            PreparedStatement ps=con.prepareStatement(sql);

            ResultSet rs=ps.executeQuery();

            while(rs.next()){

            x=rs.getInt(1);

            System.out.println(x+"x");

            }

            PreparedStatement ps1=con.prepareStatement(sql1);

            ResultSet rs1=ps1.executeQuery();

            while(rs1.next())

            {

            destloc=rs1.getString(1);

            System.out.println(destloc+"destloc");

            y=rs1.getInt(2);

            System.out.println(y+"y");

            list.put(y,destloc);

            }

            System.out.println(list.size());

            String value=String.valueOf(x);

            System.out.println(value);

            String value1=String.valueOf(y);

            System.out.println(value1);

            System.out.println();

            Iterator iterator=list.entrySet().iterator();

            while(iterator.hasNext())

            {

                Entry entry=(Entry)iterator.next();

            Integer i=    (Integer)entry.getKey();

         y1=i.intValue();

         System.out.println(y1);

         String string=(String)entry.getValue();

         if(x>=y1)

            {

                int x1=x-y1;

                if(x1>20&&x1<=30)

                {

                System.out.println(x1+"xxxxxxxxx");

                list1.put(x1,string);

                }

           

            }

           

            }

            System.out.println(list1.size()+"list1.size()");

       

       

           

           

           

           

           

           

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        }

        return list1;

    }

*/

public int deleteLocation(String hit)

{

    int x=0;

    Connection con=ConnectionFactory.getConnection();//nvl(max(userid),1000)+1

    PreparedStatement ps;

    try {

        ps = con.prepareStatement("delete from fake where hit='"+hit+"'");

     x=ps.executeUpdate();

    if(x>0)

    {

    System.out.println("fake object is deleted successfully");

    }

    else

    {

    System.out.println("fake object is failed");

    }

    }

    catch (SQLException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

    return x;

    }

public int deletefake(String distance)

{

    int x=0;

    Connection con=ConnectionFactory.getConnection();

    PreparedStatement ps;

    try {

        ps = con.prepareStatement("delete from fake where distance='"+distance+"'");

     x=ps.executeUpdate();

    if(x>0)

    {

    System.out.println("fake object is deleted successfully");

    }

    else

    {

    System.out.println("fake object is failed");

    }

    }

    catch (SQLException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

    return x;

    }

   

public int addFake(LocationForm locationForm,boolean y)

{int x=0;

Connection con=ConnectionFactory.getConnection();//nvl(max(userid),1000)+1

PreparedStatement ps;

try {

    ps = con.prepareStatement("insert into fake values((select nvl(max(id),1000) from fake),?,?,?,?,'"+y+"')");

ps.setString(1,locationForm.getLocation());

ps.setString(2,locationForm.getService());

ps.setString(3,locationForm.getDistance());

ps.setString(4,locationForm.getAddress());

x=ps.executeUpdate();

if(x>0)

{

System.out.println("fake object is inserted successfully");

}

else

{

System.out.println("fake object is failed");

}

}

catch (SQLException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

return x;

}

public List<LocationForm> searchHit()

{List<LocationForm>  list=new ArrayList<LocationForm>();

try

{

   

    LocationForm locationForm=null;

String sql="select distinct d.LOCATION,d.SERVICE ,d.DISTANCE,d.ADDRESS,f.HIT,d.ano from fake f,dataset d where d.location=f.location";

//String sql1="select * from dataset1 where location=? and distance=?";

PreparedStatement pstmt=con.prepareStatement(sql);

//PreparedStatement pstmt1=con.prepareStatement(sql1);

ResultSet rs=pstmt.executeQuery();

while(rs.next()){

locationForm=new LocationForm();

String location=rs.getString(1);

locationForm.setLocation(location);

String service=rs.getString(2);

locationForm.setService(service);

String distance=rs.getString(3);

locationForm.setDistance(distance);

String address=rs.getString(4);

locationForm.setAddress(address);

String hit=rs.getString(5);

locationForm.setHit(hit);

String ano=rs.getString(6);

locationForm.setAno(ano);

list.add(locationForm);

}

}

catch (Exception e) {

e.printStackTrace();

}

return list;

}

public List<LocationForm> searchData()

{List<LocationForm>  list=new ArrayList<LocationForm>();

try

{

   

    LocationForm locationForm=null;

String sql="select distinct LOCATION,SERVICE ,DISTANCE,ADDRESS,HIT from fake where hit='true'";

//String sql1="select * from dataset1 where location=? and distance=?";

PreparedStatement pstmt=con.prepareStatement(sql);

//PreparedStatement pstmt1=con.prepareStatement(sql1);

ResultSet rs=pstmt.executeQuery();

while(rs.next()){

locationForm=new LocationForm();

String location=rs.getString(1);

locationForm.setLocation(location);

String service=rs.getString(2);

locationForm.setService(service);

String distance=rs.getString(3);

locationForm.setDistance(distance);

String address=rs.getString(4);

locationForm.setAddress(address);

String hit=rs.getString(5);

locationForm.setHit(hit);

list.add(locationForm);

}

}

catch (Exception e) {

e.printStackTrace();

}

return list;

}

public List<LocationForm> searchData1(String dis)

{List<LocationForm>  list=new ArrayList<LocationForm>();

try

{

   

    LocationForm locationForm=null;

String sql="select distinct LOCATION,SERVICE ,DISTANCE,ADDRESS,HIT from fake where hit='true' and distance='"+dis+"'";

//String sql1="select * from dataset1 where location=? and distance=?";

PreparedStatement pstmt=con.prepareStatement(sql);

//PreparedStatement pstmt1=con.prepareStatement(sql1);

ResultSet rs=pstmt.executeQuery();

while(rs.next()){

locationForm=new LocationForm();

String location=rs.getString(1);

locationForm.setLocation(location);

String service=rs.getString(2);

locationForm.setService(service);

String distance=rs.getString(3);

locationForm.setDistance(distance);

String address=rs.getString(4);

locationForm.setAddress(address);

String hit=rs.getString(5);

locationForm.setHit(hit);

list.add(locationForm);

}

}

catch (Exception e) {

e.printStackTrace();

}

return list;

}

public List<LocationForm> searchDataLoc(String dis)

{List<LocationForm>  list=new ArrayList<LocationForm>();

try

{

   

    LocationForm locationForm=null;

String sql="select LOCATION,SERVICE ,DISTANCE,ADDRESS,HIT from fake where hit='true' and distance='"+dis+"'";

//String sql1="select * from dataset1 where location=? and distance=?";

PreparedStatement pstmt=con.prepareStatement(sql);

//PreparedStatement pstmt1=con.prepareStatement(sql1);

ResultSet rs=pstmt.executeQuery();

while(rs.next()){

locationForm=new LocationForm();

String location=rs.getString(1);

locationForm.setLocation(location);

String service=rs.getString(2);

locationForm.setService(service);

String distance=rs.getString(3);

locationForm.setDistance(distance);

String address=rs.getString(4);

locationForm.setAddress(address);

String hit=rs.getString(5);

locationForm.setHit(hit);

list.add(locationForm);

}

}

catch (Exception e) {

e.printStackTrace();

}

return list;

}

public List<LocationForm> searchData2(String dis)

{List<LocationForm>  list=new ArrayList<LocationForm>();

try

{

   

    LocationForm locationForm=null;

String sql="select LOCATION,SERVICE ,DISTANCE,ADDRESS from dataset where distance='"+dis+"'";

//String sql1="select * from dataset1 where location=? and distance=?";

PreparedStatement pstmt=con.prepareStatement(sql);

//PreparedStatement pstmt1=con.prepareStatement(sql1);

ResultSet rs=pstmt.executeQuery();

while(rs.next()){

locationForm=new LocationForm();

String location=rs.getString(1);

locationForm.setLocation(location);

String service=rs.getString(2);

locationForm.setService(service);

String distance=rs.getString(3);

locationForm.setDistance(distance);

String address=rs.getString(4);

locationForm.setAddress(address);

list.add(locationForm);

}

}

catch (Exception e) {

e.printStackTrace();

}

return list;

}

public List<LocationForm> searchData3()

{List<LocationForm>  list=new ArrayList<LocationForm>();

try

{

   

    LocationForm locationForm=null;

String sql="select LOCATION,SERVICE ,DISTANCE,ADDRESS from dataset";

//String sql1="select * from dataset1 where location=? and distance=?";

PreparedStatement pstmt=con.prepareStatement(sql);

//PreparedStatement pstmt1=con.prepareStatement(sql1);

ResultSet rs=pstmt.executeQuery();

while(rs.next()){

locationForm=new LocationForm();

String location=rs.getString(1);

locationForm.setLocation(location);

String service=rs.getString(2);

locationForm.setService(service);

String distance=rs.getString(3);

locationForm.setDistance(distance);

String address=rs.getString(4);

locationForm.setAddress(address);

list.add(locationForm);

}

}

catch (Exception e) {

e.printStackTrace();

}

return list;

}

public List<LocationForm> getDistance(String distance)

{

    List<LocationForm>  list=new ArrayList<LocationForm>();

    try

    {

       

        LocationForm locationForm=null;

   

    String sql="select * from dataset where distance='"+distance+"'";

    //String sql1="select * from dataset1 where location=? and distance=?";

   

    PreparedStatement pstmt=con.prepareStatement(sql);

    //PreparedStatement pstmt1=con.prepareStatement(sql1);

   

    ResultSet rs=pstmt.executeQuery();

while(rs.next()){

    locationForm=new LocationForm();

    int x=rs.getInt(1);

    System.out.println(x+"x");

    String location=rs.getString(2);

    locationForm.setLocation(location);

String service=rs.getString(3);

locationForm.setService(service);

distance=rs.getString(4);

locationForm.setDistance(distance);

String address=rs.getString(5);

locationForm.setAddress(address);

list.add(locationForm);

}

}

   

catch (Exception e) {

e.printStackTrace();

}

return list;

   

   

   

}

public List<LocationForm> getDistance1(String distance)

{

    List<LocationForm>  list=new ArrayList<LocationForm>();

    try

    {

       

        LocationForm locationForm=null;

   

    String sql="select * from dataset1 where distance='"+distance+"'";

    //String sql1="select * from dataset1 where location=? and distance=?";

   

    PreparedStatement pstmt=con.prepareStatement(sql);

    //PreparedStatement pstmt1=con.prepareStatement(sql1);

   

    ResultSet rs=pstmt.executeQuery();

while(rs.next()){

    locationForm=new LocationForm();

    int x=rs.getInt(1);

    System.out.println(x+"x");

    String location=rs.getString(2);

    locationForm.setLocation(location);

String service=rs.getString(3);

locationForm.setService(service);

distance=rs.getString(4);

locationForm.setDistance(distance);

String address=rs.getString(5);

locationForm.setAddress(address);

list.add(locationForm);

}

}

   

catch (Exception e) {

e.printStackTrace();

}

return list;

   

   

   

}

public int updateDistance(String auth,String location)

    {

        int x=0;

        try

        {

            String sql="update loc set distance="+auth+" where location="+location+"";

            PreparedStatement preparedStatement=con.prepareStatement(sql);

            x=preparedStatement.executeUpdate();

            if(x>0)

            {

                System.out.println("Authentication is provided");

            }

            else

            {

                System.out.println("Authentication is failed");

            }

        }

        catch (Exception e) {

            // TODO: handle exception

        }

        return x;

    }

/*    public ArrayList getDistance1(String srcloc,String destloc)

    {

        int x=0;

        int y1=0;

        int y=0;

        ArrayList list1=new ArrayList();

        ArrayList list2=new ArrayList();

   

       

       

        ArrayList list=new ArrayList();

       

        String sql="select distance from loc where location='"+srcloc+"'";

        String sql1="select distance from loc";

       

        try {

            PreparedStatement ps=con.prepareStatement(sql);

            ResultSet rs=ps.executeQuery();

            while(rs.next()){

            x=rs.getInt(1);

            System.out.println(x+"x");

            }

            PreparedStatement ps1=con.prepareStatement(sql1);

            ResultSet rs1=ps1.executeQuery();

            while(rs1.next())

            {

           

            y=rs1.getInt(1);

            System.out.println(y+"y");

            list.add(y);

            }

            System.out.println(list.size());

            String value=String.valueOf(x);

            System.out.println(value);

            String value1=String.valueOf(y);

            System.out.println(value1);

            System.out.println();

            Iterator iterator=list.iterator();

            while(iterator.hasNext())

            {

            Integer i=    (Integer)iterator.next();

         y1=i.intValue();

         System.out.println(y1);

         if(x<=y1)

            {

                int x1=y1-x;

                System.out.println(x1+"xxxxxxxxx");

                list1.add(x1);

           

            }

           

            }

            System.out.println(list1.size()+"list1.size()");

       

            list2.add(list1);

       

            System.out.println(list2.size()+"list.size()");

           

           

           

           

           

           

        } catch (SQLException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return list2;

    }*/

    public int addLocation(LocationForm  form,String ano)

    {

        int x=0;

        PreparedStatement preparedStatement=null;

        try

        {

            String sql="insert into dataset values((select nvl(max(id),1000)+1 from dataset),?,?,?,?,?)";

        preparedStatement=con.prepareStatement(sql);

            preparedStatement.setString(1,form.getLocation());

            preparedStatement.setString(2,form.getService());

            preparedStatement.setString(3,form.getDistance());

            preparedStatement.setString(4, form.getAddress());

            preparedStatement.setString(5, ano);

            x=preparedStatement.executeUpdate();

            if(x>0)

            {

                System.out.println("location is inserted successfully");

            }

            else

            {

                System.out.println("location is not inserted");

               

            }

           

        }

        catch (Exception e) {

            e.printStackTrace();

        }

        finally

        {

            try

            {

                con.commit();

                preparedStatement.close();

               

               

            }

            catch (Exception e) {

                e.printStackTrace();

            }

        }

        return x;

       

    }

    public int addLocation1(LocationForm  form,String ano)

    {

        int x=0;

        PreparedStatement preparedStatement=null;

        try

        {

            String sql="insert into dataset1 values((select nvl(max(id),1000)+1 from dataset1),?,?,?,?,?)";

        preparedStatement=con.prepareStatement(sql);

            preparedStatement.setString(1,form.getLocation());

            preparedStatement.setString(2,form.getService());

            preparedStatement.setString(3,form.getDistance());

            preparedStatement.setString(4, form.getAddress());

            preparedStatement.setString(5, ano);

            x=preparedStatement.executeUpdate();

            if(x>0)

            {

                System.out.println("location is inserted successfully");

            }

            else

            {

                System.out.println("location is not inserted");

               

            }

           

        }

        catch (Exception e) {

            e.printStackTrace();

        }

        finally

        {

            try

            {

                con.commit();

                preparedStatement.close();

               

               

            }

            catch (Exception e) {

                e.printStackTrace();

            }

        }

        return x;

       

    }

   

    public List<LocationForm> getLocation()

    {

        LocationForm locationForm=null;

        List<LocationForm> list=new ArrayList<LocationForm>();

        String sql="select * from dataset";

        try{

        PreparedStatement ps;

       

            ps = con.prepareStatement(sql);

   

            // TODO Auto-generated catch block

       

       

        ResultSet rs;

       

            rs = ps.executeQuery();

   

        while(rs.next()){

            locationForm=new LocationForm();

        int x=rs.getInt(1);

        System.out.println(x+"x");

        String location=rs.getString(2);

        locationForm.setLocation(location);

        String service=rs.getString(3);

        locationForm.setService(service);

        String distance=rs.getString(4);

        locationForm.setDistance(distance);

        String address=rs.getString(5);

        locationForm.setAddress(address);

        list.add(locationForm);

        System.out.println(list.size());

       

        }

       

       

    }

        catch (Exception e) {

        e.printStackTrace();

       

        }

   

        return list;

    }

    public List<LocationForm> getLocation1()

    {

        LocationForm locationForm=null;

        List<LocationForm> list=new ArrayList<LocationForm>();

        String sql="select * from dataset1";

        try{

        PreparedStatement ps;

       

            ps = con.prepareStatement(sql);

   

            // TODO Auto-generated catch block

       

       

        ResultSet rs;

       

            rs = ps.executeQuery();

   

        while(rs.next()){

            locationForm=new LocationForm();

        int x=rs.getInt(1);

        System.out.println(x+"x");

        String location=rs.getString(2);

        locationForm.setLocation(location);

        String service=rs.getString(3);

        locationForm.setService(service);

        String distance=rs.getString(4);

        locationForm.setDistance(distance);

        String address=rs.getString(5);

        locationForm.setAddress(address);

        list.add(locationForm);

       

        }

   

    }

        catch (Exception e) {

        e.printStackTrace();

        }

        return list;

    }

   

}

Tagged:

Answers

  • BluShadow
    BluShadow Member, Moderator Posts: 41,857 Red Diamond
    edited Sep 27, 2013 4:48AM

    This is the SQL and PL/SQL forum.  that code is Java.

    Please ask your question in an appropriate Java forum.

    Locking this thread.

    Also ensure you read the forum FAQ for a guide on how to ask questions:

This discussion has been closed.