jsp报错 MySQL server version for the right syntax to use near 'null')' at line 1
完整的报错是:com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:YouhaveanerrorinyourSQLsyn...
完整的报错是:
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);
}
%> 展开
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);
}
%> 展开
展开全部
找到新建页面所在的工程名字,然后左键选中,右键弹出功能菜单,选择Build
Path,进入配置路径。
在java build path 页面的下选择Libraries栏目(默认选择),点击右侧的Add
Library 按钮,进入添加类库。
在添加类库页面,选择默认的选项Server Runtime,点击next,继续配置。
选择自己已经配置好的服务器,本机安装的是tomcat8,以这个为例,选中之后,点击finish按钮即可,完成之后,返回java build path页面。
在java build path 页面中,选择刚刚配置好的类库,选中之后,点击ok按钮。
配置完成之后,页面不会在出错了,如果出错的话,可以先删除这个页面,再新建一次。
展开全部
我觉得应该是user表的问题 因为user这个单词在mysql中是关键字 所以可能会出现你那种情况 把表面改成别的 不要使用mysql中的关键词 当然也有可能是别的关键字有问题 仔细找下
下面是mysql中的关键字
ADD
ALL
ALTER
ANALYZE
AND
AS
ASC
ASENSITIVE
BEFORE
BETWEEN
BIGINT
BINARY
BLOB
BOTH
BY
CALL
CASCADE
CASE
CHANGE
CHAR
CHARACTER
CHECK
COLLATE
COLUMN
CONDITION
CONNECTION
CONSTRAINT
CONTINUE
CONVERT
CREATE
CROSS
CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
CURRENT_USER
CURSOR
DATABASE
DATABASES
DAY_HOUR
DAY_MICROSECOND
DAY_MINUTE
DAY_SECOND
DEC
DECIMAL
DECLARE
DEFAULT
DELAYED
DELETE
DESC
DESCRIBE
DETERMINISTIC
DISTINCT
DISTINCTROW
DIV
DOUBLE
DROP
DUAL
EACH
ELSE
ELSEIF
ENCLOSED
ESCAPED
EXISTS
EXIT
EXPLAIN
FALSE
FETCH
FLOAT
FLOAT4
FLOAT8
FOR
FORCE
FOREIGN
FROM
FULLTEXT
GOTO
GRANT
GROUP
HAVING
HIGH_PRIORITY
HOUR_MICROSECOND
HOUR_MINUTE
HOUR_SECOND
IF
IGNORE
IN
INDEX
INFILE
INNER
INOUT
INSENSITIVE
INSERT
INT
INT1
INT2
INT3
INT4
INT8
INTEGER
INTERVAL
INTO
IS
ITERATE
JOIN
KEY
KEYS
KILL
LABEL
LEADING
LEAVE
LEFT
LIKE
LIMIT
LINEAR
LINES
LOAD
LOCALTIME
LOCALTIMESTAMP
LOCK
LONG
LONGBLOB
LONGTEXT
LOOP
LOW_PRIORITY
MATCH
MEDIUMBLOB
MEDIUMINT
MEDIUMTEXT
MIDDLEINT
MINUTE_MICROSECOND
MINUTE_SECOND
MOD
MODIFIES
NATURAL
NOT
NO_WRITE_TO_BINLOG
NULL
NUMERIC
ON
OPTIMIZE
OPTION
OPTIONALLY
OR
ORDER
OUT
OUTER
OUTFILE
PRECISION
PRIMARY
PROCEDURE
PURGE
RAID0
RANGE
READ
READS
REAL
REFERENCES
REGEXP
RELEASE
RENAME
REPEAT
REPLACE
REQUIRE
RESTRICT
RETURN
REVOKE
RIGHT
RLIKE
SCHEMA
SCHEMAS
SECOND_MICROSECOND
SELECT
SENSITIVE
SEPARATOR
SET
SHOW
SMALLINT
SPATIAL
SPECIFIC
SQL
SQLEXCEPTION
SQLSTATE
SQLWARNING
SQL_BIG_RESULT
SQL_CALC_FOUND_ROWS
SQL_SMALL_RESULT
SSL
STARTING
STRAIGHT_JOIN
TABLE
TERMINATED
THEN
TINYBLOB
TINYINT
TINYTEXT
TO
TRAILING
TRIGGER
TRUE
UNDO
UNION
UNIQUE
UNLOCK
UNSIGNED
UPDATE
USAGE
USE
USING
UTC_DATE
UTC_TIME
UTC_TIMESTAMP
VALUES
VARBINARY
VARCHAR
VARCHARACTER
VARYING
WHEN
WHERE
WHILE
WITH
WRITE
X509
XOR
YEAR_MONTH
ZEROFILL
下面是mysql中的关键字
ADD
ALL
ALTER
ANALYZE
AND
AS
ASC
ASENSITIVE
BEFORE
BETWEEN
BIGINT
BINARY
BLOB
BOTH
BY
CALL
CASCADE
CASE
CHANGE
CHAR
CHARACTER
CHECK
COLLATE
COLUMN
CONDITION
CONNECTION
CONSTRAINT
CONTINUE
CONVERT
CREATE
CROSS
CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
CURRENT_USER
CURSOR
DATABASE
DATABASES
DAY_HOUR
DAY_MICROSECOND
DAY_MINUTE
DAY_SECOND
DEC
DECIMAL
DECLARE
DEFAULT
DELAYED
DELETE
DESC
DESCRIBE
DETERMINISTIC
DISTINCT
DISTINCTROW
DIV
DOUBLE
DROP
DUAL
EACH
ELSE
ELSEIF
ENCLOSED
ESCAPED
EXISTS
EXIT
EXPLAIN
FALSE
FETCH
FLOAT
FLOAT4
FLOAT8
FOR
FORCE
FOREIGN
FROM
FULLTEXT
GOTO
GRANT
GROUP
HAVING
HIGH_PRIORITY
HOUR_MICROSECOND
HOUR_MINUTE
HOUR_SECOND
IF
IGNORE
IN
INDEX
INFILE
INNER
INOUT
INSENSITIVE
INSERT
INT
INT1
INT2
INT3
INT4
INT8
INTEGER
INTERVAL
INTO
IS
ITERATE
JOIN
KEY
KEYS
KILL
LABEL
LEADING
LEAVE
LEFT
LIKE
LIMIT
LINEAR
LINES
LOAD
LOCALTIME
LOCALTIMESTAMP
LOCK
LONG
LONGBLOB
LONGTEXT
LOOP
LOW_PRIORITY
MATCH
MEDIUMBLOB
MEDIUMINT
MEDIUMTEXT
MIDDLEINT
MINUTE_MICROSECOND
MINUTE_SECOND
MOD
MODIFIES
NATURAL
NOT
NO_WRITE_TO_BINLOG
NULL
NUMERIC
ON
OPTIMIZE
OPTION
OPTIONALLY
OR
ORDER
OUT
OUTER
OUTFILE
PRECISION
PRIMARY
PROCEDURE
PURGE
RAID0
RANGE
READ
READS
REAL
REFERENCES
REGEXP
RELEASE
RENAME
REPEAT
REPLACE
REQUIRE
RESTRICT
RETURN
REVOKE
RIGHT
RLIKE
SCHEMA
SCHEMAS
SECOND_MICROSECOND
SELECT
SENSITIVE
SEPARATOR
SET
SHOW
SMALLINT
SPATIAL
SPECIFIC
SQL
SQLEXCEPTION
SQLSTATE
SQLWARNING
SQL_BIG_RESULT
SQL_CALC_FOUND_ROWS
SQL_SMALL_RESULT
SSL
STARTING
STRAIGHT_JOIN
TABLE
TERMINATED
THEN
TINYBLOB
TINYINT
TINYTEXT
TO
TRAILING
TRIGGER
TRUE
UNDO
UNION
UNIQUE
UNLOCK
UNSIGNED
UPDATE
USAGE
USE
USING
UTC_DATE
UTC_TIME
UTC_TIMESTAMP
VALUES
VARBINARY
VARCHAR
VARCHARACTER
VARYING
WHEN
WHERE
WHILE
WITH
WRITE
X509
XOR
YEAR_MONTH
ZEROFILL
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最好是能够有代码行的错误信息,就是说明你哪行代码对应的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
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+",少了一个引号
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
sql语句的条件写错了,是where shopname=?
追问
不是这个原因哦。
追答
String sql ="insert into shop(shopname,price,number,brand,date) values ('"+shopname+"','"+price+"','"+number+"','"+brand+",'"+date+"');";
改成这样试试:就是把倒数第二个";"去掉了。
String sql ="insert into shop(shopname,price,number,brand,date) values ('"+shopname+"','"+price+"','"+number+"','"+brand+",'"+date+"')";
还有的一个问题,你的主键是不是自动增长的?
不是自动增长也会出错,因为你没有插入主键的值。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询