java - rs is always true even when table employee is not present in servlet Database -


rs true when table employee not present in servlet database, want create table when table not present in database , select count(*)from information_schema.tables where(table_schema = 'servlet') , (table_name = 'employee')" return when executed preparedstement , result stored in resultset when table not there , when table there both cases

public class table { public static void main(string[] args) { connection con=null; preparedstatement pstmt=null; preparedstatement pstmt1=null; preparedstatement pstmt2=null; resultset rs=null; string qry="select count(*)from information_schema.tables where(table_schema = 'servlet') , (table_name = 'employee')"; string qry1="create table servlet.employee (" +             " id int(6) not null auto_increment, " +             " name varchar(50) not null, " +              " department varchar(20) not null, " +              " salary decimal(8,2) not null, " +              " primary key (id))"; string qry2="insert servlet.employee values(?,?,?,?)";  class.forname("com.mysql.jdbc.driver"); con=drivermanager.getconnection("jdbc:mysql://localhost:3306?user=root&password=password"); pstmt=con.preparestatement(qry); rs=pstmt.executequery(); system.out.println(rs.next());     if(true==!rs.next()){     pstmt1=con.preparestatement(qry1);     pstmt1.executeupdate();     pstmt2=con.preparestatement(qry2);     pstmt2.setint(1,id);     pstmt2.setstring(2, name);     pstmt2.setstring(3, dept);     pstmt2.setdouble(4, salary);     pstmt2.executeupdate();     }     else{      pstmt2=con.preparestatement(qry2);     pstmt2.setint(1,id);     pstmt2.setstring(2, name);     pstmt2.setstring(3, dept);     pstmt2.setdouble(4, salary);     pstmt2.executeupdate();     }  }  } 

if table exists value 1 else 0. record exists. documentation says:

resultset.next: moves cursor forward 1 row. returns true if cursor positioned on row , false if cursor positioned after last row.

rs.next return true since there position cursor on first record in case. need process logic based on value of count(*). not need else clause since not doing vary different.

if(rs.next())   int count = rs.getint(1);   if (count == 0) {     pstmt1=con.preparestatement(qry1);     pstmt1.executeupdate();   }   pstmt2=con.preparestatement(qry2);   pstmt2.setint(1,id);   pstmt2.setstring(2, name);   pstmt2.setstring(3, dept);   pstmt2.setdouble(4, salary);   pstmt2.executeupdate(); } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -