更新MySQL数据库( java.sql.SQLException: No value specified for parameter 1) 异常 解决方法
2018-01-01 00:37
836 查看
package com.swift; import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; import com.google.gson.Gson; public class UpdateUrl { public static void main(String[] args) { File file = new File("D:\\java_date", "questionJson.json"); String jsonResult = GetData.getData(file); Gson gson = new Gson(); Root root = gson.fromJson(jsonResult, Root.class); List<Result> result = root.getResult(); updateUrl(result); } private static void updateUrl(List<Result> result) { Connection conn = DBUtil.getConn(); PreparedStatement ps = null; try { ps = conn.prepareStatement("update sw_question set url = ? where id=?)"); for (int i = 0; i < result.size(); i++) { String str=result.get(i).getUrl(); int id=result.get(i).getId(); if (str != null && str.length() > 0) { ps.setString(1, str.substring(str.lastIndexOf("/")+1));//图片的URL地址截图成图片名称后更新数据库 ps.setInt(2, id); } ps.executeUpdate(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.closeAll(conn, ps, null); } } }
更新数据库url字段出错
修改代码如下:
package com.swift; import java.io.File; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; import com.google.gson.Gson; public class UpdateUrl { public static void main(String[] args) { File file = new File("D:\\java_date", "questionJson.json"); String jsonResult = GetData.getData(file); Gson gson = new Gson(); Root root = gson.fromJson(jsonResult, Root.class); List<Result> result = root.getResult(); updateUrl(result); } private static void updateUrl(List<Result> result) { Connection conn = DBUtil.getConn(); PreparedStatement ps = null; try { ps = conn.prepareStatement("update sw_question set url = ? where id=?"); for (int i = 0; i < result.size(); i++) { String str=result.get(i).getUrl(); System.out.println(str); int id=result.get(i).getId(); if (str != null && str.length() > 0) { int number=str.lastIndexOf("/")+1; System.out.println(number); String change=str.substring(number); System.out.println(change); ps.setString(1, change); ps.setInt(2, id); ps.addBatch(); } } ps.executeBatch(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.closeAll(conn, ps, null); } } }
相关文章推荐
- java.sql.SQLException: No value specified for parameter 16
- Caused by: java.sql.SQLException: No value specified for parameter 2
- mybatis TypeHandler java.sql.SQLException: No value specified for parameter
- Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法
- no value specified for 'date' 的解决方法
- java.sql.SQLException: The value supplied cannot be converted to DECIMAL 解决方法
- Tomcat 部署 时 java.sql.SQLException: No suitable driver found for jdbc:sqlserver 我是怎么解决的
- Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法
- java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)的一种解决方法
- 关于 java.sql.SQLException: Bad format for Date错误的解决方法
- Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法
- 解决java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket这个异常
- 解决java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket这个异常
- 数据库连接时一个异常的处理:java.sql.SQLException: No suitable driver found for
- MySql系列:中文写入数据库出现错误java.sql.SQLException: Incorrect string value: '\xE5\xxxx' for column 'xxxx' at row 1及其解决方法
- Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法
- 解决java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket这个异常
- Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法
- java.sql.SQLException: Value '0000-00-00' can not be represented as java.sql.Date 解决方法
- Oracle OEM 配置报错: No value was set for the parameter DBCONTROL_HTTP_PORT 解决方法