By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Getting one illegal start of expression . Please help!!

P: 1
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.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.*;
public class search extends HttpServlet
{
Connection con;
private ServletConfig config;


public void init(ServletConfig config) throws ServletException
{
this.config=config;
}

public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{

HttpSession session = req.getSession(true);

res.setContentType("text/html");

PrintWriter out = res.getWriter();

out.println("<HTML><HEAD><TITLE>Search Results</TITLE>");

out.println("</HEAD>");

out.println("<BODY bgColor=blanchedalmond text=#008000 topMargin=0>");

out.println("<P align=center><FONT face=Helvetica><FONT color=fuchsia style=\"BACKGROUND-COLOR: white\"><BIG><BIG>List of Songs</BIG></BIG></FONT></P>");

out.println("<P align=center>");

out.println("<TABLE align=center border=1 cellPadding=1 cellSpacing=1 width=\"75%\">");

out.println("<TR>");

out.println("<TD>Category</TD>");

out.println("<TD>Artist</TD>");

out.println("<TD>Song</TD>");

out.println("<TD>Movie/Album</TD>");

out.println("<TD>Link</TD></TR>");

try
{


Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:MyDataSourc e", "sa", "");
Statement stat=con.createStatement();
String category=req.getParameter("category");
String artist=req.getParameter("artist");
String song=req.getParameter("song");
String movie=req.getParameter("movie");

if(artist.length() == 0 && category == "select a category" && song.length() == 0 && movie.length() !== 0)
{
ResultSet result = stat.executeQuery("select * from music where c_movie = '" + movie + "'");
}

else if(artist.length() !== 0 && song.length() == 0 && category == "select a category" && movie.length() !== 0)
{
ResultSet result=stat.executeQuery("select * 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.executeQuery("select * 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.executeQuery("select * from music where c_category = '" + category + "'");
}

else if(song.length() !== 0 && artist.length() == 0 && category !== "select a category" && movie.length() == 0)
{
ResultSet result=stat.executeQuery("select * 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.executeQuery("select * 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.executeQuery("select * from music where c_artist = '" + artist + "'");
}

else if(song.length() !== 0 && artist.length() == 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.executeQuery("select * from music where c_song = '" + song + "'");
}

else if(song.length() !== 0 && artist.length() == 0 && category !== "select a category" && movie.length() !== 0)
{
ResultSet result=stat.executeQuery("select * 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.executeQuery("select * 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.executeQuery("select * 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.executeQuery("select * from music where c_artist = '" + artist + "' and c_category = '" + category + "' and c_movie = '" + movie + "' and c_song = '" + song + "'");
}

else(song.length() == 0 && artist.length() == 0 && category == "select a category" && movie.length() == 0)
{
ResultSet result=stat.executeQuery("select * from music");
}



while(result.next())
{

out.println();

out.println("<TR>");

out.println("<TD>" + result.getString(1) + "</TD>");

out.println("<TD>" + result.getString(2) + "</TD>");

out.println("<TD>" + result.getString(3) + "</TD>");

out.println("<TD>" + result.getString(4) + "</TD>");

out.println("<TD><a href = 'http://www.abczxyz.com'>Download</a></TD>");

out.println("</TR>");
}
result.close();

stat.close();

con.close();

}
catch(Exception e)
{

out.println(e.getMessage());

}

out.println("</TABLE></P>");

out.println("<P>&nbsp;</P></FONT></BODY></HTML>");

}
}


The error is:
search.java:64: illegal start of expression
if(artist.length() == 0 && category == "select a category" && song.length() =
= 0 && movie.length() !== 0)

^
search.java:150: ')' expected
result.close();
^
Sep 12 '07 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
1.) Use code tags when posting code
2.) Indent your code. It makes it easier for people (including yourself) to read that code.
3.) When comparing strings, use the .equals method as in
Expand|Select|Wrap|Line Numbers
  1. if (name.equals("r035198x")) {
  2. //....
  3. }
  4.  
4.) Use brackets () to tell the compiler what you mean as in
don't say
Expand|Select|Wrap|Line Numbers
  1. if(true | false & true)
but say
Expand|Select|Wrap|Line Numbers
  1. if((true | false) & true)
Sep 12 '07 #2

Expert 10K+
P: 11,448
movie.length() !== 0)
!== is nog an operator hence the compiler diagnostic; there's a whole lot more
wrong with this code but first try to get it through the compiler.

kind regards,

Jos
Sep 12 '07 #3

10K+
P: 13,264
!== is nog an operator hence the compiler diagnostic; there's a whole lot more
wrong with this code but first try to get it through the compiler.

kind regards,

Jos
However you managed to spot that ...
Sep 12 '07 #4

Expert 10K+
P: 11,448
However you managed to spot that ...
I don't have an avatar so I have good eyesight ;-)

kind regards,

Jos
Sep 12 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.