1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletsConn extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletsConn() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection con=null;
try {
// Step 2. Create a Connection object
con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/", "postgres",
"0408Med01");
System.out.println("got connection");
// Step 3. Create a Statement object and call its executeUpdate
// method to insert a record
Statement s = con.createStatement();
String sql =
"INSERT INTO reception(msg_recu,en_tete,horodatage,destinataire) VALUES ('Michael', 'Franks', '12/12/2003', 'm')";
s.executeUpdate(sql);
//Step 4. Use the same Statement object to obtain a ResultSet object
String sql1 = "SELECT message,en_tete FROM envoi";
ResultSet rs = s.executeQuery(sql1);
while (rs.next()) {
out.println(rs.getString(1) + " " + rs.getString(2) + "<br>");
}
rs.close();
s.close();
con.close();
}
catch (SQLException e2) {
System.out.println("Error 1!!!");
}
}
} |
Partager