我试图创建一个从xml文件更新数据库的函数。我在数据库中的列 - 表:'提供' :id(A_I,PK),城市,国家,酒店,描述,价格,图片
我虽然只会在城市,国家和酒店相同的情况下更新优惠。否则我将插入一个新行。我首先实现了xml文件的读取,该文件正常工作。数据库连接正常,我测试了它。
我有两个问题:第一,如果我可以把它变小。第二个为什么不起作用并说:
Parameter index out of range (1 > number of parameters, which is 0).
我认为这是sql语句的问题,但我无法告诉如何解决它。 到目前为止我的代码:
package bookings1;
import java.io.File;
import java.sql.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class addOffers implements Runnable {
public void run(){
Connection connection = null;
connection = ConnectionConfiguration.getConnection();
try {
// Start getting offers from xml
File fXmlFile = new File("c:\\offers.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("offers");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String DCity = eElement.getElementsByTagName("city").item(0).getTextContent();
String DCountry = eElement.getElementsByTagName("country").item(0).getTextContent();
String DHotel = eElement.getElementsByTagName("hotel").item(0).getTextContent();
String DDescription= eElement.getElementsByTagName("description").item(0).getTextContent();
String DPrice = eElement.getElementsByTagName("price").item(0).getTextContent();
String DImage = eElement.getElementsByTagName("image").item(0).getTextContent();
PreparedStatement st = connection.prepareStatement("SELECT * FROM offers WHERE city=? AND country=? AND hotel=?");
st.setString(1, DCity);
st.setString(2, DCountry);
st.setString(3, DHotel);
ResultSet rs = st.executeQuery();
if(rs.next()) {
int theid = rs.getInt("id");
PreparedStatement preparedStatement =
connection.prepareStatement("UPDATE offers SET description=?, price=?, image=? WHERE id=?");
preparedStatement.setString(1, DDescription);
preparedStatement.setString(2, DPrice);
preparedStatement.setString(3, DImage);
preparedStatement.setInt(4, theid);
preparedStatement.executeUpdate();
}
else {
PreparedStatement preparedStatement =
connection.prepareStatement("INSERT into orders (city, country, hotel, description, image, phone) VALUES (?,?,?,?,?,?)");
preparedStatement.setString(1, DCity);
preparedStatement.setString(2, DCountry);
preparedStatement.setString(3, DHotel);
preparedStatement.setString(4, DDescription);
preparedStatement.setString(5, DPrice);
preparedStatement.setString(6, DImage);
preparedStatement.executeUpdate();
}
connection.close();
}
}
} catch (Exception e) {
System.out.println("Fetching from xml thread interrupted! " + e.getMessage());
}
}
}
答案 0 :(得分:0)
首先,随着JDBC出现了很多锅炉板代码。它永远不会短!尝试查看JPA和Hibernate以获取数据库功能吗?
其次,您的错误消息告诉您在没有定义参数时尝试插入参数。
PreparedStatement st = connection.prepareStatement("SELECT * FROM offers WHERE city='Beijing'");
st.setString(1, DCity);
st.setString(2, DCountry);
st.setString(3, DHotel);
基本上,如果要将三个参数插入到预准备语句中,它应该如下所示:
PreparedStatement st = connection.prepareStatement("SELECT * FROM offers WHERE city=? AND country=? AND hotel=?");
st.setString(1, DCity);
st.setString(2, DCountry);
st.setString(3, DHotel);
希望让事情更清楚!
答案 1 :(得分:0)
这就是我用过的东西。 :
String sql = "SELECT * FROM offers where city='"+DCity + "' AND country='"+DCountry+"' AND hotel='"+DHotel+"'";
Statement stmp = connection.createStatement();
ResultSet rs = stmp.executeQuery(sql);
而不是
PreparedStatement st = connection.prepareStatement("SELECT * FROM offers WHERE city=? AND country=? AND hotel=?");
st.setString(1, DCity);
st.setString(2, DCountry);
st.setString(3, DHotel);
ResultSet rs = st.executeQuery();
现在它有效。问题是在我的执行查询中,我没有参数将数据保存在rs变量中。