database - Two error messages received when accessing or adding entries to Java DB -
package deadb; import java.sql.connection; import java.sql.drivermanager; import java.sql.sqlexception; import java.sql.statement; import java.sql.resultset; import java.util.scanner; public class deadb { public static void main(string[] args) { try{ int controller; string host = "jdbc:derby://localhost:1527/deadornas"; string uname = "tmybr11"; string upass = "1324132448"; connection con = drivermanager.getconnection( host, uname, upass ); statement stmt = con.createstatement( resultset.type_forward_only, resultset.concur_read_only ); string sql = "select * deadornas"; resultset rs = stmt.executequery( sql ); scanner input = new scanner( system.in ); int beta = 0; while( beta == 0 ){ system.out.printf( "\ncompany's data base. want do?\n" ); system.out.println( "1 - see products list" ); system.out.println( "2 - add product" ); system.out.println( "----------" ); controller = input.nextint(); if ( controller == 1 ){ while( rs.next() ){ string product_name = rs.getstring("product_name"); string outtern = "product: "+ product_name; system.out.println(outtern); } } else if ( controller == 2 ){ int rowcount = 0; while ( rs.next() ){ rowcount++; } int newrow = rowcount + 1; system.out.println( "inform name of product:" ); string prod = input.next(); int c = stmt.executeupdate( "insert deadornas" + "(id_product, product_name)" + "values ("+ newrow +", '"+ prod +"')"); } } } catch( sqlexception err ){ system.out.println( err.getmessage() ); } } }
the code above main class. created database using netbeans , i'm accessing using above application. works fine if don't use while loop. is, when user chooses option 1 or option 2. added while loop user can either add entry , see new entries list after or see entries list , add entry (an endless loop. add way out latter).
this output when add entry , see entries list:
company's data base. o que deseja fazer? 1 - see products list 2 - add product ---------- 2 inform name of product: test company's data base. o que deseja fazer? 1 - see products list 2 - add product ---------- 1 o resultset não está aberto. operação 'next' não é permitida. verifique se auto-efetivação está desabilitada.
so, didn't found exact translation (i'm brazilian) message above it's "the resultset isn't opened. 'next' operation not allowed. check if auto-effectuation disabled."
i don't know why happens , how can fix error.
and output when see entries list , add new entry:
company's data base. want do? 1 - see products list 2 - add product ---------- 1 product: rabit product: lyon product: cat product: dog product: bird product: cabelo product: tv product: fruit product: fruit product: onion product: test company's data base. want do? 1 - see products list 2 - add product ---------- 2 inform name of product: carrot instrução foi interrompida, porque iria gerar um valor duplicado da chave em uma restrição de chave primária ou de unicidade identificada por 'sql150619130501560' definida em 'deadornas'.
the translation "the statement aborted because have caused duplicate key value in unique or primary key constraint or unique index identified 'sql150619130501560' defined on 'deadornas'."
so, these messages receive. ideas?
inside of block option 1, need re execute query
resultset rs = stmt.executequery( sql );
a resultset maintains state, , knows in table. when reaches end, needs repointed beginning or can create new result set. result set cursor.
for second error, little related first problem.
int rowcount = 0; while ( rs.next() ){ rowcount++; }
since not using resultset, rowcount initializes 0 value, 1 added it. insert happens, , tries insert 1 index there 1 primary key.
Comments
Post a Comment