@GET @Produces(MediaType.TEXT_HTML) public String returnData(@PathParam("param") String msg) throws Exception { PreparedStatement query = null; String myString = null; String returnString = null; Connection conn = null; try { Class.forName(driver); conn = DriverManager.getConnection(url, userName, passwrod); query = conn.prepareStatement("select * from WATCH_DATA where _id = " + msg); ResultSet rs = query.executeQuery(); while (rs.next()) { myString = "<p> id : " + rs.getInt(1) + " brand : " + rs.getString(2) + " model : " + rs.getString(3) + " color : " + rs.getString(4) + " gender : " + rs.getString(5) + " price : " + rs.getString(6) + " warrenty : " + rs.getString(7) + " image : " + rs.getString(8) + "</p>"; } query.close(); returnString = "<p>Database search</p>" + "<p>Date return: </p>" + myString; } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) conn.close(); } return returnString; }
public static void main(String[] args) throws Exception { StringBuilder sb = new StringBuilder(); sb.append("insert into popedom (id, id_parent, code, name, menu, idx"); for (int i = 0; i < ACTIONS; i++) { sb.append(",action" + i); sb.append(",action" + i + "_name"); } sb.append(") values (?, null, ?, ?, true, ?"); for (int i = 0; i < ACTIONS; i++) { sb.append(",?"); sb.append(",?"); } sb.append(")"); Class.forName("org.postgresql.Driver"); Connection conn = null; try { conn = DriverManager.getConnection( "jdbc:postgresql://192.168.2.250:5432/child", "postgres", "1234"); conn.createStatement().executeUpdate("delete from popedom "); PreparedStatement ps = conn.prepareStatement(sb.toString()); WinkApplication wa = new WinkApplication(); int i = 0; for (Class c : wa.getClasses()) { Path path = (Path) c.getAnnotation(Path.class); ps.setLong(1, i + 1); ps.setString(2, path.value()); if (c.isAnnotationPresent(Description.class)) { Description description = (Description) c.getAnnotation(Description.class); ps.setString(3, description.value()); } else { ps.setString(3, path.value()); } ps.setLong(4, i + 1); System.out.println(path.value()); Map<String, String> methodMap = new TreeMap<String, String>(); for (Method method : c.getDeclaredMethods()) { String code = null; if (method.isAnnotationPresent(GET.class)) { code = "GET"; } else if (method.isAnnotationPresent(POST.class)) { code = "POST"; } else if (method.isAnnotationPresent(PUT.class)) { code = "PUT"; } else if (method.isAnnotationPresent(DELETE.class)) { code = "DELETE"; } else { continue; } boolean isAnn = method.isAnnotationPresent(Path.class); if (isAnn) { code += " " + method.getAnnotation(Path.class).value(); } String name = null; if (method.isAnnotationPresent(Description.class)) { Description description = method.getAnnotation(Description.class); name = description.value(); } else { name = code; } methodMap.put(code, name); } int j = 0; for (Map.Entry<String, String> entry : methodMap.entrySet()) { ps.setString(5 + j * 2, entry.getKey()); ps.setString(5 + j * 2 + 1, entry.getValue()); j++; } for (int k = j; k < ACTIONS; k++) { ps.setString(5 + k * 2, null); ps.setString(5 + k * 2 + 1, null); } ps.executeUpdate(); i++; } ps.close(); } finally { try { conn.close(); } finally { conn = null; } } }