Javatpoint Logo

91-9990449935

 0120-4256464


servlet insert postgres

By: ronpdf@gmail.com On: Thu Jun 16 08:30:32 IST 2016     0 0 0  0
hi,
I am new in the java-servlet.
I have been studying through books, internet...
well, I have a doubt: how to make a simple insert in the database postgres?
1) a page JSP with forms.
2) the servlet receives the data from forms-jsp
3) the SAME servlet open the connection to the database postgres
4) th SAME servlet insert the data in the postgres.
I know, I know, several class... several servlets... but I want to make all in the only one servlet.
who would help me?

I made a simple code:

<html>
<center>
<h1> inserting data-form </h1>
</center>
<body>
<center>
<formmethod="post" action="insertservlet.do">
<fieldset>
<legend>putting data</legend><br><br>
<label>Name:</label><input type="text" name="name" ><br><br>
<label>Phone:</label><input type="text" phone="phone" ><br><br>
<inputtype="submit" value="send"><input type="reset" value="clear"><br><br>
</fieldset>
</form>
</center>
</body>
</html>

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.*;
import java.sql.Connection;
import java.sql.DriverManager;

public class InsertServlet extends HttpServlet {

String nam;
String phon;

String fate= "x";

//****************************************************

public void setNam (String nam){
this.nam = nam;
}
public void setPhon (String phon){
this.phon = phon;
}


//****************************************************

public String getNam(){
return nam;
}
public String getPhon(){
return phon;
}

//---------------------------------------------------
public void doPost (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");

InsertServlet obj = new InsertServlet();

variavel.setNam(request.getParameter("name"));
variavel.setPhon(request.getParameter("phone"));
}

//*************************************************************************************************************************************************************************
public Connection getConexao() {
Connection conexao = null;
String usuario = "app";
String senha = "1234567";
String nomeBancoDados = "locadora";

try {
Class.forName("org.postgresql.Driver");
conexao = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + nomeBancoDados, usuario, senha);
} catch (Exception e) {
e.printStackTrace();
}
return conexao;
}

//*************************************************************************************************************************************************************************
public void insertindatabase( ) {
try {
Connection conexao = getConexao();
PreparedStatement pstm = conexao.prepareStatement("Insert into tbaluno (name, phone) values (?,?)");
pstm.setString(2, variavel.getNam());
pstm.setString(3, variavel.getPhon());

pstm.execute();
pstm.close();
conexao.close();
fate = "sucesso.jsp";
} catch (Exception e) {
e.printStackTrace();
fate = "erro.jsp";
}
}

}

so... anyone help me please?
I want to make this code works.
it is simple.
no made with IDE.
no divided in several class.
I want all in a only one servlet.
just make a simple insert.
please, help me.
0



PLEASE REPLY

Please login first to post reply. Login please!