java - How to display record from oracle database using select keyword -
i want display record oracle database in java using select keyword, it's not working. when run program displays sql command not result want.
my code this:
static void modify() { system.out.println("enter employee id"); empid=sc.nextint(); try{ con=connection1.getconnection(); stmt=con.createstatement(); string d="select * emp empid='"+empid+"'"; rs=stmt.executequery(d); system.out.println(""+d); } catch(sqlexception e) { e.printstacktrace(); } }
when run application result shown instead of record database:
select * emp empid='14'
the problem printing name of results not results itself.
i have changed code below,
static void modify() { system.out.println("enter employee id"); empid=sc.nextint(); try{ con=connection1.getconnection(); stmt=con.createstatement(); string d="select * emp empid='"+empid+"'"; rs=stmt.executequery(d); while(rs.next()){ //getting column value record giving column no system.out.println(rs.getstring(1)); //line 1 //getting column value record giving column name, system.out.println(rs.getstring("empid"));// line 2 } } catch(sqlexception e) { e.printstacktrace(); } }
please note above code (line 1) print first column of each records( returned db), here order of returned columns not guaranteed.
if want specific column can specify column name argument in getstring method of resultset (line 2).
you have use appropriate methods values such , if column 2 has data type in db integer have use rs.getint(2)
.
in code,
system.out.println(""+rs);
in java when call println(object) method in system.out,which call string.valueof(object) method again call tostring() method, return string representation of object, here nothing query passed, not records.
Comments
Post a Comment