我正在修改以前使用JDBC编写的Java Web应用程序(由其他人编写)。数据库是使用PHPMyAdmin创建的。在此过程中,我需要重命名其中一个表。我在代码中用正确的名称替换旧名称,但现在当我尝试插入新记录时,应用程序似乎正在搜索旧表。
这是一个例外:
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;
}
}
答案 0 :(得分:0)
“tableName”在哪里定义? 您的SQL异常声明此表不存在,因此请检查“tableName”的定义并将其设置为表的正确名称。