title: I want to fetch multiple data and return multiple records from database and convert that to json format?is this write way or not?
body: <p>I want to fetch multiple data and  return multiple records from database and convert that to json format?is this write way or not?</p>
  
  <p>also i want to set multiple data in bean class to that i can change it to json using gson lib.?
  whic on is better ??</p>
  
  <p>//WifiDb.java</p>
  
  <p>public class WifiDb {</p>
  
  <pre><code>public static void main(String[] args) {
  
     db d=new db();
  
     String s="charreendigarh";
  
       List jsonString=d.getInfo(s);
      System.out.println("hello"+jsonString);
     if(jsonString != null)
     {
         System.out.println("jsonString::"+jsonString);    
         JSONArray jsonArray = new JSONArray();
         JSONObject responseDetailsJson = new JSONObject();
  
         //   System.out.println("size "+jsonString);
         System.out.println("\n==&gt; Iterator Example...");
         Iterator itr = jsonString.iterator();
  
         while (itr.hasNext())
         {
  
             JSONObject formDetailsJson = new JSONObject();
             formDetailsJson.put("wifi_address",itr.next() );
             jsonArray.add(formDetailsJson);
         }
      //   GetAndSetWifiInfo gt=new GetAndSetWifiInfo(0, " Records Founds");
    //     responseDetailsJson.put("", gt);
  
         responseDetailsJson.put("data", jsonArray);
         System.out.println(""+responseDetailsJson);
  
     }
     else
      {
                  GetAndSetWifiInfo gt=new GetAndSetWifiInfo(1, "No Records Founds");
                   Gson gson = new Gson();
  
                  String gsn = gson.toJson(gt);
                  System.out.println("gsn message is" + gsn);
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <ol start="2">
  <li>db.java file</li>
  </ol>
  
  <p>class db {</p>
  
  <pre><code>static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  static final String DB_URL = "jdbc:mysql://localhost/test";
  static final String USER = "root";
  static final String PASS = "";
  
  
  List Addres_name_list=new ArrayList();
  
  List getInfo(String address) {
           System.out.println("address is ::"+address);
      Connection conn = null;
      Statement stmt = null;
      try {
          //STEP 2: Register JDBC driver
          Class.forName("com.mysql.jdbc.Driver");
  
          //STEP 3: Open a connection
          System.out.println("Connecting to database...");
          conn = DriverManager.getConnection(DB_URL, USER, PASS);
  
          //STEP 4: Execute a query
          System.out.println("Creating statement...");
          stmt = conn.createStatement();
          String sql;
          sql = "select * from wifidb_address where wifi_state='" + address + " '";
          System.out.println("sql"+sql);
          try (ResultSet rs = stmt.executeQuery(sql)) {
              while (rs.next())
  
              {
                  String wifi_address = rs.getString("wifi_address");
                  System.out.println("wifi_address is:"+wifi_address);
                  Addres_name_list.add(wifi_address);
  
  
  
              }
              //STEP 6: Clean-up environment
          }
          stmt.close();
          conn.close();
      } catch (SQLException | ClassNotFoundException se) {
      } finally {
          //finally block used to close resources
          try {
              if (stmt != null) {
                  stmt.close();
              }
          } catch (SQLException se2) {
          }// nothing we can do
          try {
              if (conn != null) {
                  conn.close();
              }
          } catch (SQLException se) {
          }//end finally try
      }//end try
      System.out.println("Goodbye!");
      return Addres_name_list;
  
  }
  </code></pre>
  
  <p>}</p>