No value specified for parameter 1

java_shi 2008-08-07 02:36:25
Exception in thread "main" com.witbridge.service.exception.ProductException: java.sql.SQLException: No value specified for parameter 1
at com.witbridge.service.impl.JDBCProductServiceImpl.infoById(JDBCProductServiceImpl.java:100)
at com.witbridge.service.impl.JDBCProductServiceImpl.main(JDBCProductServiceImpl.java:262)
Caused by: java.sql.SQLException: No value specified for parameter 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:1699)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:1647)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1357)
at com.witbridge.service.impl.JDBCProductServiceImpl.infoById(JDBCProductServiceImpl.java:78)
... 1 more


请高手明示,十分感谢:


package com.witbridge.service.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.witbridge.domain.Product;
import com.witbridge.service.ProductService;
import com.witbridge.service.exception.ProductException;

public class JDBCProductServiceImpl implements ProductService {



public void deleteByIds(Long[] ids) throws ProductException {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Connection connection = null; // A
PreparedStatement stmt = null; // B
try {
// A. Connection
connection = getConnection();

// B. Statement
String sql = "delete from product where id=?";
stmt = connection.prepareStatement(sql); // Create statement
for (int i = 0; i < ids.length; i++) {
Long id = ids[i];
stmt.setLong(1, id.longValue()); // Render statement
stmt.addBatch(); // Batch
}
int[] rowCounts = stmt.executeBatch();

System.out.println(rowCounts.length + " row(s) deleted");
} catch (ClassNotFoundException e) {
throw new ProductException(e);
} catch (SQLException e) {
throw new ProductException(e);
} finally {
// Release resources
close(connection, stmt);
}
}

public void deleteById(Long id) throws ProductException {
deleteByIds(new Long[] { id });
}


public void modify(Product product) throws ProductException {
String sql = "update product set name=?,types=?,price=?,origin=?,sizes=?, msg=? where id=?";

}


public Product infoById(String id) throws ProductException {
Connection connection = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{

//--A.
connection = getConnection();
//--B.
String sql = "select * from product where name=?";
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
//--C.
Product pt = new Product();
if(rs.next()){
String name = rs.getString("name");
String types = rs.getString("types");
Double price = new Double(rs.getDouble("price"));
String origin = rs.getString("origin");
String sizes = rs.getString("sizes");
String msg = rs.getString("msg");

pt.setName(name);
pt.setTypes(types);
pt.setPrice(price);
pt.setOrigin(origin);
pt.setSizes(sizes);
pt.setMsg(msg);
}return pt;

} catch (ClassNotFoundException e) {
throw new ProductException(e);
} catch (SQLException e) {
throw new ProductException(e);
}finally{

close(connection, stmt, rs);
}
}


public List list() throws ProductException {
Connection connection = null;
Statement stmt = null;
ResultSet rs = null;
try {
// A. Get connection
connection = getConnection();

// B. Create a SQL statement
String sql = "select * from product";
stmt = connection.createStatement();
// Execute the statement
rs = stmt.executeQuery(sql);

// C. Process the result set
List products = new ArrayList();
// Navigate to a certain row
while (rs.next()) {
// Get column value
Long id = new Long(rs.getLong("id"));
String name = rs.getString("name");
String types = rs.getString("types");
Double price = new Double(rs.getDouble("price"));
String origin = rs.getString("origin");
String sizes = rs.getString("sizes");
String msg = rs.getString("msg");

Product product = new Product();
product.setId(id);
product.setName(name);
product.setTypes(types);
product.setPrice(price);
product.setOrigin(origin);
product.setSizes(sizes);
product.setMsg(msg);

products.add(product);
}

return products;
} catch (ClassNotFoundException e) {
throw new ProductException(e);
} catch (SQLException e) {
throw new ProductException(e);
} finally {
// Release resources
close(connection, stmt, rs);
}
}

public void save(Product product) throws ProductException {
Connection connection = null;
PreparedStatement stmt = null;
try {
// A. Get connection
connection = getConnection();

// B. Create a SQL statement
String sql = "insert into product values (?, ?, ?, ?, ?, ?, ?)";
stmt = connection.prepareStatement(sql); // Create statement
// Render statement
stmt.setLong(1, product.getId().longValue());
stmt.setString(2, product.getName());
stmt.setString(3, product.getTypes());
stmt.setDouble(4, product.getPrice().doubleValue());
stmt.setString(5, product.getOrigin());
stmt.setString(6, product.getSizes());
stmt.setString(7, product.getMsg());
int rowCount = stmt.executeUpdate(); // Execute statement

System.out.println(rowCount + " row(s) inserted");
} catch (ClassNotFoundException e) {
throw new ProductException(e);
} catch (SQLException e) {
throw new ProductException(e);
} finally {
// Release resources
close(connection, stmt);
}
}

private Connection getConnection() throws ClassNotFoundException,
SQLException {

String driver = "com.mysql.jdbc.Driver";

String url = "jdbc:mysql://localhost/product?user=root&password=root";

Class.forName(driver);
return DriverManager.getConnection(url);
}

private void close(Connection connection, PreparedStatement stmt) {
close(connection, stmt, null);
}

private void close(Connection connection, Statement stmt, ResultSet rs) {
// C. Release the result set
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
}
// B. Release the statement
if (stmt != null)
try {
stmt.close();
} catch (SQLException e) {
}
// A. Release the connection
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
}
}



