public void displayQuizOptions() { // displays the the quizzes the pupil can do based on their skill level int quizNum = 0; int total = 0; String quizSkill = ""; try { DatabaseConnect.init(); Connection conn = DatabaseConnect.getConnection(); Statement stmt = conn.createStatement(); String query = "SELECT * FROM QUIZ where QUIZ_LEVEL ='" + skillLevel + "'"; ResultSet result = stmt.executeQuery(query); DefaultTableModel model = (DefaultTableModel) tblQuiz.getModel(); while (model.getRowCount() > 0) { model.removeRow(0); } if (result != null) { while (result.next()) { quizNum = result.getInt("QUIZ_NUMBER"); total = result.getInt("TOTAL_POINTS"); quizSkill = result.getString("QUIZ_LEVEL"); int number = NumQuestions(quizNum); model.addRow(new Object[] {quizNum, quizSkill, total, number}); } } stmt.close(); conn.close(); result.close(); } // end try catch (Exception ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } } // END method
/** * only for sql statements UPDATE, INSERT, DELETE. * * @param sql * @throws SQLException */ public static void executeUpdate(String sql) { Connection con = null; Statement st = null; DatabaseConnect.dbConnect(); con = DatabaseConnect.getConnection(); try { st = con.createStatement(); st.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public static ResultSet execute(String sql) { Connection con = null; Statement st = null; DatabaseConnect.dbConnect(); con = DatabaseConnect.getConnection(); ResultSet rs = null; try { st = con.createStatement(); rs = st.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return rs; }
public int NumQuestions(int quizNum) { // return the number of questions there are in a particular quiz int numQuestions = 0; try { DatabaseConnect.init(); Connection conn = DatabaseConnect.getConnection(); Statement stmt = conn.createStatement(); ResultSet result = stmt.executeQuery("SELECT * FROM QUIZ_QUESTIONS WHERE QUIZ_NUMBER=" + quizNum + ""); if (result != null) { while (result.next()) { numQuestions++; } } stmt.close(); conn.close(); result.close(); } // end try catch (Exception ex) { System.err.print("SQLException: "); System.err.println(ex.getMessage()); } return numQuestions; }