【发布时间】:2017-09-04 13:50:35
【问题描述】:
我在尝试添加报价时收到错误消息。我正在尝试从字段中获取记录,然后使用 Query 插入 derby 数据库。 我收到一条错误消息:java.sql.SQLSyntaxErrorException: Syntax error: Encountered "\',\'\',\'\',\'" at line 1, column 33.
String Query = "select * from QUOTATION order by DATE DESC";
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/MavenUp","adminmu","admin");
Statement st= conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery(Query);
int id =0;
rs.beforeFirst();
if(rs.next())
{
id = rs.getInt(1);
id++;
}
else
{
id=1;
}
String idd = Integer.toString(id);
String Query1;
Query1 = "Insert into QUOTATION " + "values ("+idd+"','"+ClientName.getText()+"','"+QuotationNo.getText()+"','"+Date.getDate()+"'}";
if(st.executeUpdate(Query1)== 1)
{
JOptionPane.showMessageDialog(this,"Your Quote info has been Inserted");
}
else
{
JOptionPane.showMessageDialog(this, "Something went wrong Try again");
}
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
【问题讨论】:
-
您正在执行的查询是错误的,因为您缺少单引号
-
1.调试器工具在这些类型的问题中方式比我们更有用。 2. 请避免使用字符串连接来构建查询 - JDBC 有绑定参数的概念,这对查询非常有用。
-
那我还应该怎样串接字符串
-
哪里缺少单引号?
-
不要尝试玩带引号的游戏。使用 PreparedStatement:docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
标签: java mysql database swing derby