public boolean ifExist(Product product) throws SQLException {
return false;
}


public static void main(String[] args) throws ProductException {

ProductService service = new JDBCProductServiceImpl();

System.out.println(service.list().size() + " row(s) before insert");

Product product1 = new Product();

product1.setName("P006");
product1.setPrice(new Double(10));
product1.setOrigin("en");
product1.setSizes("1*2*3");
product1.setMsg("XXX");

service.infoById("P007");
service.modify(product1);

}

}

...全文
25927 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
HuaIng 2009-12-27
  • 打赏
  • 举报
回复
//--B.
String sql = "select * from product where name=?";
stmt = connection.prepareStatement(sql);
这个位置加上这一句stmt.setString(1,id(你定义的参数));rs = stmt.executeQuery();
//--C.
我也碰到过你这个问题。
不过我是把位置写错了
dryZeng 2008-08-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 java_shi 的回复:]
要如何指定呢?
[/Quote]


你源程序里不是有个例子吗?
String sql = "insert into product values (?, ?, ?, ?, ?, ?, ?)";
stmt = connection.prepareStatement(sql); // Create statement
// Render statement
stmt.setLong(1, product.getId().longValue());
stmt.setString(2, product.getName());
stmt.setString(3, product.getTypes());
stmt.setDouble(4, product.getPrice().doubleValue());
stmt.setString(5, product.getOrigin());
stmt.setString(6, product.getSizes());
stmt.setString(7, product.getMsg());
int rowCount = stmt.executeUpdate(); // Execute statement
废穴S猫 2008-08-07
  • 打赏
  • 举报
回复
请lz结贴吧。上面几个楼主已经帮你找出问题了。
废穴S猫 2008-08-07
  • 打赏
  • 举报
回复
我说话比较直接。lz你这个问题不应该发帖,你仔细看异常,就知道问题在哪儿?
Liebestraum 2008-08-07
  • 打赏
  • 举报
回复
stmt.setString(1,产品名);
java_shi 2008-08-07
  • 打赏
  • 举报
回复
要如何指定呢?
dryZeng 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Liebestraum 的回复:]
//--B.
String sql = "select * from product where name=?";
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
没有指定参数了吧
[/Quote]

支持

name=? 还没有给赋值.
cool_scorpion 2008-08-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Liebestraum 的回复:]
//--B.
String sql = "select * from product where name=?";
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
没有指定参数了吧
[/Quote]

是啊,在其他地方你都懂得 stmt.setLong(1, product.getId().longValue());

而这里怎么就没设置了呢。。。。
Liebestraum 2008-08-07
  • 打赏
  • 举报
回复
//--B.
String sql = "select * from product where name=?";
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();
没有指定参数了吧

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