-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathViewRecords.java
More file actions
59 lines (52 loc) · 1.83 KB
/
ViewRecords.java
File metadata and controls
59 lines (52 loc) · 1.83 KB
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
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
public class ViewRecords extends HttpServlet {
Connection con;
HashMap methods;
@Override
public void init() throws ServletException {
try {
Class.forName("org.hsqldb.jdbcDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace(System.out);
}
try {
con=DriverManager.getConnection("jdbc:hsqldb:hsqldb-1_7_2","SA","");
} catch (SQLException e) {
e.printStackTrace(System.out);
}
methods = new HashMap<String,String>();
methods.put("str", "select * from contacts where name='%s'");
methods.put("int_groupby", "SELECT * FROM contacts GROUP BY %s");
methods.put("int_orderby", "SELECT * FROM contacts ORDER BY %s");
methods.put("int_inline", "%s");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
try {
String inject = request.getParameter("inject");
String method = request.getParameter("method");
String query = String.format((String)methods.get(method), inject);
ResultSet rs =con.createStatement().executeQuery(query);
while(rs.next()){
out.write("<br/>"+rs.getString(1));
out.write(", "+rs.getString(2));
out.write(", "+rs.getString(3));
}
out.write("<hr/><a href='index.html'>Home</a> ");
} catch (SQLException e) {
throw new ServletException(e);
}
}
}