JSP向ACCESS插入记录的报SQL语法错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP向ACCESS插入记录的报SQL语法错误相关的知识,希望对你有一定的参考价值。

<%@page contentType="text ml;charset=GB2312"%>
<%@page import="java.util.*,java.sql.*"%>
<%request.setCharacterEncoding("GB2312");%>

<%request.setCharacterEncoding("gb2312");%>

<html>
<HEAD>
<TITLE> </TITLE>

</HEAD>

<BODY><BR><BR><BR><BR><BR><BR>
<%

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //加载驱动程序

//catch(Exception e)

//System.out.println("驱动程序加载失败"+e);

//Connection conn=null;
//Statement stmt=null;
//ResultSet rs=null;

Connection conn = DriverManager.getConnection("jdbc:odbc:db1"); //建立数据库链接,library为数据源名称,这里应该填写你刚刚建立的数据源的名称
Statement stmt = conn.createStatement();//建立语句对象

String str1 = "'"+request.getParameter("txtnm")+"'";
String str2 = "'"+request.getParameter("txtpw")+"'";
String strupd = "insert into Text(Name,Password) values("+str1+","+str2+")";//正常的sql语言,可以自己编写
stmt.executeUpdate(strupd);//查询语言时这里要改为stmt.executeQuery

// rs = stmt.executeQuery(sql); //将查询结果放入数据集中

//rs.close();
//stmt.close();
//conn.close();

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

</BODY>
< ML>】

总报错
org.apache.jasper.JasperException: Exception in JSP: /zc.jsp:45

42: String str1 = "'"+request.getParameter("txtnm")+"'";
43: String str2 = "'"+request.getParameter("txtpw")+"'";
44: String strupd = "insert into Text(Name,Password) values("+str1+","+str2+")";//正常的sql语言,可以自己编写
45: stmt.executeUpdate(strupd);//查询语言时这里要改为stmt.executeQuery
46:
47: // rs = stmt.executeQuery(sql); //将查询结果放入数据集中

使用了Access的关键字:
这种异常通常提示为:"XX语句有错误",比如:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] FROM 子句语法错误。
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] INSERT INTO 语句的语法错误。

Access数据库表名不能为text.
如果非要使用text这个关键字,则在拼写SQL语句时使用转义字符[],写为:
String strupd = "INSERT INTO [text](Name,Password) values("+str1+","+str2+")";

也就是将表名text用[]括起来.

Access保留字=====================================

-A
ABSOLUTE
ACTION
ADD
ADMINDB
ALL
ALLOCATE
ALPHANUMERIC
ALTER
AND
ANY
ARE
AS
ASC
ASSERTION
AT
AUTHORIZATION
AUTOINCREMENT
AVG
-B
BAND
BEGIN
BETWEEN
BINARY
BIT
BIT_LENGTH
BNOT
BOR
BOTH
BXOR
BY
BYTE
-C
CASCADE
CASCADED
CASE
CAST
CATALOG
CHAR
CHARACTER
CHAR_LENGTH
CHARACTER_LENGTH
CHECK
CLOSE
COALESCE
COLLATE
COLLATION
COLUMN
COMMIT
COMP
COMPRESSION
CONNECT
CONNECTION
CONSTRAINT
CONSTRAINTS
CONTAINER
CONTINUE
CONVERT
CORRESPONDING
COUNT
COUNTER
CREATE
CREATEDB
CROSS
CURRENCY
CURRENT
CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP
CURRENT_USER
CURSOR
-D
DATABASE
DATE
DATETIME
DAY
DEALLOCATE
DEC
DECIMAL
DECLARE
DEFAULT
DEFERRABLE
DEFERRED
DELETE
DESC
DESCRIBE
DESCRIPTOR
DIAGNOSTICS
DISALLOW
DISCONNECT
DISTINCT
DOMAIN
DOUBLE
DROP
-E
ELSE
END
END-EXEC
ESCAPE
EXCEPT
EXCEPTION
EXCLUSIVECONNECT
EXEC
EXECUTE
EXISTS
EXTERNAL
EXTRACT
-F
FALSE
FETCH
FIRST
FLOAT
FLOAT4
FLOAT8
FOR
FOREIGN
FOUND
FROM
FULL
-G
GENERAL
GET
GLOBAL
GO
GOTO
GRANT
GROUP
GUID
-H
HAVING
HOUR
-I
IDENTITY
IEEEDOUBLE
IEEESINGLE
IGNORE
IMAGE
IMMEDIATE
ININDEX
INDICATOR
INHERITABLE
INITIALLY
INNER
INPUT
INSENSITIVE
INSERT
INT
INTEGER
INTEGER1
INTEGER2
INTEGER4
INTERSECT
INTERVAL
INTO
IS
ISOLATION
-J
JOIN
-K
KEY
-L
LANGUAGE
LAST
LEADING
LEFT
LEVEL
LIKE
LOCAL
LOGICAL
LOGICAL1
LONG
LONGBINARY
LONGCHAR
LONGTEXT
LOWER
-M
MATCH
MAX
MEMO
MIN
MINUTE
MODULE
MONEY
MONTH
-N
NAMES
NATIONAL
NATURAL
NCHAR
NEXT
NO
NOT
NOTE
NULL
NULLIF
NUMBER
NUMERIC
-O
OBJECT
OCTET_LENGTH
OFOLEOBJECT
ONONLY
OPEN
OPTION
ORORDER
OUTER
OUTPUT
OVERLAPS
OWNERACCESS
-P
PAD
PARAMETERS
PARTIAL
PASSWORD
PERCENT
PIVOT
POSITION
PRECISION
PREPARE
PRESERVE
PRIMARY
PRIOR
PRIVILEGES
PROC
PROCEDURE
PUBLIC
-Q
-R
READ
REAL
REFERENCES
RELATIVE
RESTRICT
REVOKE
RIGHT
ROLLBACK
ROWS
-S
SCHEMA
SCROLL
SECOND
SECTION
SELECT
SELECTSCHEMA
SELECTSECURITY
SESSION
SESSION_USER
SET
SHORT
SINGLE
SIZE
SMALLINT
SOME
SPACE
SQL
SQLCODE
SQLERROR
SQLSTATE
STRING
SUBSTRING
SUM
SYSTEM_USER
-T
TABLE
TABLEID
TEMPORARY
TEXT
THEN
TIME
TIMESTAMP
TIMEZONE_HOUR
TIMEZONE_MINUTE
TO
TOP
TRAILING
TRANSACTION
TRANSFORM
TRANSLATE
TRANSLATION
TRIM
TRUE
-U
UNION
UNIQUE
UNIQUEIDENTIFIER
UNKNOWN
UPDATE
UPDATEIDENTITY
UPDATEOWNER
UPDATESECURITY
UPPER
USAGE
USER
USING
-V
VALUE
VALUES
VARBINARY
VARCHAR
VARYING
VIEW
-W
WHEN
WHENEVER
WHERE
WITH
WORK
WRITE
-X
-Y
YEAR
YESNO
-Z
ZONE
参考技术A 方SQL试试

以上是关于JSP向ACCESS插入记录的报SQL语法错误的主要内容,如果未能解决你的问题,请参考以下文章

C# 向Access数据库插入数据时提示INSERT INTO语法错误

由于“SQL 语法”错误,无法插入 MS Access [关闭]

INSERT INTO 语句中的语法错误(将多条记录从 Gridview 插入到 Access)

向access数据库插入出现“insert into 语法错误”

插入查询中发生语法错误(MS Access)

SQL INSERT INTO 语句:向表中插入新记录语法及案例剖析