JDBC API报错:ResultSet is from UPDATE. No Data

用了框架多了之后,JDBC API有点遗忘,作下笔记避免再次掉坑

昨天在调用存储mysql的存储过程的时候,没有“select”返回结果集,然后报了这么一个错误(如果返回结果集就不会报错),一开始感觉莫名奇妙的。然后就开始baidu一下错误,发现国内的论坛或博客上对这个问题都是描述个现象(那个现象我也知道呀
(┬_┬)),基本没有解决方案。类似以下回答:

————————————————————————————————
这里写图片描述

————————————————————————————————
最后,去stackoverflow上搜索一下:
http://stackoverflow.com/questions/32682916/mysql-java-sql-sqlexception-resultset-is-from-update-no-data
这里写图片描述

调用存储过程的时候,要用execute(),不能用executeQuery(),突然想起之前学习JDBC的时候,只会用executeQuery做查询,怎么昨天就用来调用存储过程了呢?久不使用就会生疏呀~~

那为什么使用executeQuery(),没有返回结果集,不是报空指针异常,而是报上面那个错呢?
API文档里面有说明:
这里写图片描述
所以说,用executeQuery()始终会返回ResultSet对象,而不会返回null。

如果我们确切的知道要使用查询语句的时候,我们可以直接使用executeQuery();

如果是调用存储过程的话,就使用execute()。然后通过以下方法得到ResultSet:getResultSet();
这里写图片描述
然后判断ResultSet是否为null,不为null才可以执行ResultSet.next(),否则不执行。

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据以下要求:Instead of using a binary file to save the arraylist of points, change the savaData method and the constructor of the Model class to use a database to write / read the coordinates of all the points. Use XAMPP and phpMyAdmin to create a database called "java" with a table called "points" that has two integer columns x and y (in addition to the ID primary key). Hint: make sure you delete all the old point coordinates from the database before inserting new ones. Hint: use phpMyAdmin to check what is stored in the database。修改下述代码:public class Model implements Serializable { private ArrayList<Point> points; private ArrayList<ModelListener> listeners; private static final String FILE_NAME = "points.bin"; public Model() { points = new ArrayList<Point>(); listeners = new ArrayList<ModelListener>(); // Read points from file if it exists File file = new File(FILE_NAME); if (file.exists()) { try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); points = (ArrayList<Point>) in.readObject(); in.close(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } public void addListener(ModelListener l) { listeners.add(l); } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { points.add(p); notifyListeners(); // points changed so notify the listeners. saveData(); // save point to file } public void clearAllPoints() { points.clear(); notifyListeners(); // points changed so notify the listeners. saveData(); // save empty list to file } public void deleteLastPoint() { if (points.size() > 0) { points.remove(points.size() - 1); notifyListeners(); // points changed so notify the listeners. saveData(); // save updated list to file } } private void notifyListeners() { for (ModelListener l : listeners) { l.update(); // Tell the listener that something changed. } } public int numberOfPoints() { return points.size(); } public void saveData() { try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(FILE_NAME)); out.writeObject(points); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
05-25
修改后的代码如下所示,主要是将数据存储方式从二进制文件改为了使用数据库。具体实现是创建了一个名为"java"的数据库,并在其中创建了一个名为"points"的表,该表有三个列,分别是ID、x和y,其中ID是主键。在saveData()方法中,使用JDBC连接数据库,并将所有点的坐标存储到"points"表中。在Model类的构造函数中,读取数据库中保存的所有点的坐标,并将其存储在points列表中。 ``` import java.sql.*; import java.util.ArrayList; public class Model implements Serializable { private ArrayList<Point> points; private ArrayList<ModelListener> listeners; private static final String DB_URL = "jdbc:mysql://localhost:3306/java"; private static final String DB_USER = "root"; private static final String DB_PASSWORD = "password"; public Model() { points = new ArrayList<Point>(); listeners = new ArrayList<ModelListener>(); // Read points from database try { Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM points"); while (rs.next()) { int x = rs.getInt("x"); int y = rs.getInt("y"); points.add(new Point(x, y)); } rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } public void addListener(ModelListener l) { listeners.add(l); } public ArrayList<Point> getPoints() { return points; } public void addPoint(Point p) { points.add(p); notifyListeners(); // points changed so notify the listeners. saveData(); // save point to database } public void clearAllPoints() { points.clear(); notifyListeners(); // points changed so notify the listeners. saveData(); // save empty list to database } public void deleteLastPoint() { if (points.size() > 0) { points.remove(points.size() - 1); notifyListeners(); // points changed so notify the listeners. saveData(); // save updated list to database } } private void notifyListeners() { for (ModelListener l : listeners) { l.update(); // Tell the listener that something changed. } } public int numberOfPoints() { return points.size(); } public void saveData() { try { Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD); Statement stmt = conn.createStatement(); stmt.executeUpdate("DELETE FROM points"); for (Point p : points) { stmt.executeUpdate("INSERT INTO points (x, y) VALUES (" + p.x + ", " + p.y + ")"); } stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值