java.sql.SQLException:无限循环
java.sql.SQLException:with infinite loop
在我的“学生考勤管理系统”项目中,在给出主题名称和出现的卷号后,我的项目开始向给定的主题无限添加“1”,并且还显示错误:
java.sql.SQLException: Before start of result set
这是我的 Teacher_Window.java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
public class Teacher_Window extends javax.swing.JFrame {
private static final long serialVersionUID=1L;
public Teacher_Window() throws SQLException {
initComponents();
populate_attendance_table(); //method definition is written below
}
@SuppressWarnings({ "unchecked", "serial" })
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
class_attendance = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
update_attendance_btn = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Attendance Card");
setAutoRequestFocus(false);
jPanel1.setBackground(new java.awt.Color(102, 153, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Attendance Card", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 24), new java.awt.Color(255, 255, 255))); // NOI18N
class_attendance.setBackground(new java.awt.Color(204, 255, 255));
class_attendance.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Roll_no", "Name", "Tamil Attended", "Tamil Total", "English Attended", "English Total", "Maths Attended", "Maths Total","Science Attended","Science Total","Social Attended","Social Total", "Attendance(%)"
}
) {
@SuppressWarnings("rawtypes")
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class,java.lang.Integer.class,java.lang.Integer.class,java.lang.Integer.class,java.lang.Integer.class, java.lang.Float.class
};
@SuppressWarnings("rawtypes")
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane1.setViewportView(class_attendance);
jPanel2.setBackground(new java.awt.Color(153, 153, 255));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "----------", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 13), new java.awt.Color(255, 255, 255))); // NOI18N
update_attendance_btn.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
update_attendance_btn.setText("Update Attendance");
update_attendance_btn.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
update_attendance_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
update_attendance_btnActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jButton2.setText("Get Student Details");
jButton2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jButton2.setPreferredSize(new java.awt.Dimension(153, 27));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(257, Short.MAX_VALUE)
.addComponent(update_attendance_btn, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE)
.addGap(229)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 222, GroupLayout.PREFERRED_SIZE)
.addGap(272))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(72)
.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
.addComponent(update_attendance_btn, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
.addContainerGap(59, Short.MAX_VALUE))
);
jPanel2.setLayout(jPanel2Layout);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
public void populate_attendance_table() throws SQLException //to diplay attendance table contents in jTable
{
Connection conn=null;
try {
//DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
DefaultTableModel dtm = (DefaultTableModel) class_attendance.getModel();
//setRowColour(1, Color.YELLOW);
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/attendance","root","happy");
Statement stmt=conn.createStatement();
int rowCount = dtm.getRowCount();
//clear the class_attendance table before filling it everytime
for (int i = rowCount - 1; i >= 0; i--) {
dtm.removeRow(i);
}
ResultSet rs=stmt.executeQuery("select a.Roll_no,sd.name,a.tamil,a.t,a.english,a.e,a.maths,a.m,a.science,a.s,a.social,a.so,a.Attendance from attendance a,details sd WHERE a.Roll_no=sd.rollno order by a.Roll_no");
//testResultSet(rs);
ResultSetMetaData meta = rs.getMetaData();
int numberOfColumns;
numberOfColumns = meta.getColumnCount();
while(rs.next()){
Object [] rowData = new Object[numberOfColumns];
for (int i = 0; i < rowData.length; ++i)
{
rowData[i] = rs.getObject(i+1);
if(i==rowData.length-1) //when last column i.e. attendaance column calculate attendance and update
{
int total_attended=((int)rowData[2]+(int)rowData[4]+(int)rowData[6]+(int)rowData[8]+(int)rowData[10]); //subject1+subject2+subject3
int total_lectures=((int)rowData[3]+(int)rowData[5]+(int)rowData[7]+(int)rowData[9]+(int)rowData[11]); //S1+S2+S3
//JOptionPane.showMessageDialog(null,""+total_attended+" "+total_lectures,"", JOptionPane.ERROR_MESSAGE);
float result=((float)total_attended/(float)total_lectures)*100;
rowData[i]=(float)(result); //Attendance=(Subject1+Subject2+Subject3)/(S1+S2+S3);
// JOptionPane.showMessageDialog(null,total_attended+" "+total_lectures+" "+result,"", JOptionPane.ERROR_MESSAGE);
}
//JOptionPane.showMessageDialog(null,""+rowData[i],"", JOptionPane.ERROR_MESSAGE);
}
dtm.addRow(rowData);
//dtm.addRow(new Object[]{"Column 1", "Column 2", "Column 3","Column 1", "Column 2", "Column 3"});
dtm.fireTableDataChanged();
//dtm.fireTableStructureChanged();
}
} catch (SQLException ex) {
Logger.getLogger(attendance.class.getName()).log(Level.SEVERE, null, ex);
}
conn.close();
}
private void update_attendance_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_update_attendance_btnActionPerformed
// TODO add your handling code here:
String subject_name= JOptionPane.showInputDialog("Enter the subject name: ");
String rollnos = JOptionPane.showInputDialog("Enter the roll numbers of the present students: ");
String subject_present=null;
if(subject_name.equalsIgnoreCase("Tamil"))
{
subject_present="t";
}
else if(subject_name.equalsIgnoreCase("English"))
{
subject_present="e";
}
else if(subject_name.equalsIgnoreCase("Maths"))
{
subject_present="m";
}
else if(subject_name.equalsIgnoreCase("Science"))
{
subject_present="s";
}
else if(subject_name.equalsIgnoreCase("Social"))
{
subject_present="so";
}
else
{
JOptionPane.showMessageDialog(null, "Invalid subject name!","", JOptionPane.ERROR_MESSAGE);
try {
new Teacher_Window().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
}
int cnt=0;
String[] rno = rollnos.split(","); // to split the roll no list
Connection conn=null;
Statement stmt=null;
while(cnt<rno.length)
{
//System.out.println(rno[cnt++]);
try {
//DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
DefaultTableModel dtm = (DefaultTableModel) class_attendance.getModel();
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/attendance","root","happy");
stmt=conn.createStatement();
//int sub_name=Integer.parseInt(subject_name);
//sub_name++;
//subject_name=""+sub_name;
//System.out.println("Updated Subject count:"+subject_name);
stmt.executeUpdate("update attendance set "+subject_name+"="+subject_name+"+1"+" where Roll_no="+rno[cnt]); //increment total attended lecture count of that subject
//calculate updated attendance percentage
ResultSet rs= stmt.executeQuery("select * from attendance where Roll_no="+rno[cnt]);
int tamil=(int)rs.getInt(2);
int tamil_total=(int)rs.getInt(3);
int english=(int)rs.getInt(4);
int english_total=(int)rs.getInt(5);
int maths=(int)rs.getInt(6);
int maths_total=(int)rs.getInt(7);
int sci=(int)rs.getInt(8);
int sci_total=(int)rs.getInt(9);
int soc=(int)rs.getInt(10);
int soc_total=(int)rs.getInt(11);
int attended=tamil+english+maths+sci+soc;
int total=tamil_total+english_total+maths_total+sci_total+soc_total+1; //add 1 because attended subject's total is not incremented here so we need to consider it as well
System.out.println(tamil+"\t"+tamil_total+"\n"+english+"\t"+english_total+"\n"+maths+"\t"+maths_total+"\n"+sci+"\t"+sci_total+"\n"+soc+"\t"+soc_total);
float percentage=((float)attended/(float)total)*100;
System.out.println("Percentage:"+percentage);
stmt.executeUpdate("update attendance set Attendance="+percentage+" where Roll_no="+rno[cnt]); //update attendance %
cnt++;
}
catch (SQLException ex) {
Logger.getLogger(attendance.class.getName()).log(Level.SEVERE, null, ex);
}
}//end of while
try {
stmt.executeUpdate("update attendance set "+subject_present+"="+subject_present+"+1"); //increment total lecture count of that subject
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
try {
//new Teacher_Window().setVisible(true);
populate_attendance_table();
JOptionPane.showMessageDialog(null,"Attendance updated successfully!","", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_update_attendance_btnActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
new ViewStudent().setVisible(true); //goto student details tab
}//GEN-LAST:event_jButton2ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Teacher_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Teacher_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Teacher_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Teacher_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Teacher_Window().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTable class_attendance;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton update_attendance_btn;
}
错误是:
java.sql.SQLException: Before start of result set
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:517)
at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1314)
at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:822)
at Teacher_Window.update_attendance_btnActionPerformed(Teacher_Window.java:273)
at Teacher_Window.actionPerformed(Teacher_Window.java:88)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
就这样一直走下去
在 update_attendance_btnActionPerformed
中,您甚至在调用 rs.next()
.
之前就直接从 ResultSet
访问数据
//calculate updated attendance percentage
ResultSet rs= stmt.executeQuery("select * from attendance where Roll_no="+rno[cnt]);
int tamil=(int)rs.getInt(2);
你应该拥有的是:
ResultSet rs= stmt.executeQuery("select * from attendance where Roll_no="+rno[cnt]);
while(rs.next()){
int tamil=(int)rs.getInt(2);
...
}
在我的“学生考勤管理系统”项目中,在给出主题名称和出现的卷号后,我的项目开始向给定的主题无限添加“1”,并且还显示错误:
java.sql.SQLException: Before start of result set
这是我的 Teacher_Window.java:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
public class Teacher_Window extends javax.swing.JFrame {
private static final long serialVersionUID=1L;
public Teacher_Window() throws SQLException {
initComponents();
populate_attendance_table(); //method definition is written below
}
@SuppressWarnings({ "unchecked", "serial" })
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
class_attendance = new javax.swing.JTable();
jPanel2 = new javax.swing.JPanel();
update_attendance_btn = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Attendance Card");
setAutoRequestFocus(false);
jPanel1.setBackground(new java.awt.Color(102, 153, 255));
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Attendance Card", javax.swing.border.TitledBorder.LEFT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 24), new java.awt.Color(255, 255, 255))); // NOI18N
class_attendance.setBackground(new java.awt.Color(204, 255, 255));
class_attendance.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Roll_no", "Name", "Tamil Attended", "Tamil Total", "English Attended", "English Total", "Maths Attended", "Maths Total","Science Attended","Science Total","Social Attended","Social Total", "Attendance(%)"
}
) {
@SuppressWarnings("rawtypes")
Class[] types = new Class [] {
java.lang.Integer.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class,java.lang.Integer.class,java.lang.Integer.class,java.lang.Integer.class,java.lang.Integer.class, java.lang.Float.class
};
@SuppressWarnings("rawtypes")
public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
});
jScrollPane1.setViewportView(class_attendance);
jPanel2.setBackground(new java.awt.Color(153, 153, 255));
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "----------", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 13), new java.awt.Color(255, 255, 255))); // NOI18N
update_attendance_btn.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
update_attendance_btn.setText("Update Attendance");
update_attendance_btn.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
update_attendance_btn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
update_attendance_btnActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
jButton2.setText("Get Student Details");
jButton2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
jButton2.setPreferredSize(new java.awt.Dimension(153, 27));
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(257, Short.MAX_VALUE)
.addComponent(update_attendance_btn, GroupLayout.PREFERRED_SIZE, 217, GroupLayout.PREFERRED_SIZE)
.addGap(229)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 222, GroupLayout.PREFERRED_SIZE)
.addGap(272))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(72)
.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
.addComponent(jButton2, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
.addComponent(update_attendance_btn, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
.addContainerGap(59, Short.MAX_VALUE))
);
jPanel2.setLayout(jPanel2Layout);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
.addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 482, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}// </editor-fold>//GEN-END:initComponents
public void populate_attendance_table() throws SQLException //to diplay attendance table contents in jTable
{
Connection conn=null;
try {
//DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
DefaultTableModel dtm = (DefaultTableModel) class_attendance.getModel();
//setRowColour(1, Color.YELLOW);
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/attendance","root","happy");
Statement stmt=conn.createStatement();
int rowCount = dtm.getRowCount();
//clear the class_attendance table before filling it everytime
for (int i = rowCount - 1; i >= 0; i--) {
dtm.removeRow(i);
}
ResultSet rs=stmt.executeQuery("select a.Roll_no,sd.name,a.tamil,a.t,a.english,a.e,a.maths,a.m,a.science,a.s,a.social,a.so,a.Attendance from attendance a,details sd WHERE a.Roll_no=sd.rollno order by a.Roll_no");
//testResultSet(rs);
ResultSetMetaData meta = rs.getMetaData();
int numberOfColumns;
numberOfColumns = meta.getColumnCount();
while(rs.next()){
Object [] rowData = new Object[numberOfColumns];
for (int i = 0; i < rowData.length; ++i)
{
rowData[i] = rs.getObject(i+1);
if(i==rowData.length-1) //when last column i.e. attendaance column calculate attendance and update
{
int total_attended=((int)rowData[2]+(int)rowData[4]+(int)rowData[6]+(int)rowData[8]+(int)rowData[10]); //subject1+subject2+subject3
int total_lectures=((int)rowData[3]+(int)rowData[5]+(int)rowData[7]+(int)rowData[9]+(int)rowData[11]); //S1+S2+S3
//JOptionPane.showMessageDialog(null,""+total_attended+" "+total_lectures,"", JOptionPane.ERROR_MESSAGE);
float result=((float)total_attended/(float)total_lectures)*100;
rowData[i]=(float)(result); //Attendance=(Subject1+Subject2+Subject3)/(S1+S2+S3);
// JOptionPane.showMessageDialog(null,total_attended+" "+total_lectures+" "+result,"", JOptionPane.ERROR_MESSAGE);
}
//JOptionPane.showMessageDialog(null,""+rowData[i],"", JOptionPane.ERROR_MESSAGE);
}
dtm.addRow(rowData);
//dtm.addRow(new Object[]{"Column 1", "Column 2", "Column 3","Column 1", "Column 2", "Column 3"});
dtm.fireTableDataChanged();
//dtm.fireTableStructureChanged();
}
} catch (SQLException ex) {
Logger.getLogger(attendance.class.getName()).log(Level.SEVERE, null, ex);
}
conn.close();
}
private void update_attendance_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_update_attendance_btnActionPerformed
// TODO add your handling code here:
String subject_name= JOptionPane.showInputDialog("Enter the subject name: ");
String rollnos = JOptionPane.showInputDialog("Enter the roll numbers of the present students: ");
String subject_present=null;
if(subject_name.equalsIgnoreCase("Tamil"))
{
subject_present="t";
}
else if(subject_name.equalsIgnoreCase("English"))
{
subject_present="e";
}
else if(subject_name.equalsIgnoreCase("Maths"))
{
subject_present="m";
}
else if(subject_name.equalsIgnoreCase("Science"))
{
subject_present="s";
}
else if(subject_name.equalsIgnoreCase("Social"))
{
subject_present="so";
}
else
{
JOptionPane.showMessageDialog(null, "Invalid subject name!","", JOptionPane.ERROR_MESSAGE);
try {
new Teacher_Window().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
}
int cnt=0;
String[] rno = rollnos.split(","); // to split the roll no list
Connection conn=null;
Statement stmt=null;
while(cnt<rno.length)
{
//System.out.println(rno[cnt++]);
try {
//DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
DefaultTableModel dtm = (DefaultTableModel) class_attendance.getModel();
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/attendance","root","happy");
stmt=conn.createStatement();
//int sub_name=Integer.parseInt(subject_name);
//sub_name++;
//subject_name=""+sub_name;
//System.out.println("Updated Subject count:"+subject_name);
stmt.executeUpdate("update attendance set "+subject_name+"="+subject_name+"+1"+" where Roll_no="+rno[cnt]); //increment total attended lecture count of that subject
//calculate updated attendance percentage
ResultSet rs= stmt.executeQuery("select * from attendance where Roll_no="+rno[cnt]);
int tamil=(int)rs.getInt(2);
int tamil_total=(int)rs.getInt(3);
int english=(int)rs.getInt(4);
int english_total=(int)rs.getInt(5);
int maths=(int)rs.getInt(6);
int maths_total=(int)rs.getInt(7);
int sci=(int)rs.getInt(8);
int sci_total=(int)rs.getInt(9);
int soc=(int)rs.getInt(10);
int soc_total=(int)rs.getInt(11);
int attended=tamil+english+maths+sci+soc;
int total=tamil_total+english_total+maths_total+sci_total+soc_total+1; //add 1 because attended subject's total is not incremented here so we need to consider it as well
System.out.println(tamil+"\t"+tamil_total+"\n"+english+"\t"+english_total+"\n"+maths+"\t"+maths_total+"\n"+sci+"\t"+sci_total+"\n"+soc+"\t"+soc_total);
float percentage=((float)attended/(float)total)*100;
System.out.println("Percentage:"+percentage);
stmt.executeUpdate("update attendance set Attendance="+percentage+" where Roll_no="+rno[cnt]); //update attendance %
cnt++;
}
catch (SQLException ex) {
Logger.getLogger(attendance.class.getName()).log(Level.SEVERE, null, ex);
}
}//end of while
try {
stmt.executeUpdate("update attendance set "+subject_present+"="+subject_present+"+1"); //increment total lecture count of that subject
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
try {
//new Teacher_Window().setVisible(true);
populate_attendance_table();
JOptionPane.showMessageDialog(null,"Attendance updated successfully!","", JOptionPane.INFORMATION_MESSAGE);
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
}//GEN-LAST:event_update_attendance_btnActionPerformed
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
new ViewStudent().setVisible(true); //goto student details tab
}//GEN-LAST:event_jButton2ActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Teacher_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Teacher_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Teacher_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Teacher_Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new Teacher_Window().setVisible(true);
} catch (SQLException ex) {
Logger.getLogger(Teacher_Window.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTable class_attendance;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton update_attendance_btn;
}
错误是:
java.sql.SQLException: Before start of result set
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.result.ResultSetImpl.checkRowPos(ResultSetImpl.java:517)
at com.mysql.cj.jdbc.result.ResultSetImpl.getObject(ResultSetImpl.java:1314)
at com.mysql.cj.jdbc.result.ResultSetImpl.getInt(ResultSetImpl.java:822)
at Teacher_Window.update_attendance_btnActionPerformed(Teacher_Window.java:273)
at Teacher_Window.actionPerformed(Teacher_Window.java:88)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
就这样一直走下去
在 update_attendance_btnActionPerformed
中,您甚至在调用 rs.next()
.
ResultSet
访问数据
//calculate updated attendance percentage
ResultSet rs= stmt.executeQuery("select * from attendance where Roll_no="+rno[cnt]);
int tamil=(int)rs.getInt(2);
你应该拥有的是:
ResultSet rs= stmt.executeQuery("select * from attendance where Roll_no="+rno[cnt]);
while(rs.next()){
int tamil=(int)rs.getInt(2);
...
}