• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt jsp + servlet + postgresql

 
ronald silva
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi.
I am begining in the world of java/servlet.
and... I am still learning (without IDE)... I am trying a simple insert in a database postgresql.
it is very simple:
1) a page JSP with forms. the user fill 4 fields.
2) the datas from froms go to the servlet. the servlet receives those datas and it put them in a object. after that, those data are rescued from object, and the servlet open the conection to the database (postgres). so the servlet do the "insert".
3) all ok? it happens a redirection to the jsp "sucesso.jsp" (success). all wrong? it happens a redirection to the jsp "errorjsp" (error)
observation: I am respecting the MVC.
when I compile the code below, I receive a message of error.
what am I doing wrong?





the message-error when I compile is:

error: invalid method declaration; return required
inserir();
1 error

I confess that I dont discover so far, how to pass the objetc like a argument/parameter...
there are not an example of CRUD in the internet without IDE...
(I dont like IDE)
an "insert" is so simple in others languages of programming... but is not in the java servlet...
I have been reading books of java, but I didnt find anything simple example that resolve this "problem".
anyone help me please?

 
Knute Snortum
Bartender
Pie
Posts: 2482
43
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have this line near the end of your program:

It is outside of any method, so the compiler thinks you're starting a method declaration. You should probably just remove that line.
 
ronald silva
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, friend,
well... how do I "call" the method below?

I put
but I received other message-error.
 
ronald silva
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put obj.inserir();
but I received other message-error.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As with any other Java code, statements must be contained within a method. Servlet classes are no different than any other Java class.
 
ronald silva
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, the satementes stay inside the method.
but in som time I need "call" the method, dont I?
for example, if I have a
and I have a objetc "snoopy".
and I have a method
how do I call this method ?


but in the servlet seems more complicated...
I just want insert the data in the postgresql...
please help.
tell me sugestions.
I want this servlet works... but I dont know how...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, first things first. Your code is really really hard to read because the indentation is not good. If you want help with your code, you need to make sure that it is properly indented and formatted. Otherwise, it's just too hard to read and people will simply move on to other people's posts.

Secondly, it looks like you are declaring a constructor for the servlet at line 21. Servlets should not have explicit constructors. Remove this.

Then, you are using instance variables in the servlet. Also not allowed. This will create threading problems in the servlet.

Then, at line 59, you are creating a new instance of the servlet. You never create instances of servlets. Servlets are created by the servlet container and you should never create them yourself.

While servlets are Java classes, yes they have special rules that must be followed. You really need to find a good tutorial on servlets and work through it. Learning servlets from scratch is not something that is possible in forum posts.
 
ronald silva
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I have been studying java and servlets. I bought two books last year. but... nowhere I find how to make a CRUD using servlets without IDE. and you can see... I just want to make a simple insert in a database. so could you help me? if you teach me how to make a insert, I can make the others thins: delete, edit, consult...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Learning how to use a database is separate from servlets. Once you understand how servlets work and can create simple servlets, you'll need to look into JDBC or an ORM for database access.

A servlet tutorial will not teach you how to use a database. Here's a tutorial on JDBC.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, note that your database access code should not be in the servlets. The servlets are part of the control layer. DB access should be part of the model, in classes that the servlets can call.
 
ronald silva
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, I didnt divide the servlet in several servlets... one servlet to DAO, other servlet to model... etc.
in this moment I would like to do all in only one servlet.
this only servlet receives the data from form-jsp, open de connection to the databae, and insert the data in the database.
can you tell me how?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the JDBC tutorial.

And I said nothing about multiple servlets. The DB access should be in other classes, not other servlets.
 
Dave Tolls
Ranch Hand
Posts: 1914
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ronald silva wrote:
this only servlet receives the data from form-jsp, open de connection to the databae, and insert the data in the database.
can you tell me how?


This is very important, and will help you not get confused later on, but your servlet does not receive data from a jsp. It receives it from the client.


That's the basic structure (I threw in the database interaction to try and show what Bear is talking about, though that would often be a service of some sort which then calls the DAO).
Note the client is not doing anything with JSPs.
It is working with the HTML produced by a JSP.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point, Dave.

If any of what Dave posted is confusing, please read this article to understand how JSP works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic