java - I need help to fetch the data from database -


this table

firstname----lastname----city--------state  ram----------padhan------houston-----tx  ram----------padhan------washingtom--mi  ram----------padhan------alok--------bg  keiry--------lading------azan--------bl  keiry--------lading------aror--------bs 

how can fetch data output be:

firstname: ram--lastname: padhan  city: houston--state:tx  city:washington--state:mi  city:alok--state:bg firstname: keiry--lastname: lading  city: azan--state:bl  city:aror--state:bs 

beside getting value each row:

firstname: ram--lastname: padhan  city: houston--state:tx  firstname: ram--lastname: padhan  city:washington--state:mi 

here's did. output getting first row firstname , lastname whereas state , city columns , getting value till end row.

public class practise  {      public static void main(string[] args)      {          connection conn = null;          statement stmt = null;           try {               class.forname("com.mysql.jdbc.driver") ;               string url = "jdbc:mysql://localhost:3306/table_name";                conn = drivermanager.getconnection(url,"root","root" );                stmt = (statement) conn.createstatement();                string sql = "select first_name ,last_name ,city_name , state_name, table_name";                resultset rs = stmt.executequery(sql);                while(rs.next())              {                 string first = rs.getstring("first_name");                  string last = rs.getstring("last_name");                  system.out.print("first: " + first);                  system.out.println("last: " + last);                                   {                      string city = rs.getstring("city_name");                      string state = rs.getstring("state_name");                      system.out.println("city: " + city);                     system.out.println("state: " + state);                      system.out.println();                  } while(rs.next());              }              rs.close();          }         catch(sqlexception se)         {               se.printstacktrace();          }         catch(exception e)         {               e.printstacktrace();         }         {               try {                  if (stmt != null)                      conn.close();               }              catch(sqlexception se)              { }               try {                  if (conn != null)                       conn.close();               }              catch(sqlexception se)              {                   se.printstacktrace();               }          }      }  } 

consider using 2 tables. 1 person, other city/state. select persons first, iterate, use key city/state.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -