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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| package testBiblio;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import java.sql.Statement;
@Path("/Bibbli")
public class biblioTestWS {
// Connection CONSTANTS
private static String LOGIN = null;
private static String PASSWORD = null;
private static String DRIVER = null;
private static String URL = null;
public static Connection getConnection() {
ResourceBundle rb = PropertyResourceBundle
.getBundle("testBiblio.DBConf");
LOGIN = rb.getString("login");
PASSWORD = rb.getString("password");
DRIVER = rb.getString("driver");
URL = rb.getString("url");
Connection maConnection = null;
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out
.println("---------------------------------------------------------------------");
System.out.println("----------DRIVER CONNECTION");
e.printStackTrace();
System.out
.println("---------------------------------------------------------------------");
}
try {
maConnection = DriverManager.getConnection(URL, LOGIN, PASSWORD);
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out
.println("---------------------------------------------------------------------");
System.out.println("----------DATA BASE ACCESS");
e.printStackTrace();
System.out
.println("---------------------------------------------------------------------");
}
return maConnection;
}
@GET
@Path("/ville")
@Produces(MediaType.APPLICATION_JSON)
public String getVille(@QueryParam("City") String city_id) {
Connection dbCon = null;
dbCon = getConnection();
Statement stmt;
try {
stmt = dbCon.createStatement();
String query = "SELECT lib_name FROM library where city_id = '"
+ city_id + "';";
System.out.println(query);
ResultSet rs = stmt.executeQuery(query);
JSONArray jsonArray = new JSONArray();
System.out
.println("---------------------------------------------------------------------");
System.out
.println("------------------------- LISTE DES BIBLIO ------------------");
while (rs.next()) {
String lib_name = rs.getString("lib_name");
System.out
.println("-------->>>>>> "+lib_name);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("library", lib_name);
jsonArray.put(jsonObject);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out
.println("---------------------------------------------------------------------");
return jsonArray.toString();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
} |