我制作了一个简单的科学测验java应用程序,包含4个框架:登录,菜单,测试和结果。
前三帧工作正常但是当我点击显示结果时它显示错误。这是我在Test JFrame中的代码。如果你想我也可以发送其他帧的编码
这是我的错误
java.sql.SQLException: Illegal operation on empty result set. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926) at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815) at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528) at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448) at com.mysql.jdbc.ResultSetImpl.getFloat(ResultSetImpl.java:2499) at Test.ResultBTNActionPerformed(Test.java:292) at Test.access$500(Test.java:18) at Test$6.actionPerformed(Test.java:215) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6216) at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) at java.awt.Component.processEvent(Component.java:5981) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4583) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4413) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4556) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4220) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4150) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2475) at java.awt.Component.dispatchEvent(Component.java:4413) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
测试JFrame编码
import java.sql.*;
import javax.swing.JOptionPane;
public class Test extends javax.swing.JFrame {
String ID;
String Subject;
int index =1;
int max =0;
int result = 0;
char [] answers;
/** Creates new form Test */
public Test()
{
initComponents();
}
public Test(String subject, String id)
{
initComponents();
ID = id;
Subject = subject;
PrevBTN.setVisible(false);
try
{
Class.forName("java.sql.Driver");
String database = "jdbc:mysql://localhost:3306/quizdb";
Connection conn = DriverManager.getConnection(database, "root", "1234");
Statement stmt = conn.createStatement();
String sql = "select max(SNo) from " + Subject;
ResultSet rs = stmt.executeQuery(sql);
rs.next();
max = rs.getInt(1);
answers = new char[max];
for(int i=0; i<max;i++)
answers[i] = 'e';
rs.close();
stmt.close();
conn.close();
getQues();
}
catch(Exception e){ e.printStackTrace();
}
}
private void getQues()
{
try
{
Class.forName("java.sql.Driver");
String database = "jdbc:mysql://localhost:3306/quizdb";
Connection conn = DriverManager.getConnection(database, "root", "1234");
Statement stmt = conn.createStatement();
String sql = "select * from " + Subject + " where SNo = " + index;
ResultSet rs = stmt.executeQuery(sql);
rs.next();
QuesTA.setText("\nQ" + index + ". " + rs.getString(2));
a.setText(rs.getString("a"));
b.setText(rs.getString("b"));
c.setText(rs.getString("c"));
d.setText(rs.getString("d"));
a.setSelected(answers[index-1] == 'a');
b.setSelected(answers[index-1] == 'b');
c.setSelected(answers[index-1] == 'c');
d.setSelected(answers[index-1] == 'd');
e.setSelected(answers[index-1] == 'e');
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){ e.printStackTrace(); }
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
jLabel2 = new javax.swing.JLabel();
a = new javax.swing.JRadioButton();
b = new javax.swing.JRadioButton();
c = new javax.swing.JRadioButton();
jScrollPane1 = new javax.swing.JScrollPane();
QuesTA = new javax.swing.JTextArea();
PrevBTN = new javax.swing.JButton();
NextBTN = new javax.swing.JButton();
ResultBTN = new javax.swing.JButton();
d = new javax.swing.JRadioButton();
jLabel1 = new javax.swing.JLabel();
e = new javax.swing.JRadioButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Quiz Time");
setResizable(false);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jLabel2.setFont(new java.awt.Font("Castellar", 1, 36));
jLabel2.setForeground(new java.awt.Color(255, 0, 51));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("SCIENCE QUIZ");
getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(111, 20, 360, -1));
buttonGroup1.add(a);
a.setFont(new java.awt.Font("Tahoma", 1, 14));
a.setForeground(new java.awt.Color(102, 0, 102));
a.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
a.setOpaque(false);
a.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aActionPerformed(evt);
}
});
getContentPane().add(a, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 210, -1, -1));
buttonGroup1.add(b);
b.setFont(new java.awt.Font("Tahoma", 1, 14));
b.setForeground(new java.awt.Color(102, 0, 102));
b.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
b.setOpaque(false);
b.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bActionPerformed(evt);
}
});
getContentPane().add(b, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 240, -1, -1));
buttonGroup1.add(c);
c.setFont(new java.awt.Font("Tahoma", 1, 14));
c.setForeground(new java.awt.Color(102, 0, 102));
c.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
c.setOpaque(false);
c.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cActionPerformed(evt);
}
});
getContentPane().add(c, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 270, -1, -1));
jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
jScrollPane1.setAlignmentX(5.0F);
jScrollPane1.setAlignmentY(5.0F);
QuesTA.setBackground(new java.awt.Color(248, 241, 244));
QuesTA.setColumns(20);
QuesTA.setEditable(false);
QuesTA.setFont(new java.awt.Font("Tahoma", 0, 18));
QuesTA.setForeground(new java.awt.Color(0, 153, 153));
QuesTA.setLineWrap(true);
QuesTA.setRows(3);
QuesTA.setWrapStyleWord(true);
QuesTA.setAutoscrolls(false);
QuesTA.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(204, 0, 204), new java.awt.Color(204, 0, 204), new java.awt.Color(153, 0, 153), new java.awt.Color(153, 0, 153)));
QuesTA.setSelectionColor(new java.awt.Color(255, 255, 51));
jScrollPane1.setViewportView(QuesTA);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 80, 460, 100));
PrevBTN.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
PrevBTN.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
PrevBTN.setForeground(new java.awt.Color(0, 102, 102));
PrevBTN.setText("<< Prev");
PrevBTN.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 255), new java.awt.Color(255, 204, 255), new java.awt.Color(153, 0, 153), new java.awt.Color(153, 0, 153)));
PrevBTN.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
PrevBTNActionPerformed(evt);
}
});
getContentPane().add(PrevBTN, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 370, 90, 40));
NextBTN.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
NextBTN.setFont(new java.awt.Font("Tahoma", 1, 16)); // NOI18N
NextBTN.setForeground(new java.awt.Color(0, 102, 153));
NextBTN.setText("Next >>");
NextBTN.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 255), new java.awt.Color(255, 204, 255), new java.awt.Color(153, 0, 153), new java.awt.Color(153, 0, 153)));
NextBTN.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
NextBTNActionPerformed(evt);
}
});
getContentPane().add(NextBTN, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 370, 90, 40));
ResultBTN.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.highlight"));
ResultBTN.setFont(new java.awt.Font("Castellar", 1, 16)); // NOI18N
ResultBTN.setForeground(new java.awt.Color(0, 102, 102));
ResultBTN.setText("GEt RESULT");
ResultBTN.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED, new java.awt.Color(255, 204, 255), new java.awt.Color(255, 204, 255), new java.awt.Color(153, 0, 153), new java.awt.Color(153, 0, 153)));
ResultBTN.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ResultBTNActionPerformed(evt);
}
});
getContentPane().add(ResultBTN, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 370, 140, 40));
buttonGroup1.add(d);
d.setFont(new java.awt.Font("Tahoma", 1, 14));
d.setForeground(new java.awt.Color(102, 0, 102));
d.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
d.setOpaque(false);
d.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
dActionPerformed(evt);
}
});
getContentPane().add(d, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 300, -1, -1));
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
jLabel1.setForeground(new java.awt.Color(102, 0, 102));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setOpaque(true);
getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, 430));
buttonGroup1.add(e);
getContentPane().add(e, new org.netbeans.lib.awtextra.AbsoluteConstraints(550, 450, 10, 10));
pack();
}// </editor-fold>
private void PrevBTNActionPerformed(java.awt.event.ActionEvent evt) {
index--;
getQues();
if(index == 1)
PrevBTN.setVisible(false);
if(index < max)
NextBTN.setVisible(true);
}
private void NextBTNActionPerformed(java.awt.event.ActionEvent evt) {
index++;
getQues();
if(index == max)
NextBTN.setVisible(false);
if(index > 1)
PrevBTN.setVisible(true);
}
private void bActionPerformed(java.awt.event.ActionEvent evt) {
answers[index-1] = 'b';
}
private void ResultBTNActionPerformed(java.awt.event.ActionEvent evt) {
try
{
Class.forName("java.sql.Driver");
String database = "jdbc:mysql://localhost:3306/quizdb";
Connection conn = DriverManager.getConnection(database, "root", "1234");
Statement stmt = conn.createStatement();
String sql = "select Result from " + Subject;
ResultSet rs = stmt.executeQuery(sql);
int i = 0;
while(rs.next())
{
char ans = rs.getString(1).charAt(0);
if(ans == answers[i])
result++;
i++;
}
float res = ((float)result * 100 ) / max;
sql = "Select " + Subject + "Result from Result where ID = '" + ID + "'";
rs = stmt.executeQuery(sql);
rs.next();
if(res > rs.getFloat(1))
{
sql = "update Result set " + Subject + "Result = " + res + " where ID = '" + ID + "'";
stmt.executeUpdate(sql);
}
stmt.close();
conn.close();
new Result(res,ID).setVisible(true);
this.setVisible(false);
}
catch(Exception e)
{ e.printStackTrace();
}
}
private void aActionPerformed(java.awt.event.ActionEvent evt) {
answers[index-1] = 'a';
}
private void cActionPerformed(java.awt.event.ActionEvent evt) {
answers[index-1] = 'c';
}
private void dActionPerformed(java.awt.event.ActionEvent evt) {
answers[index-1] = 'd';
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Test().setVisible(true);
}
});
}
答案 0 :(得分:2)
您之前未检查过结果集是否为空。尝试使用while
这样的循环:
While(rs.next()){
max = rs.getInt(1);
...
答案 1 :(得分:1)
而不是原始 PipelineOptions options = PipelineOptionsFactory.fromArgs(args).withValidation().create();
,您需要检查结果(如果没有更多行,则返回rs.next()
)....类似的事情,
false
答案 2 :(得分:0)
正如其他答案所述,只要rs.next()
返回true,就可以调用列getter。
在你的情况下:
int max = rs.next() ? rs.getInt(1) : 0;
float x = rs.next() ? rs.getFloat(1) : 0f;
也许SQL查询也不是最合适的。
在堆栈跟踪中,您会看到对getFloat的调用引发了错误。