File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Html Input to oracle through servlet and JSP output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Html Input to oracle through servlet and JSP output " Watch "Html Input to oracle through servlet and JSP output " New topic
Author

Html Input to oracle through servlet and JSP output

jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Hi everyone,

The main aim is to use Html input text, which has to go into the oracle
Data base through the servlet and create a table with different columns corresponding
To the texts entered in the html and the output should be in the Jsp with the data inside the tables of the oracle database.

What I did , I created a servlet and through the servlet I can connect the oracle database and create the tables and can retrive the data on the console. But I want to connect through the Html to oracle through servlet and output in Jsp and I am using all this in eclipse and the server is tomcat.

Thanks
[ January 21, 2008: Message edited by: jumpa lahari ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

HTML can not connect directly with an Oracle database.
Why don't you want to go through a servlet?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
HTML can not connect directly with an Oracle database.
Why don't you want to go through a servlet?


Hi Ben,

Yes i am connecting through the servlet and getting connected to the database and getting the data but i want to connect the Html form input to the servlet to the oracle database.

I am unable to make a link between the html and the servlet.

The code below is the servlet code to connect the jsp as well as connectio n to the oracle database.



Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

<form name="input" action="/Database/finish" method="post">

Can you post your servlet-entry and your servlet-mapping from your web.xml file.
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
<form name="input" action="/Database/finish" method="post">

Can you post your servlet-entry and your servlet-mapping from your web.xml file.


Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

And your contextPath is "Database"?

If you're not sure, post the full path to your servlet's class file, starting with the drive letter.
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
And your contextPath is "Database"?

If you're not sure, post the full path to your servlet's class file, starting with the drive letter.


I dint get you can you please precise your question.

Thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

What is the full path to your servlet's class file on your machine?

Example:
c:\tomcat\webapps\MyApp\WEB-INF\classes\us\souther\MyServlet.class
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
What is the full path to your servlet's class file on your machine?

Example:
c:\tomcat\webapps\MyApp\WEB-INF\classes\us\souther\MyServlet.class


Hi,

As i am using the eclipse , There is no root with the application name in the tomcat.
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
What is the full path to your servlet's class file on your machine?

Example:
c:\tomcat\webapps\MyApp\WEB-INF\classes\us\souther\MyServlet.class


Hi,

As i am using the eclipse , There is no root with the application name in the tomcat.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Can you connect to your HTML page with a browser?
If so, what URL are you using to do so?
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
Can you connect to your HTML page with a browser?
If so, what URL are you using to do so?


Hi,

This is the http://localhost:8082/DataBaseResult/DataBasehtml.html
[ January 21, 2008: Message edited by: jumpa lahari ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by jumpa lahari:


Hi,

This is the http://localhost:8082/DataBaseResult/DataBasehtml.html


OK, so your contextPath is DataBaseResult.
The URL in your HTML form doesn't match your contextPath:
<form name="input" action="/Database/finish" method="post">

Try:
<form name="input" action="/DataBaseResult/finish" method="post">
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:


OK, so your contextPath is DataBaseResult.
The URL in your HTML form doesn't match your contextPath:
<form name="input" action="/Database/finish" method="post">

Try:
<form name="input" action="/DataBaseResult/finish" method="post">



Ok contextPath is nothing but the name of the application ok,

I even gave like form name="input" action="/DataBaseResult/finish" method="post"

but when I enter some text in the form only the Jsp is executing not the database.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Uhmmm.
Why is your database code in your servlet's main method?
Why does your servlet have a main method?

The container is never going to call your main method.
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
Uhmmm.
Why is your database code in your servlet's main method?
Why does your servlet have a main method?

The container is never going to call your main method.


Hi,

Ya servlet wont have the main method , thats the reason i thought placing the code to connect the database make no difference.

ok i will keep the database connection in a different java class.

The main problem is how to connect the data from the form text to the servlet.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

So here I am able to connect the database but how to send the getParameter() into the database is the main problem.




You would either build up your SQL query with the values retrieved from the form parameters (not the best way):



Or learn about Prepared Statements.
The first method is error prone, can cause you grief if apostrophes or quotes are present in the form parameters, and opens you up to SQL Injection hacks.

Try Googling for "JDBC Tutorial PreparedStatement" to find tutorials and example code.
[ January 21, 2008: Message edited by: Ben Souther ]
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:




You would either build up your SQL query with the values retrieved from the form parameters (not the best way):



Or learn about Prepared Statements.
The first method is error prone, can cause you grief if apostrophes or quotes are present in the form parameters, and opens you up to SQL Injection hacks.

Try Googling for "JDBC Tutorial PreparedStatement" to find tutorials and example code.

[ January 21, 2008: Message edited by: Ben Souther ]


Thanks Ben,

I go through them and come back to you in a day.
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by jumpa lahari:


Thanks Ben,

I go through them and come back to you in a day.


Hi Ben,

I am using the statement in the java class, I mean in the class which is able to access the oracle database and which is updating and also retrieving the data on to the console.

But my problem is how to make connection between the HTML form Input to the Oracle Database through the java or a servlet class.

As we are getting the parameters from the html text form into servlet by the



But I want this parameters to be added in the oracle database connection class


For example in the below statement I want my parameters be added to the values , so that I can insert data directly into the database from the html form through the servlet but i dont know how to make it can you please bring some light.





Thanks.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

In my earlier post, I mentioned two ways.

1.) Use the variables created from getParameter calls when building up the SQL string.

2.) Use the methods provided by PreparedStatement (better route).
Have you looked into these?
kalyan godavarthy
Greenhorn

