- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我制作了一个简单的科学测验 java 应用程序,它有 4 个框架:登录、菜单、测试和结果。
前三帧工作正常,但当我点击显示结果时显示错误。这是我来自测试 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);
}
});
}
最佳答案
您之前没有检查结果集是否为空。尝试像这样使用 while
循环:
While(rs.next()){
max = rs.getInt(1);
...
关于java - :Illegal operation on empty result set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34998884/
我有点困惑为什么在 RoutineRetrieved 函数中分配 ACTIVITYIMAGE 时使用 result.getInt(2) 并在分配 SLOT 时使用 result.getInt(3)..
我是android领域的新手,我想从响应json数组访问每个结果元素,但我无法做到这一点,我试图获取每个元素,但我只得到一个值“rohit1”是第一个元素。请帮助我。 我是 rohit parmar,
我只有从 sql 查询返回的一行 (count(*)),但在执行包时,它向我显示上述错误,并且包失败。 我将结果类型设置为“单行”,并将查询的输出(select count(*) as 'result
我正在尝试使用Diesel将简单的原始SQL转换为MySQL,如本示例所示: https://docs.diesel.rs/diesel/fn.sql_query.html let users = s
我正在尝试 Play 框架的第一个示例,但出现了此错误 在我的路线文件中: # API # ~~~~ GET /api/geotweets/index controllers.api.GeoTw
这段代码可以返回null吗? (this.Result == Result.OK) 此行(或类似行)是否可以返回除 true 或 false 之外的任何内容(例如 null)? 最佳答案 (this.
我有一个 SSIS 执行 SQL 任务。它返回一个完整的结果集(即一个表)。但是,当我执行包时出现以下错误。我已经正确地为返回的结果集命名。 [执行 SQL 任务] 错误:对于完整的结果集和 XML
最近我刚刚将 swift 2.3 项目转换为 3.2,alamofire 也得到了转换,我收到了很多问题,其中大部分都已解决,现在我被给定的两个问题所困扰 问题在 alamofire 的 Respon
我在 R 中收到以下错误消息: Error in .verify.JDBC.result(r, "Unable to retrieve JDBC result set", : Unable to r
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。
我正在使用一个简单的命令运行以下存储过程sp_MSforeachdb。我的问题是如何限制结果仅显示至少有 1 条记录满足命令的数据库: 这是我的存储过程: EXECUTE master.sys.sp_
我在单独的线程中运行一些代码: fn f1() -> Result { Err("err1".to_string()) } fn f2() -> Result { Err("err2"
我在这里尝试使用 AsyncTask 从 OWM API 显示 7 天的天气预报。 doInBackground(String...param) 方法也工作正常。我检查了 LOGCAT。 异步完成执行
我已经创建了mysql的索引和全文索引,后端存储引擎是MyISAM。 mysql> show index from play_movie; +------------+------------+---
我有一个表articles,它的结构如下: id, name, date, contents 我有一个表authors,它的结构如下: id, article_id, name, email 一篇文章
我很困惑我们是否应该创建单独的 API 来获取结果和结果计数,或者我们应该只根据结果 API 中的查询字符串来获取计数。 /api/results/ : Fetches all records /ap
我正在制作一个将两个数字相除的系统,如果第二个数字不存在,它将选择第一个数字。这是代码: let new_num: f32 = match num1/num2 { Ok(num) => n
这个问题在这里已经有了答案: Why am I getting "unused Result which must be used ... Result may be an Err variant,
我正在修改 the texture synth 中的示例之一项目: use texture_synthesis as ts; fn main() -> Result { //create a
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: ^ operator in java 我假设 c ^ d是一个类似“的幂”的计算,所以c = 5 , d = 2 ,
我是一名优秀的程序员,十分优秀!