gpt4 book ai didi

java - 更改表名后 ExecuteUpdate() 异常

转载 作者:行者123 更新时间:2023-11-30 00:44:55 25 4
gpt4 key购买 nike

我正在修改一个 Java Web 应用程序,该应用程序是以前(由其他人)编写的,它使用 JDBC。该数据库是使用 PHPMyAdmin 创建的。在此过程中,我需要重命名其中一张表。我在代码中用正确的名称替换了旧名称,但现在当我尝试插入新记录时,应用程序似乎正在搜索旧表。

这是异常(exception):

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.reclamation' doesn't exist

reclamation 是表的旧名称。

这是我的代码:

public class ServletUserEnquiryForm extends HttpServlet{
public void init(ServletConfig config) throws ServletException{

super.init(config);
}

/**Process the HTTP Get request*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{

request.setCharacterEncoding("UTF-8");

String shop = request.getParameter("shop");
System.out.println(shop);
String name = request.getParameter("names");
System.out.println(name);
String tell = request.getParameter("tell");
String stoke = request.getParameter("stoke");
String model = request.getParameter("model");
String barcod = request.getParameter("barkod");
String bon = request.getParameter("bon");
String fack = request.getParameter("fack");
if(fack.equals("")){
fack = "0";
}
String comentar = request.getParameter("comentar");
String reasonable = request.getParameter("reasonable");
if(reasonable == "yes")
{
reasonable = "yes";
}
else
{
reasonable = "no";
}

String in_store = request.getParameter("in_store");
if(in_store == "yes")
{
in_store = "yes";
}
else
{
in_store = "no";
}

String satisfaction = request.getParameter("satisfaction");
if(satisfaction == "zamqna")
{
satisfaction = "Замяна";
}
else
if(satisfaction == "remont")
{
satisfaction = "Ремонт";
}
else
if(satisfaction == "namalenie")
{
satisfaction = "Намаление";
}
else
if(satisfaction == "return")
{
satisfaction = "Връщане на сума";
}
//String okomplektovka = request.getParameter("okomplektovka");

String type_return = request.getParameter("type_return");
if(type_return == "sum")
{
type_return = "sum";
}
else
if(type_return == "bank")
{
type_return = "bank";
}
else
type_return = "";

String sum = request.getParameter("sum");
if(sum == "")
{
sum = "0.00";
}

String iban = request.getParameter("iban");
if(iban =="")
{
iban = "N/A";
}
String servis = request.getParameter("servis");

if(reasonable != null && in_store != null && satisfaction != null && type_return != null)
{
try {
Connection con = dbSQL.getConnection();
String tableName = "shop_"+shop;
String sql = "INSERT INTO " + tableName + " (name,tel,product_type,model,genkod,receipts,invoice,description,reasonable,in_store,satisfaction,type_return,sum,iban,shop,data_in,servis)" + " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate(), ?)";
PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = null;
ps.setString(1, name);
ps.setString(2, tell);
ps.setString(3, stoke);
ps.setString(4, model);
ps.setString(5, barcod);
ps.setString(6, bon);
ps.setString(7, fack);
ps.setString(8, comentar);
ps.setString(9, reasonable);
ps.setString(10, in_store);
ps.setString(11, satisfaction);
ps.setString(12, type_return);
ps.setString(13, sum);
ps.setString(14, iban);
ps.setString(15, shop);
ps.setString(16, servis);
ps.executeUpdate();
ps.close();

}
catch(Exception e){

}
finally {


}

}
}

}

工作正常,直到到达 ps.executeUpdate() 并且我看不出问题出在哪里。

这是dbSQL的定义:

public class dbSQL {

private static final String DB_HOST ="192.168.15.26";
private static final String DB_USER = "root";
private static final String DB_PASS = "passs";
private static final String DB_NAME = "test?characterEncoding=UTF-8";

private static final String useDB = "mysql";


public static Connection getConnection() throws ClassNotFoundException {

Connection conn = null;
try {

Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://" + DB_HOST + ":3306/" + DB_NAME, DB_USER, DB_PASS);

} catch (SQLException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
return conn;
}

}

最佳答案

“tableName”在哪里定义?您的 SQLExceptions 声明该表不存在,因此请检查“tableName”的定义并将其设置为正确的表名称。

关于java - 更改表名后 ExecuteUpdate() 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21460936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com