Skip to content
Snippets Groups Projects
Commit fd78a294 authored by Yannis Devos's avatar Yannis Devos
Browse files

dao preque fini

parent 9cbfae96
No related branches found
No related tags found
No related merge requests found
...@@ -8,47 +8,103 @@ import jakarta.servlet.http.*; ...@@ -8,47 +8,103 @@ import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.annotation.WebServlet;
@WebServlet("/sae-userDAO") @WebServlet("/sae-userDAO")
public class UserDAO extends HttpServlet public class UserDAO extends HttpServlet{
private Connection con = DriverManager.getConnection("jdbc:postgresql://psqlserv/but2", "yannisdevosetu", "moi"); private Connection con;
{ public UserDAO(){
public void service( HttpServletRequest req, HttpServletResponse res ) try{
throws ServletException, IOException Class.forName("org.postgresql.Driver");
{ this.con = DriverManager.getConnection("jdbc:postgresql://psqlserv/but2", "yannisdevosetu", "moi");
StringBuilder txt = new StringBuilder("<tr><th>nMatch</th><th>eq1</th><th>eq2</th><th>date</th><th>sc1</th><th>sc2</th></tr>"); } catch (Exception e) {
e.getStackTrace();
}
}
public String getAllUsers(){
StringBuilder txt = new StringBuilder();
try{ try{
Statement stmt = con.createStatement(); Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM rencontres"); ResultSet rs = stmt.executeQuery("SELECT * FROM userAccount;");
while(rs.next()){ while(rs.next()){
int nMatch = rs.getInt(1); int userId = rs.getInt(1);
int eq1 = rs.getInt(2); String username = rs.getString(2);
int eq2 = rs.getInt(3); String pwd = rs.getString(3);
String date = rs.getString(4);
int sc1 = rs.getInt(5); txt.append("Id : ").append(userId).append(" Username : ").append(username).append(" Password : ").append(pwd).append('\n');
int sc2 = rs.getInt(6); }
} catch (SQLException sqle) {
sqle.getStackTrace();
}
txt.append("<tr><td>").append(nMatch).append("</td><td>").append(eq1) return txt.toString();
.append("</td><td>").append(eq2)
.append("</td><td>").append(date)
.append("</td><td>").append(sc1)
.append("</td><td>").append(sc2)
.append("</td></tr>");
} }
} catch(ClassNotFoundException cnfe){ public String getUserById(int id){
System.out.println(cnfe.getMessage()); StringBuilder txt = new StringBuilder();
try{
PreparedStatement stmt = con.prepareStatement("SELECT * FROM userAccount WHERE id=?");
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
if(rs.next()){
int userId = rs.getInt(1);
String username = rs.getString(2);
String pwd = rs.getString(3);
txt.append("Id : ").append(userId).append(" Username : ").append(username).append(" Password : ").append(pwd).append('\n');
}
} catch (SQLException sqle) {
sqle.getStackTrace();
} }
res.setContentType("text/html;charset=UTF-8"); return txt.toString();
PrintWriter out = res.getWriter();
out.println( "<head><title>servlet first</title>" );
out.print("<link href=\"css/styles.css\" rel=\"stylesheet\" />");
out.println( "<META content=\"charset=UTF-8\"></head><body><center>" );
out.println( "<h1>Test de ma Servlet</h1>" );
out.println( "<h2>Super ! ça marche</h2>" );
out.println("<table>" + txt + "</table>");
out.println( "</center> </body>" );
} }
} }
//{
// public void service( HttpServletRequest req, HttpServletResponse res )
// throws ServletException, IOException
// {
// StringBuilder txt = new StringBuilder("<tr><th>nMatch</th><th>eq1</th><th>eq2</th><th>date</th><th>sc1</th><th>sc2</th></tr>");
//
// try {
// Statement stmt = con.createStatement();
// ResultSet rs = stmt.executeQuery("SELECT * FROM rencontres");
//
// while (rs.next()) {
// int nMatch = rs.getInt(1);
// int eq1 = rs.getInt(2);
// int eq2 = rs.getInt(3);
// String date = rs.getString(4);
// int sc1 = rs.getInt(5);
// int sc2 = rs.getInt(6);
//
// txt.append("<tr><td>").append(nMatch).append("</td><td>").append(eq1)
// .append("</td><td>").append(eq2)
// .append("</td><td>").append(date)
// .append("</td><td>").append(sc1)
// .append("</td><td>").append(sc2)
// .append("</td></tr>");
// }
//
// } catch(ClassNotFoundException cnfe){
// System.out.println(cnfe.getMessage());
// }
//
// res.setContentType("text/html;charset=UTF-8");
// PrintWriter out = res.getWriter();
// out.println( "<head><title>servlet first</title>" );
// out.print("<link href=\"css/styles.css\" rel=\"stylesheet\" />");
// out.println( "<META content=\"charset=UTF-8\"></head><body><center>" );
// out.println( "<h1>Test de ma Servlet</h1>" );
// out.println( "<h2>Super ! ça marche</h2>" );
// out.println("<table>" + txt + "</table>");
// out.println( "</center> </body>" );
// }
//}
\ No newline at end of file
{
"name": "s4a021-web-backend",
"lockfileVersion": 3,
"requires": true,
"packages": {}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment