0

I am really new in Java (ok, totally brad new) and I'm trying to create a very basic form let's say just name and last name with Java and introduce the data in my postgresql database.

I need some step by step instructions on how to do it.

This is my understanding but I have questions in surly miss-conceptions:

  1. HTML file with Form

    <HMTL>
    <body>
    <form action=servlet" method="get">
    Name: <input type="text" name="name"><br>
    Last Name: <input type="text" name="lastname"><br>
    <input type="submit" value="Submit">
    </body>
    </html>
    
  2. Have the servlet file (First question: Is this a servlet.java or servlet.class file?)

Second question: whether is a .java or .class file, can I create and edit those files in a regular text editor? gedit in Ubuntu for instance? or do I need something like Eclipse?

Third question: to debug the code, I guess I need Eclipse or other debugger.

Forth question: How do I call the servlet.java or .class file from HTML? I have this in the HTML () but that will perform the connection to the Database as well?

And I I have to establish the connection to the Database

    import java.io.IOExcetion;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;

    public class servletExample extends HttpServlet{
    protected void doGet (HttpServletRequest reg, HttpSevletR
         throws ServletException, IOException {
       String name = req.getParameter ("name");
       String name = req.getParameter ("lastname");
         }
     }

    public class PostgreSQLJDBC {
       public static void main(String args[]) {
          Connection c = null;
          Statement stmt = null;
          try {
     Class.forName("org.postgresql.Driver");
     c = DriverManager
        .getConnection("jdbc:postgresql://localhost:5432/testdb",
        "user", "password");
     c.setAutoCommit(false);
     System.out.println("Opened database successfully");

     stmt = c.createStatement();
     String sql = "INSERT INTO USERS (name, lastname) ";
     stmt.executeUpdate(sql);

     stmt.close();
     c.commit();
     c.close();
     } catch (Exception e) {
     System.err.println( e.getClass().getName()+": "+ e.getMessage() );
     System.exit(0);
    }
      System.out.println("Records created successfully");
       }
    }
  1. I run my HTML file in the server: (If html file is index.html) localhost/index.html

This will show in my browser a form asking for name and lastname and a submit button and when clicking submit, it will introduce the values in the database.

THANK YOU SO MUCH FOR ALL YOUR HELP!!!!!!!!!!!

Samuel
  • 109
  • 2
  • 12

1 Answers1

0

Follow the tutorial: All type of issues and CRUD is available there.

  1. http://www.tutorialspoint.com/postgresql/
  2. http://www.postgresqltutorial.com/

In your code, you need to set name and lastname values using PreparedStatement.

 stmt = c.createStatement();
 String sql = "INSERT INTO USERS (name, lastname) ";
 stmt.executeUpdate(sql);

For this section you can follow the tutorial https://zetcode.com/java/postgresql/

Jan Bodnar
  • 10,969
  • 6
  • 68
  • 77
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • Thank you for the comment, but those are tutorials on postgresql. I guess my questions is more directed to which files do I need to have a HTML form running through Java. Thanks – Samuel Mar 19 '16 at 15:04
  • @Samuel on that case, you can follow http://www.cs.uofs.edu/~bi/eclipse/servlet-jdbc.html – SkyWalker Mar 19 '16 at 15:13