I have this below code in which a request goes from browser through rest web service to the controller and then database connection get established through jdbc , my query is that i want to change my below code in such a way so that it can retrieve all the columns of the table , please advise how can i achieve the same
@GET
@Produces(MediaType.TEXT_HTML)
public String retriveData(@QueryParam("tablename") String tablename) throws SQLException
{
Connection con=null;
PreparedStatement ps=null;
String statement="";
String retString="";
try {
//Class.forName("com.mysql.jdbc.Driver");
//put sql jdbc jar in tomcat lib
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println(" ******Driver Loaded *******");
con=DriverManager.getConnection("jdbc:sqlserver://sv53720.hk.sunlife:1433; databaseName=pin_uat", "pinread","pinread123");
con.setAutoCommit(false);
System.out.println(" ******Connected To MSSql ******");
System.out.println("FROM TABLE NAME : "+tablename);
statement="SELECT * FROM "+tablename+";";
System.out.println("STATEMENT : "+statement);
ps=con.prepareStatement(statement);
// Turn use of the cursor on.
//ps.setFetchSize(50);
ps.setMaxRows(10);
ResultSet rs=ps.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
String name=rsmd.getColumnName(1);
while(rs.next())
{
retString=retString+name+" : "+rs.getString(name)+"<br>";
System.out.println(retString);
}
System.out.println("Table FOUND!!!");
ps.close();
rs.close();
con.close();
return retString;
}catch(Exception e) {
e.printStackTrace();
}
finally {
if(con!=null)
con.close();
}
return "Unable To Read Table :(";
}