java - Mysql: Get Strings? -
i try receive names out of database.
i did write code:
public static string getcmdcommand(int resultcount) throws exception { try { // load mysql driver, each db has own driver class.forname("com.mysql.jdbc.driver"); // setup connection db connect = drivermanager.getconnection(""+mybot.mysqldbpath+"",""+mybot.mysqldbusername+"",""+mybot.mysqldbpassword+""); preparedstatement zpst=null; resultset zrs=null; zpst=connect.preparestatement("select `befehlsname` `eigenebenutzerbefehle`"); zrs=zpst.executequery(); if(zrs.next()){ return zrs.getstring(resultcount); }else{ return "-none-"; } }catch (exception e) { throw e; } { close(); } }
and start method running loop:
for(int = 0; <= cmdamount-1; i++){ try { eebbenutzerbefehl = dao.getcmdcommand(i); } catch (exception e) { e.printstacktrace(); } }
cmdamount integer valuable of total fields inside database.
so i.e database holds name1 name2 name3, wrong call them this? :
return zrs.getstring(resultcount);
which should be:
zrs.getstring(0) = name1 zrs.getstring(1) = name2 zrs.getstring(2) = name3
i receive java.sql.sqlexception: column index out of range, perhaps continue check first entry in database :confused:
return zrs.getstring(resultcount);
the getstring() method should given index of column want return going same. should pass in constant here such 0.
also, should open database once rather on , on again in 1 method passing in "connect" variable parameter.
here's if wanting retrieve name each row of table.
public static arraylist<string> getcmdcommand(connection connect) throws exception { try { preparedstatement zpst=null; resultset zrs=null; arraylist<string> names = new arraylist<string>(); zpst=connect.preparestatement("select `befehlsname` `eigenebenutzerbefehle`"); zrs=zpst.executequery(); // result set contains names retrieved call database, // need iterate through them , store them in list. while(zrs.next()) { names.add(zrs.getstring(0)); } } catch (exception e) { throw e; } { close(); } return names; }
you don't need tell how many fields there because figure out itself.
class.forname("com.mysql.jdbc.driver"); connection connect = drivermanager.getconnection(""+mybot.mysqldbpath+"",""+mybot.mysqldbusername+"",""+mybot.mysqldbpassword+""); try { arraylist<string> names = dao.getcmdcommand(connect); } catch (exception e) { e.printstacktrace(); } if(names.size() < 1) { // " - none - " }
Comments
Post a Comment