Joined: Jan 14, 2008
Posts: 5
Hi
Copy the code from the servlet into a new method in another new class, send all the parameters you fetch from request from the browser to this method, before that create an object of this new class in the servlet doGet or doPost whatever and then call this method with all the parameters. so 1. enter the values in the browser,2. servlet receives the request and creates an object of new class and sends the parameters to this method,3. this method creates a conn, stmt or pstmt, resultset and does the database part what ever you return back from the databse for either select or insert or update iterate through result set if any and store it in an vector or so and return this vector,4. this vector is used to create the jsp response and so use the printwriter out for writing the response onto the jsp page
thanks


live and let livE
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
In my earlier post, I mentioned two ways.

1.) Use the variables created from getParameter calls when building up the SQL string.

2.) Use the methods provided by PreparedStatement (better route).
Have you looked into these?


Hi Ben,

I tried to look into the preparedstatement, but i am unable to understand how to send the parameters from the html file.

Thanks
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by KALYAN ANAND:
Hi
Copy the code from the servlet into a new method in another new class, send all the parameters you fetch from request from the browser to this method, before that create an object of this new class in the servlet doGet or doPost whatever and then call this method with all the parameters. so 1. enter the values in the browser,2. servlet receives the request and creates an object of new class and sends the parameters to this method,3. this method creates a conn, stmt or pstmt, resultset and does the database part what ever you return back from the databse for either select or insert or update iterate through result set if any and store it in an vector or so and return this vector,4. this vector is used to create the jsp response and so use the printwriter out for writing the response onto the jsp page
thanks


Hi kalyan,

Thanks for your reply but i haven't got the result.

[ January 22, 2008: Message edited by: jumpa lahari ]
[ January 22, 2008: Message edited by: jumpa lahari ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by jumpa lahari:


Hi Ben,

I tried to look into the preparedstatement, but i am unable to understand how to send the parameters from the html file.

Thanks


Can you show me what code you've written so far for your prepared statement?
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:


Can you show me what code you've written so far for your prepared statement?


Hi ben,

this is the code that i wrote ,



but i am getting error like invalid table name even though i created a table inside the oracle with the table name NOTESDB.

I know this is not sufficent for the connection of the html to the database connection class.
[ January 22, 2008: Message edited by: jumpa lahari ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Look at whatever tutorial you're using again.
Specifically, look for question marks "?" inside the SQL statement.
Read up on those.

See if that gets you going in the right direction.
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
Look at whatever tutorial you're using again.
Specifically, look for question marks "?" inside the SQL statement.
Read up on those.

See if that gets you going in the right direction.


Ya i looked at the "?" , it is representing the setmethod for the int or string which are invoked by the object created by the preparedstatement, which i did in the code.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by jumpa lahari:


Ya i looked at the "?" , it is representing the setmethod for the int or string which are invoked by the object created by the preparedstatement, which i did in the code.


No you didn't.
This is the last SQL statement that you've posted:

There are no question marks in that string.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

This thread, at this point, is more about JDBC and PreparedStatements so I'm going to move it to our JDBC forum.
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Ben Souther:
This thread, at this point, is more about JDBC and PreparedStatements so I'm going to move it to our JDBC forum.


Thanks for moving into the JDBC,

Ya i havent kept any "?" inside the prestatement now i gave like this

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150


I see a few problems in this code snippet.
1) That is not a valid SQL statement. There is a missing = after the word title.
2) There are only two question marks in the query, but the prepared statement sets three parameters.
3) There is no where clause. Do you really want to update every single row in the table with those values?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Jeanne Boyarsky:

I see a few problems in this code snippet.
1) That is not a valid SQL statement. There is a missing = after the word title.
2) There are only two question marks in the query, but the prepared statement sets three parameters.
3) There is no where clause. Do you really want to update every single row in the table with those values?


Hi,

1)i gave like title =
2)i commented in my code the third statement from the above code.
3)Ya we have to give the where clause , and i tried to give the like



I dont think this is correct one in the prepared statement.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Originally posted by jumpa lahari:

1)i gave like title =
2)i commented in my code the third statement from the above code.
3)Ya we have to give the where clause , and i tried to give the like

Can you post the new code/prepared statement calls? It's requiring some imagination to figure out what you are looking at.
jumpa lahari
Greenhorn

Joined: Jan 17, 2008
Posts: 18
Originally posted by Jeanne Boyarsky:

Can you post the new code/prepared statement calls? It's requiring some imagination to figure out what you are looking at.


Hi,

I gave the code like


But the main issue is that, i am unable to find a solution to pass the paramaters when i enter a text in the html file into oracle database through the servlet, please bring some light ,
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2500
    
    8

You set two parameters, but the sql statement only contains 1 question mark.
Every ps1.set..() binds a parameter to a question mark. Your line 'ps1.setString(2, "tom")' can't be bound.

Regards, Jan
[ January 24, 2008: Message edited by: Jan Cumps ]

OCUP UML fundamental and ITIL foundation
youtube channel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Html Input to oracle through servlet and JSP output