jsp报错 MySQL server version for the right syntax to use near

jsp报错 MySQL server version for the right syntax to use near 'null')' at line 1
完整的报错是:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server version for
the right syntax to use near 'null')' at line 1


应该是我的sql语言写错了,这个是向shop表添加字段的.





<%
String shopname=request.getParameter("shopname");

String price=request.getParameter("Price");

String number=request.getParameter("number");

String brand=request.getParameter("brand");

String date=request.getParameter("date");








String DBDRIVER = "com.mysql.jdbc.Driver";


//定义连接的mysql数据库
String DBURL ="jdbc:mysql://localhost:3306/project_314_422";
String DBUSER ="root";
String DBPASSWORD ="123";
Connection conn = null;
Statement stmt = null;
PreparedStatement pstmt=null;




try
{


Class.forName(DBDRIVER);


//连接数据库需要使用到数据源,用户名,密码
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);


//把数据加进数据库.


pstmt=conn.prepareStatement("select username from user where username=?");
pstmt.setString(1,shopname);
ResultSet rs=pstmt.executeQuery();




if(rs.next()){
out.print("有这个用户名了,请重新注册");
}
else
{
stmt =conn.createStatement();
String sql ="insert into shop(shopname,price,number,brand,date) values ('"+shopname+"','"+price+"','"+number+"','"+brand+",'"+date+"');";
stmt.executeUpdate(sql);
out.print("商品添加成功,2秒钟后自动跳转");
response.setHeader("Refresh","2;URL=admin/shop.jsp");
stmt.close();
}



conn.close();
}

catch(Exception e)
{
e.printStackTrace();
out.println(e);
}


%>





shmilyzoe 1年前 已收到1个回答 举报

黄昏下的星空 幼苗

共回答了19个问题采纳率:94.7% 举报

最好是能够有代码行的错误信息,就是说明你哪行代码对应的SQL语句出的问题,这样更好分析.
根据代码,目测可能有以下问题:
1
pstmt=conn.prepareStatement("select username from user where username=?");
pstmt.setString(1,shopname);
这段中如果shopname是null的话执行结果可能有问题,所以最好先判断以下shopname是否非null.
2
String sql ="insert into shop(shopname,price,number,brand,date) values ('"+shopname+"','"+price+"','"+number+"','"+brand+",'"+date+"');";
这个段中是否都是字符型?,还有就是'"+brand+",少了一个引号

1年前

8
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 16 q. 0.046 s. - webmaster@yulucn.com