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 Inserting data into database in jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting data into database in jsp page" Watch "Inserting data into database in jsp page" New topic
Author

Inserting data into database in jsp page

Harshal Gurav
Ranch Hand

Joined: May 29, 2008
Posts: 151
Hi,
my adduser.jsp page consist of form with field username,groupid like.
I am forwarding this page to insertuser.jsp. my aim is that when I submit adduser.jsp page then the field filled in form should insert into the usertable.The insertuser.jsp is like:
<% String USERID=request.getParameter("id");
String NAME=request.getParameter("name");
String GROUPID=request.getParameter("group");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mynewdatabase","root", "root123");
PreparedStatement st;
st = con.prepareStatement("Insert into user values (1,2,4)");
st.setString(1,USERID);
st.setString(2,GROUPID);
st.setString(4,NAME);
// PreparedStatement.executeUpdate();//
}catch(Exception ex){
System.out.println("Illegal operation");

}
%>

But showing error at the marked lines lines as:non static method executeupdate can not be referenced from static context.
Really Speaking I am newbie in this java world.
whether you have any other solution for above issue?
waiting Your valuable suggestion.
Thanks and regards
haresh
[ July 09, 2008: Message edited by: Harshal Gurav ]
winay Kumar
Ranch Hand

Joined: Aug 13, 2007
Posts: 34
Hi Harshal,

Try with st.executeUpdate() instead of PreparedStatement.executeUpdate()


Winay Kumar<br />SCJP 5.0
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi..

May i know what you tried to do with your page.. Then try the solution given by winay kumar...it surely kickes the error.. You need to learn more about JDBC.. Just Click here to learn more on that...
When you create an object for an Interface, you must use them to access the methods in it..Hope it help you..


Never try to be a hard-worker. Be a smart-worker.
My Blog
Rakesh Chaudhary
Ranch Hand

Joined: Feb 19, 2008
Posts: 120
I think that was to simple for you Winay.....

Isn't it...............


Acquire the unexpected.......
Preparing for SCJP 6
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Hi Harshal,

first of all this considered a bad practice to use connection management in presentation layers.

the way you use PreparedStatement is actually not correct.. I would suggest to read Java Docs or use the tutorial provided by Sun.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

There should be a rule that JSP pages cannot access any JDBC packages... its scary when people include JDBC inside JSPs.


My Blog: Down Home Country Coding with Scott Selikoff
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Scott Selikoff:
There should be a rule that JSP pages cannot access any JDBC packages... its scary when people include JDBC inside JSPs.
There seems to be a lot of people doing that. Maybe they are living in a different time zone where it's still 2003.



But anyway I would recommend to Harshal: scriptlets in a JSP are really difficult for debugging if you aren't really comfortable with Java yet. So put your JDBC code into a plain old Java application and work with it until you have it working and understood. Then, if you are being made to use scriptlets, go ahead and put your JDBC code into the scriptlet.

Like the others, though, I don't think that code should be in a scriptlet. I think it should be in a servlet at least. But sometimes the people writing the code have bad choices forced on them from above.
[ July 10, 2008: Message edited by: Paul Clapham ]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

Originally posted by Paul Clapham:
There seems to be a lot of people doing that. Maybe they are living in a different time zone where it's still 2003.


I can only hope they read this and learn the error of their ways.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting data into database in jsp page