I'm new to programming. I have created a servlet which retrieves values from the database after a value is put into the HTML page to which it is attached. But during compilation, it shows two errors. Please help me in solving this error.
import java.io.*;
import java.util.Enume ration;
import javax.servlet.* ;
import javax.servlet.h ttp.*;
import java.sql.*;
import java.net.*;
public class search extends HttpServlet
{
Connection con;
private ServletConfig config;
public void init(ServletCon fig config) throws ServletExceptio n
{
this.config=con fig;
}
public void service (HttpServletReq uest req, HttpServletResp onse res)
throws ServletExceptio n, IOException
{
HttpSession session = req.getSession( true);
res.setContentT ype("text/html");
PrintWriter out = res.getWriter() ;
out.println("<H TML><HEAD><TITL E>Search Results</TITLE>");
out.println("</HEAD>");
out.println("<B ODY bgColor=blanche dalmond text=#008000 topMargin=0>");
out.println("<P align=center><F ONT face=Helvetica> <FONT color=fuchsia style=\"BACKGRO UND-COLOR: white\"><BIG><B IG>List of Songs</BIG></BIG></FONT></P>");
out.println("<P align=center>") ;
out.println("<T ABLE align=center border=1 cellPadding=1 cellSpacing=1 width=\"75%\">" );
out.println("<T R>");
out.println("<T D>Category</TD>");
out.println("<T D>Artist</TD>");
out.println("<T D>Song</TD>");
out.println("<T D>Movie/Album</TD>");
out.println("<T D>Link</TD></TR>");
try
{
Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
con = DriverManager.g etConnection("j dbc:odbc:MyData Source", "sa", "");
Statement stat=con.create Statement();
String category=req.ge tParameter("cat egory");
String artist=req.getP arameter("artis t");
String song=req.getPar ameter("song");
String movie=req.getPa rameter("movie" );
if(artist.lengt h() == 0 && category == "select a category" && song.length() == 0 && movie.length() !== 0)
{
ResultSet result = stat.executeQue ry("select * from music where c_movie = '" + movie + "'");
}
else if(artist.lengt h() !== 0 && song.length() == 0 && category == "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "' and c_movie = '" + movie + "'");
}
else if(song.length( ) !== 0 && artist.length() !== 0 && category == "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_movie = '" + movie + "' and c_artist = '" + artist + "' and c_song = '" + song + "'");
}
else if(song.length( ) == 0 && artist.length() == 0 && category !== "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_category = '" + category + "'");
}
else if(song.length( ) !== 0 && artist.length() == 0 && category !== "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_song = '" + song + "' and c_category = '" + category + "'");
}
else if(song.length( ) !== 0 && artist.length() !== 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_song = '" + song + "' and c_artist = '" + artist + "'");
}
else if(song.length( ) == 0 && artist.length() !== 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "'");
}
else if(song.length( ) !== 0 && artist.length() == 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_song = '" + song + "'");
}
else if(song.length( ) !== 0 && artist.length() == 0 && category !== "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_song = '" + song + "' and c_category = '" + category + "' and c_movie = '" + movie + "'");
}
else if(song.length( ) == 0 && artist.length() !== 0 && category !== "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "' and c_category = '" + category + "' and c_movie = '" + movie + "'");
}
else if(song.length( ) !== 0 && artist.length() !== 0 && category !== "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "' and c_category = '" + category + "' and c_song = '" + song + "'");
}
else if(song.length( ) !== 0 && artist.length() !== 0 && category !== "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "' and c_category = '" + category + "' and c_movie = '" + movie + "' and c_song = '" + song + "'");
}
else(song.lengt h() == 0 && artist.length() == 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music");
}
while(result.ne xt())
{
out.println();
out.println("<T R>");
out.println("<T D>" + result.getStrin g(1) + "</TD>");
out.println("<T D>" + result.getStrin g(2) + "</TD>");
out.println("<T D>" + result.getStrin g(3) + "</TD>");
out.println("<T D>" + result.getStrin g(4) + "</TD>");
out.println("<T D><a href = 'http://www.abczxyz.com '>Download</a></TD>");
out.println("</TR>");
}
result.close();
stat.close();
con.close();
}
catch(Exception e)
{
out.println(e.g etMessage());
}
out.println("</TABLE></P>");
out.println("<P > </P></FONT></BODY></HTML>");
}
}
The error is:
search.java:64: illegal start of expression
if(artist.lengt h() == 0 && category == "select a category" && song.length() =
= 0 && movie.length() !== 0)
^
search.java:150 : ')' expected
result.close();
^
import java.io.*;
import java.util.Enume ration;
import javax.servlet.* ;
import javax.servlet.h ttp.*;
import java.sql.*;
import java.net.*;
public class search extends HttpServlet
{
Connection con;
private ServletConfig config;
public void init(ServletCon fig config) throws ServletExceptio n
{
this.config=con fig;
}
public void service (HttpServletReq uest req, HttpServletResp onse res)
throws ServletExceptio n, IOException
{
HttpSession session = req.getSession( true);
res.setContentT ype("text/html");
PrintWriter out = res.getWriter() ;
out.println("<H TML><HEAD><TITL E>Search Results</TITLE>");
out.println("</HEAD>");
out.println("<B ODY bgColor=blanche dalmond text=#008000 topMargin=0>");
out.println("<P align=center><F ONT face=Helvetica> <FONT color=fuchsia style=\"BACKGRO UND-COLOR: white\"><BIG><B IG>List of Songs</BIG></BIG></FONT></P>");
out.println("<P align=center>") ;
out.println("<T ABLE align=center border=1 cellPadding=1 cellSpacing=1 width=\"75%\">" );
out.println("<T R>");
out.println("<T D>Category</TD>");
out.println("<T D>Artist</TD>");
out.println("<T D>Song</TD>");
out.println("<T D>Movie/Album</TD>");
out.println("<T D>Link</TD></TR>");
try
{
Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
con = DriverManager.g etConnection("j dbc:odbc:MyData Source", "sa", "");
Statement stat=con.create Statement();
String category=req.ge tParameter("cat egory");
String artist=req.getP arameter("artis t");
String song=req.getPar ameter("song");
String movie=req.getPa rameter("movie" );
if(artist.lengt h() == 0 && category == "select a category" && song.length() == 0 && movie.length() !== 0)
{
ResultSet result = stat.executeQue ry("select * from music where c_movie = '" + movie + "'");
}
else if(artist.lengt h() !== 0 && song.length() == 0 && category == "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "' and c_movie = '" + movie + "'");
}
else if(song.length( ) !== 0 && artist.length() !== 0 && category == "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_movie = '" + movie + "' and c_artist = '" + artist + "' and c_song = '" + song + "'");
}
else if(song.length( ) == 0 && artist.length() == 0 && category !== "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_category = '" + category + "'");
}
else if(song.length( ) !== 0 && artist.length() == 0 && category !== "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_song = '" + song + "' and c_category = '" + category + "'");
}
else if(song.length( ) !== 0 && artist.length() !== 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_song = '" + song + "' and c_artist = '" + artist + "'");
}
else if(song.length( ) == 0 && artist.length() !== 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "'");
}
else if(song.length( ) !== 0 && artist.length() == 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_song = '" + song + "'");
}
else if(song.length( ) !== 0 && artist.length() == 0 && category !== "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_song = '" + song + "' and c_category = '" + category + "' and c_movie = '" + movie + "'");
}
else if(song.length( ) == 0 && artist.length() !== 0 && category !== "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "' and c_category = '" + category + "' and c_movie = '" + movie + "'");
}
else if(song.length( ) !== 0 && artist.length() !== 0 && category !== "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "' and c_category = '" + category + "' and c_song = '" + song + "'");
}
else if(song.length( ) !== 0 && artist.length() !== 0 && category !== "select a category" && movie.length() !== 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music where c_artist = '" + artist + "' and c_category = '" + category + "' and c_movie = '" + movie + "' and c_song = '" + song + "'");
}
else(song.lengt h() == 0 && artist.length() == 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.exe cuteQuery("sele ct * from music");
}
while(result.ne xt())
{
out.println();
out.println("<T R>");
out.println("<T D>" + result.getStrin g(1) + "</TD>");
out.println("<T D>" + result.getStrin g(2) + "</TD>");
out.println("<T D>" + result.getStrin g(3) + "</TD>");
out.println("<T D>" + result.getStrin g(4) + "</TD>");
out.println("<T D><a href = 'http://www.abczxyz.com '>Download</a></TD>");
out.println("</TR>");
}
result.close();
stat.close();
con.close();
}
catch(Exception e)
{
out.println(e.g etMessage());
}
out.println("</TABLE></P>");
out.println("<P > </P></FONT></BODY></HTML>");
}
}
The error is:
search.java:64: illegal start of expression
if(artist.lengt h() == 0 && category == "select a category" && song.length() =
= 0 && movie.length() !== 0)
^
search.java:150 : ')' expected
result.close();
^
Comment