Guest User

Untitled

a guest
May 23rd, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. <%@page import="java.sql.*,org.json.*;" %>
  2.  
  3. <%
  4. final String jdbcdriver="com.mysql.jdbc.Driver";
  5. final String url="jdbc:mysql://localhost/test";
  6. final String user="your_username";
  7. final String pass="your_password";
  8.  
  9. Connection con = null;
  10. Statement stmt = null;
  11. ResultSet result = null;
  12.  
  13. try{
  14. Class.forName(jdbcdriver);
  15. con = DriverManager.getConnection(url,user,pass);
  16. }catch(ClassNotFoundException e){
  17. }catch(SQLException e){}
  18.  
  19. try{
  20.  
  21. String query;
  22.  
  23. stmt = con.createStatement();
  24.  
  25. query = "SELECT * FROM jspAjax";
  26. result = stmt.executeQuery(query);
  27.  
  28. if(!result.next()){
  29. out.print("0");
  30. }else{
  31. JSONArray array=new JSONArray();
  32. do{
  33. JSONObject obj = new JSONObject();
  34. obj.put("FirstName",result.getString("FirstName"));
  35. obj.put("LastName",result.getString("LastName"));
  36. obj.put("Location",result.getString("Location"));
  37. array.put(obj.toString());
  38. }while(result.next());
  39. out.print(array);
  40. }
  41.  
  42. }catch(SQLException e){
  43. out.print("Exception: "+e);
  44. }
  45. finally{
  46. if(stmt != null){
  47. try{
  48. stmt.close();
  49. }catch(SQLException e){}
  50. }
  51. if(con != null){
  52. try{
  53. con.close();
  54. }catch(SQLException e){}
  55. }
  56. }
  57. %>
Add Comment
Please, Sign In to add comment