• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Inserting data into database in jsp page

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

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

Isn't it...............
 
Ranch Hand
Posts: 1325
Android Java Windows
  • 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.
 
author
Posts: 4107
28
Google Web Toolkit Eclipse IDE Flex
  • 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.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • 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
Posts: 4107
28
Google Web Toolkit Eclipse IDE Flex
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!