i need there code working on eclipse. when try execute 2 select queries in java doesn't seem work , searched internet couldn't find solution. need execute 2 select because need data 2 tables got in database.
table 1: questions table 2: selections
well first query seems work fine can find items should when execute. items table 2 throws me execution "column 'selid' not found. ". // selid column on table 2.
i posting faulty code on bottom in case can me on this. in advance.
public void doget(httpservletrequest req, httpservletresponse res) throws ioexception, servletexception { //string connectionurl = "jdbc:mysql://127.0.0.1:3306/newdata";// newdata database //connection connection; connection conn=null; string dbname="teddb"; res.setcontenttype("text/html"); res.setcharacterencoding("utf-8"); printwriter out = res.getwriter(); //string dbusername="root"; //string dbpassword="root"; try{ string qid = ""; string question = ""; string sel1 = ""; string sel2 = ""; string sel3 = ""; string correct = ""; string selid =""; string sel=""; connection dbcon; class.forname(driver); dbcon = drivermanager.getconnection(dburl); resultset rs; resultset rs2; statement stmt; statement stmt2; stmt = dbcon.createstatement(); stmt2 = dbcon.createstatement(); string qry = ""; string qry2 = ""; qry = "select * questions"; qry2 = "select * selections"; rs = stmt.executequery(qry); stmt = dbcon.preparestatement(qry); rs2 = stmt2.executequery(qry2); stmt2 = dbcon.preparestatement(qry2); string[] columns = new string[] { "qid", "question_text" , "selid" , "selection_text" ,}; random rn = new random(); int range = 2 - 1 + 1; int randomnum = rn.nextint(range) + 1; out.println(randomnum); while (rs.next()) { (int = randomnum; <= randomnum; i++) { question = rs.getstring(columns[1]); sel1 = rs.getstring(columns[2]); sel2 = rs.getstring(columns[3]); } } preparedstatement pstmt; (int z=1;z<=3;z++){ selid = string.valueof(rs.getstring(columns[2])); pstmt = dbcon.preparestatement(qry2 + " qid = ? , selid ='z'"); pstmt.setstring(1, qid); rs2 = pstmt.executequery(); while (rs2.next()) { (int = randomnum; <= randomnum; i++) { if (z==1) sel1 = rs.getstring(columns[3]); else if (z==2) sel2 = rs.getstring(columns[3]); else sel3 = rs.getstring(columns[3]); } } } out.println("<!doctype html>"+ "<html><body>"+ "<form method=\"post\" action=\"demoservlet\">"+ "<b><h1>Ερώτηση</h1></b> <br><br>"+ "<b><h1>"+question+" </h1></b> <br><br>"+ "<b> 1: </b> <input type=\"radio\" name=\"iscorrect\" value=\"" + sel1 + "\"/><br>"+ "<b> 2: </b> <input type=\"radio\" name=\"iscorrect\" value=\"" + sel2 + "\"/> <br>"+ "<b> 3: </b> <input type=\"radio\" name = \"iscorrect\" value=\"" + sel3 + "\"/><br><br>"+ "<br><input type=\"submit\" name=\"submit\" value=\"Απάντηση\"/>"+ "</form></body></html>"); dbcon.commit(); string msg=" "; rs.close(); rs2.close(); stmt.close(); dbcon.close(); } catch (exception e){ out.println(e); }
concept have 2 tables , iam making form users answers questions. iam executing both tables , iam trying put variables in form submit. dopost take effect after doget in same servlet.
here example of tables.
questions | selections qid | question | qid | | selid | selection_text |correct q1 | 1+1? | q1 1 5 0 q1 2 2 1 // true q1 3 4 0
change
rs = stmt.executequery(qry); rs2 = stmt.executequery(qry2);
to
rs = stmt.executequery(qry);//first query rs2 = stmt2.executequery(qry2);//second query
Comments
Post a Comment