[Tex/LaTex] Listing code going out of page

listings

I'm using listings to display my codes. I'm getting some issues. First, the codes when it is too long, it is going out of page as shown here. Secondly, the codes are in SPARQL. It is recognising some of the variables I use as SQL keywords and highlighted them as shown here where Class is inspite being a variable is being highlighted as a keyword. My codes are as follows:

\begin{lstlisting}[captionpos=b, caption=Retrieval of Individuals, label=listing:sparql_getallindividuals,
   basicstyle=\ttfamily]
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  wo:   <http://purl.org/ontology/wo/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT  ?individual ?type ?name
  WHERE
    { ?individual rdf:type ?type .
      ?individual wo:commonName ?name
      FILTER ( ?type IN (wo:Kingdom, wo:Phylum, wo:Class, wo:Order, wo:Species, wo:Family, wo:Genus) )
    }
\end{lstlisting}

Any help would be greatly appreciated

This is preamble of my report:

\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

 \lstset{frame=tb,
  language=Java,
  showstringspaces=false,
  columns=flexible,
  numbers=none,
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  tabsize=3
}
\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type,ma-id}% list your attributes here
}

Best Answer

For the line breaking problem, you can use the breaklines=true option:

\documentclass{book}
\usepackage{listings}
\usepackage{color}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

 \lstset{frame=tb,
  language=Java,
  breaklines=true,
  showstringspaces=false,
  columns=flexible,
  numbers=none,
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  tabsize=3
}
\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type,ma-id}% list your attributes here
}

\begin{document}

\begin{lstlisting}[captionpos=b, caption=Retrieval of Individuals, label=listing:sparql_getallindividuals,
   basicstyle=\ttfamily]
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  wo:   <http://purl.org/ontology/wo/>
PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT  ?individual ?type ?name
  WHERE
    { ?individual rdf:type ?type .
      ?individual wo:commonName ?name
      FILTER ( ?type IN (wo:Kingdom, wo:Phylum, wo:Class, wo:Order, wo:Species, wo:Family, wo:Genus) )
    }
\end{lstlisting}

\end{document}

enter image description here

For the other issue, you haven't provided enough information to detect the problem.