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:
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>
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");
}
}
- 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!!!!!!!!!!!