i've created this servlet to connect to database and retrieve data then output them in JSON format. this is the servlet :
public class ConnectServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("application/json");
PrintWriter out=res.getWriter();
JSONArray relatedWorkordersArray = new JSONArray();
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/users","root","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from user");
JSONObject jObj=new JSONObject();
st = (Statement) con.createStatement();
rs = (ResultSet) st.executeQuery("select * from user");
while (rs.next()) {
String nom = rs.getString("nom");
String prenom = rs.getString("prenom");
int age = rs.getInt("age");
JSONObject Object = new JSONObject();
Object.put("nom", nom);
Object.put("prenom", prenom);
Object.put("age", age);
relatedWorkordersArray.put(Object);
}
jObj.put("data",relatedWorkordersArray.toString());
jObj.put("Success", true);
rs.close ();
st.close ();
con.close();
//print JSON object
out.print(jObj);
Now, In my sencha touch project i've created a JSON-P STORE and i've putted in his url the name of this servlet : ConnectServlet.java but i have an error: unable to load data using the supplied configuration. open in browser : ConnectServlet.java
how can I do to make this proxy using the servlet to get data from database?
you may find your solution here. click here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.