• Post Reply Bookmark Topic Watch Topic
  • New Topic

getString method

 
ankit walia
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys!!!

i have just started using servlets in java. My problem is i have first created a class BookServlet.java in which i have created multiple checkboxes and there is another class getServlet in which if multiple checkboxes are selected then i have retrieved values using getParameterValues method and stored in string array.

now further i have to retrieve different books from database according to categories selected using checkboxes.But in the second class getServlet.java it is giving a compatibility error with getString method. Source codes of both classes are posted under.
If this wrong can anybody suggest alternative method please. its really urgent!!!

getServlet.java

import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;

public class BookServlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
PrintWriter p2=res.getWriter();
String ss[]=req.getParameterValues("c1");//c1 here is the checkbox name

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbcdbc:ankit");
PreparedStatement stmt=con.prepareStatement("Select * from Books where type=?");

for(int i=0;i<ss.length;i++)
{
stmt.setString(1,ss[i]);
ResultSet rs=stmt.executeQuery();
}

while(rs.next())
{
String ss1[]=rs.getString("ss[]");
for(int j=0;j<ss1.length;j++)
{
p2.println("Selected Books are" +ss1[j]);
}
}
con.close();
}
catch(Exception e)
{
p2.println(e);
}
}
}


BookServlet.java:


import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;

public class BookServlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
PrintWriter p2=res.getWriter();

p2.println("<html>");
p2.println("<body bgcolor=\"lightblue\">");
p2.println("<h1>Select Books</h1>");
p2.println("<br>");
p2.println("<input type=checkbox value=JAVA name=c1><b>JAVA");
p2.println("<input type=checkbox value=C name=c1><b>C");
p2.println("<input type=checkbox value=C++ name=c1><b>C++");
p2.println("<br>");
p2.println("<br>");
p2.println("<input type=submit value=\"OK\">");
}
}
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

please put your code between code tags.


In a servlet you do not override the service method !!

In your case override either doGet or doPost.

Further on, your input fields are not enclosed in a form tag, which means your form would not be sent anywhere.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankit walia wrote:its really urgent!!!


Please read this.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ankit walia wrote:it is giving a compatibility error with getString method.

This is meaningless. What is the specific error that you are getting? Please read this for more information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!