java.sql.SQLException: No value specified for parameter 1要怎么解决?
题目描述:我在用eclipse编写java代码时,出现java.sql.SQLException: No value specified for parameter 1这样的错误,要怎么解决?
题目来源及自己的思路
相关代码1.报错的代码:java.sql.SQLException: No value specified for parameter 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2256)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2236)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2166)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1986)
at com.cn.util.DBHelper.executeQuery(DBHelper.java:306)
at com.cn.dao.daoImpl.RoomDaoImpl.Money(RoomDaoImpl.java:56)
at com.cn.service.serviceImpl.RoomServiceImpl.Money(RoomServiceImpl.java:72)
at com.cn.view.OpenFrame$4.mouseClicked(OpenFrame.java:206)
at java.desktop/java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:278)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6620)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
at java.desktop/java.awt.Component.processEvent(Component.java:6382)
at java.desktop/java.awt.Container.processEvent(Container.java:2264)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4993)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2322)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4934)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4572)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4504)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2308)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2773)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4825)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
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)
粘贴代码文本(请勿用截图)2.出现错误的地方:
public static <T> List<T> executeQuery(String sql,List<Object> params,Class<T> cls) throws Exception{
Connection conn=null; PreparedStatement pstmt = null;
ResultSet rs=null;
List<T> data=new ArrayList<T>();
try {
conn=getConnection();
pstmt=conn.prepareStatement(sql);//装载sql语句
if(params!=null){
//加入有?占位符,在执行之前把?占位符替换掉
for(int i=0;i<params.size();i++){
pstmt.setObject(i+1, params.get(i));
}
}
rs=pstmt.executeQuery();
//把查询出来的记录封装成对应的实体类对象
ResultSetMetaData rsd=rs.getMetaData();//获得列对象,通过此对象可以得到表的结构,包括,列名,列的个数,列的数据类型
while(rs.next()){
T m=cls.newInstance();
for(int i=0;i<rsd.getColumnCount();i++){
String col_name=rsd.getColumnName(i+1);//获得列名
Object value=rs.getObject(col_name);//获得列所对应的值
Field field=cls.getDeclaredField(col_name);
field.setAccessible(true);//给私有属性设置可访问权
field.set(m, value);//给对象的私有属性赋值
System.out.print("标准房间"+rsd.getColumnName(i+1));
}
data.add(m);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
closeDB();
}
return data;
}
你期待的结果是什么?实际看到的错误信息又是什么?
回答:
就是 SQL 中的参数没有传值。
回答:
调用时入参传的是什么,sql中占位符数量与param数量是否能一一对应匹配上
以上是 java.sql.SQLException: No value specified for parameter 1要怎么解决? 的全部内容, 来源链接: utcz.com/p/944576.html