Biblioteca Java - Rev 19
Subversion Repositories:
package exemple.scd.web;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AdaugaPersoana2Servelet
*/
@WebServlet(name = "adaugaPersoana2", urlPatterns = { "/adaugaPersoana2" })
public class AdaugaPersoana2Servelet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection con;
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp?user=root");
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nume = request.getParameter("nume");
String prenume = request.getParameter("prenume");
PrintWriter out = response.getWriter();
try{
Statement s = con.createStatement();
s.executeUpdate("INSERT INTO PERSOANE VALUES('"+nume+"','"+prenume+"')");
out.println("Inregistrarea a fost adaugata");
}catch(SQLException e){
out.println("A aparut o eroare pe server la introducere date:"+e.getMessage());
}
}
}
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class AdaugaPersoana2Servelet
*/
@WebServlet(name = "adaugaPersoana2", urlPatterns = { "/adaugaPersoana2" })
public class AdaugaPersoana2Servelet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection con;
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp?user=root");
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String nume = request.getParameter("nume");
String prenume = request.getParameter("prenume");
PrintWriter out = response.getWriter();
try{
Statement s = con.createStatement();
s.executeUpdate("INSERT INTO PERSOANE VALUES('"+nume+"','"+prenume+"')");
out.println("Inregistrarea a fost adaugata");
}catch(SQLException e){
out.println("A aparut o eroare pe server la introducere date:"+e.getMessage());
}
}
}