File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting data into database in jsp page

 
Harshal Gurav
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Harshal,

Try with st.executeUpdate() instead of PreparedStatement.executeUpdate()
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Rakesh Chaudhary
Ranch Hand
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that was to simple for you Winay.....

Isn't it...............
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 3884
14
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should be a rule that JSP pages cannot access any JDBC packages... its scary when people include JDBC inside JSPs.
 
Paul Clapham
Sheriff
Pie
Posts: 20158
23
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Saloon Keeper
Posts: 3884
14
Eclipse IDE Flex Google Web Toolkit
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic