Java SQL (JDBC) : how to move to the next column? -



Java SQL (JDBC) : how to move to the next column? -

i'm trying check if "username" , "email" arguments in constructor existed in sql table.

this code:

public db(string usr, string eml, string pwd) { this.usr = usr; this.eml = eml; this.pwd = pwd; string jdbcurl = "jdbc:mysql://localhost:3306/registered"; string jdbcuser = "...."; string jdbcpassword = "...."; seek { class.forname("com.mysql.jdbc.driver"); connection = drivermanager.getconnection(jdbcurl, jdbcuser, jdbcpassword); statement = connection.createstatement();

now , if utilize select 2 columns, this:

string command = "select username,email users username '" + this.usr.tostring() + "';"; resultset = statement.executequery(command);

and loop resultset... this:

while (resultset.next()) { if (usr.equalsignorecase(resultset.getstring("username"))) { system.out.println("username : " + this.usr + " taken!"); } else if (eml.equalsignorecase(resultset.getstring("email"))) { system.out.println("email : " + this.eml + " taken!"); } else { system.out.println("email : " + this.eml + " , username : " + this.usr + " available!"); command = "insert users set username = '" + this.usr.tostring() + "',email = '" + this.eml.tostring() + "',password = '" + this.pwd.tostring() + "',status = '0' ,connected = '1';"; statement.executeupdate(command); } } } grab (sqlexception e) { system.out.println("sqlexception: " + e.getmessage()); system.out.println("vendor error: " + e.geterrorcode()); } grab (classnotfoundexception e) { e.printstacktrace(); }

the

resultset.next()

only runs on "first" column means if "usr" exists in table works, if "usr" not exist in table, other 2 if statements does-not work ..

,... want check both first column , second,.. , maybe 3rd or more soon.. , help?

your clause tests username, if username doesn't match this.usr.tostring(), resultset empty, while loop won't entered.

you should alter query match fields care - - "select username,email users username = ... or email = ..."

if resultset empty, you'll know can insert new record. otherwise, can check of fields (username, email) taken.

one more thing should aware of - executing sql statement without preparedstatement makes code vulnerable sql injection attacks.

you should alter code :

preparedstatement pstmt = con.preparestatement("select username,email users username = ? or email = ?"); pstmt.setstring(1, this.usr); pstmt.setstring(2, this.eml); resultset = pstmt.executequery();

you should alter insert statement utilize preparedstatement.

java sql jdbc resultset

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -