aspose file tools*
The moose likes Servlets and the fly likes can anybody help me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "can anybody help me" Watch "can anybody help me" New topic
Author

can anybody help me

rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
Iam trying to view the changes that i made in my html by using servlet,as
import javax.swing.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class postparametersservlet extends HttpServlet
{
private Connection connection;
private JTable table;
String url="jdbc dbc:book";
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
String isbn[]={"1234","34234","45362","78960"};
String name[]={"java","c++","c","oracle"};
String data=request.getParameter("data");
Cookie c=new Cookie("java","isbn");
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
response.addCookie(c);
pw.println("<html><head><title>");
pw.println("cod cookies");
pw.println("</title></head><body>");
pw.println(data);
if(data=="java")
{
pw.println(isbn[2]);
}
pw.println("</body></html>");
pw.close();
}
}
and my html is,
<html>
<body>
<center>
<form name="Form1" method="get" action="http://localhost:8080/servlet/postparametersservlet">
select the Name of the book<select name="data">
<option value="java" selected>java
<option value="c++">C++
<option value="c">C
<option value="oracle">oracle
</select><br><br>
<input TYPE="SUBMIT" ></body>
</html>
Like if i select the book "java" it has to display the isbn number of the book.
Is there anything wrong in that?
please help me.
rajani
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Instead of using if(data=="java")
try it using if(data.equals("java"))
and please let me know if it works
[This message has been edited by Marcos Maia (edited July 18, 2001).]
rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
Thank u.
it's working.but,i've another question.
I want to create a database that stores information about customer choices.Like,my html page contains a choice of text books.Corresponding to the selection the user made, i want to insert the isbn number of the book in the database.
If i try to do that iam getting an error message saying that"no db".
please check this out.
import java.io.*;
import javax.swing.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class postparametersservlet extends HttpServlet
{
private Connection connection;
private JTable table;
String url="jdbc dbc:book";
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
String isbn[]={"1234","34234","45362","78960"};
String name[]={"java","c++","c","oracle"};
String data=request.getParameter("data");
Cookie c=new Cookie("java","isbn");
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
response.addCookie(c);
pw.println("<html><head><title>");
pw.println("cod cookies");
pw.println("</title></head><body>");
pw.println(data);
pw.println("</body></html>");
pw.close();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection=DriverManager.getConnection(url);
}
catch(ClassNotFoundException cnf)
{
System.out.println("no class");
}
catch(SQLException sqle)
{
System.out.println("no db");
}
try
{
Statement s=connection.createStatement();
String q;
if(data.equals("java"))
{
q="insert into choices(bookname, isbnnumber)values(data,isbn[0])";
connection.nativeSQL(q);
int result=s.executeUpdate(q);
if(result==1)
System.out.println("successfull");
else
System.out.println("unsuccessfull");
s.close();
}
}
catch(SQLException sqle)
{
System.out.println("failed access");
}
}
}
Kevin Wright
Ranch Hand

Joined: Jul 10, 2001
Posts: 38
I haven't perused your code with too much attention, but make sure you register your DB in the ODBC Administrator in Control Panel ( on Win 9x machines ). If that isn't the problem, I would go ahead and blame the Jdbc-Odbc bridge. It's not so hot, as I found out myself. Try using a 3rd party driver ( I used the one at www.infozoom.de ). Good luck.
Kevin
rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
Yes,
i did register my database in odbc administrator in control panel.
what should i do now?
thanks in advance
rajini
Rehan Malik
Ranch Hand

Joined: Jul 09, 2001
Posts: 76
I personally don't use the JDBC-ODBC drivers (a slight overhead) but instead just use type IV drivers (i.e - mm.mysql)
I'm not sure but should there be a username/password associated with accessing the database? Because I didn't see this in your implementation. Maybe it's just the databases that I use which require this in the connection and not the one that you use.

Rehan Malik
rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
i don't think there is a necessity for username and password to be associated with accssing a database
rajani
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
Hi, try to do your catch statements display a more useful message with something like:
catch(SQLException e)
{
e.printStackTrace();
}
and or
catch(SQLException e)
{
System.out.println(e.getMessage());

}
than you shoul be able to debug the errors better.
rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
I changed the database.now it's working.
But the problem is iam trying to insert into table choices.
if i type,
"insert into choices(bookname, isbnnumber)values('java','1234')"
it's inserting the string java and 1234.
now,
what i want to do is,
i want to insert data where data is a variable whose value is java.
what is wrong with this?
"insert into choices(bookname, isbnnumber)values(data,'1234')"
thanks in advance
rajani
rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
if i do
"insert into choices(bookname, isbnnumber)values("'"+data+"'",'1234')"
this is the output:
C:\JSDK2.0\examples>javac postparametersservlet.java
postparametersservlet.java:50: unclosed character literal
q="insert into choices(bookname, isbnnumber)values("'"+data+"'",'1234')";
^
postparametersservlet.java:50: unclosed character literal
q="insert into choices(bookname, isbnnumber)values("'"+data+"'",'1234')";
^
postparametersservlet.java:50: unclosed character literal
q="insert into choices(bookname, isbnnumber)values("'"+data+"'",'1234')";
^
postparametersservlet.java:50: unclosed string literal
q="insert into choices(bookname, isbnnumber)values("'"+data+"'",'1234')";
^
4 errors
rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
sorry, but now it is,
C:\JSDK2.0\examples>javac postparametersservlet.java
postparametersservlet.java:50: illegal character: \92
q="insert into choices(bookname, isbnnumber)values("\'"+data+"\'",'1234')";
^
postparametersservlet.java:50: unclosed character literal
q="insert into choices(bookname, isbnnumber)values("\'"+data+"\'",'1234')";
^
postparametersservlet.java:50: unclosed character literal
q="insert into choices(bookname, isbnnumber)values("\'"+data+"\'",'1234')";
^
postparametersservlet.java:50: unclosed character literal
q="insert into choices(bookname, isbnnumber)values("\'"+data+"\'",'1234')";
^
postparametersservlet.java:50: unclosed string literal
q="insert into choices(bookname, isbnnumber)values("\'"+data+"\'",'1234')";
^
5 errors
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
really sorry the correct way is:

"insert into choices(bookname,isbnnumber)values('"+data+"','1234')"

really sorry for wasting your time.


[This message has been edited by Marcos Maia (edited July 18, 2001).]
rajani adapa
Ranch Hand

Joined: Jan 24, 2001
Posts: 54
Thank u guys.
bye
rajini
 
Don't get me started about those stupid light bulbs.
 
subject: can anybody help me
 
Similar Threads
Help with this code
JDBC/servlet connection
servlet-jdbc - internal servlet error (error:500)
Doubt in reDirect and post
Help with this code