No value specified for parameter 1
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);
}
}