title: java how retrieve data from database
body: <p>I have code</p>
  
  <pre><code>public static String getData(String query) {
          String output = "";
          try {
              String connectionUrl = "jdbc:sqlserver://localhost:1234;databaseName=123;user=123;password=123";
              Connection con = null;
              Statement stmt = null;
              ResultSet rs = null;
                  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                  con = DriverManager.getConnection(connectionUrl);
                  String SQL = "select smth from tableName where smth";
                  stmt = con.createStatement();
                  rs = stmt.executeQuery(query);
                  while (rs.next()) {
                      output =  (String) rs.getObject(1);
                  }
                  rs.close();
          }
          catch (Exception e) {
  
              return "ERROR while retrieving data: " + e.getMessage();
          }
          return output;
      }
  </code></pre>
  
  <p>It works if value is string. But if it integer? Or boolean? How to modify this method so it would be universal, no  matter what type data I get I still return it as string?</p>