package servletshomework;import java.io.IOException;
import java.io.PrintWriter;import java.sql.Connection;
import java.sql.DriverManager;import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.servlet.*;
import javax.servlet.http.*;public class AddServlets extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; public void init(ServletConfig config) throws ServletException {
super.init(config);
} public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>AddServlets</title></head>");
out.println("<body>"); Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott",
"woshihk"); stmt = conn.createStatement();
//唯一约束, UNIQUE CONSTRAINT 不然有异常
//CHECK约束 ORA-01438 value larger than specified precision allows for this column // Cause: When inserting or updating records, a numeric value was entered that exceeded the precision defined for the column. // Action: Enter a value that complies with the numeric column's precision, or use the MODIFY option with the ALTER TABLE command to expand the precision.
//ORA-00947 not enough values // Cause: This error occurs when a SQL statement requires two sets of values equal in number, but the second set contains fewer items than the first set. This can occur in a WHERE or HAVING clause in which a nested SELECT returns too few columns as in: // WHERE (A,B) IN (SELECT C FROM ...)
// Another common cause of this error is an INSERT statement in which the VALUES or SELECT clause does not contain enough values needed for the INSERT, as in // INSERT INTO EMP(EMPNO,ENAME) VALUES('JONES')
// Action: Check the number of items in each set and change the SQL statement to make them equal. //ORA-00928 missing SELECT keyword // Cause: A SELECT subquery must be included in a CREATE VIEW statement. // Action: Correct the syntax. Insert the required SELECT clause after the CREATE VIEW clause and then retry the statement.
int executeUpdate =
stmt.executeUpdate("INSERT INTO emp (7777,'ABC')VALUES('天府学院')");
System.out.println(executeUpdate);
rs = stmt.executeQuery("SELECT * FROM emp");
while (rs.next()) {
out.println(rs.getInt(1) + " " + rs.getString(2) +
" " + rs.getString(3) + "<br >");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException sqle) {
// TODO: Add catch code
sqle.printStackTrace();
} }
out.println("</body></html>");
out.println("<p>The servlet has received a POST or GET. This is the reply.</p>");
out.println("</body></html>");
out.close();
}
}
问题是:ava.sql.SQLSyntaxErrorException: ORA-00928: 缺失 SELECT 关键字 at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:91)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1035)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:183)
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:942)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1223)
at oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1711)
at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1679)
at oracle.jdbc.driver.OracleStatementWrapper.executeUpdate(OracleStatementWrapper.java:275)
at servletshomework.AddServlets.service(AddServlets.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3594)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
import java.io.PrintWriter;import java.sql.Connection;
import java.sql.DriverManager;import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;import javax.servlet.*;
import javax.servlet.http.*;public class AddServlets extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK"; public void init(ServletConfig config) throws ServletException {
super.init(config);
} public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>AddServlets</title></head>");
out.println("<body>"); Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "scott",
"woshihk"); stmt = conn.createStatement();
//唯一约束, UNIQUE CONSTRAINT 不然有异常
//CHECK约束 ORA-01438 value larger than specified precision allows for this column // Cause: When inserting or updating records, a numeric value was entered that exceeded the precision defined for the column. // Action: Enter a value that complies with the numeric column's precision, or use the MODIFY option with the ALTER TABLE command to expand the precision.
//ORA-00947 not enough values // Cause: This error occurs when a SQL statement requires two sets of values equal in number, but the second set contains fewer items than the first set. This can occur in a WHERE or HAVING clause in which a nested SELECT returns too few columns as in: // WHERE (A,B) IN (SELECT C FROM ...)
// Another common cause of this error is an INSERT statement in which the VALUES or SELECT clause does not contain enough values needed for the INSERT, as in // INSERT INTO EMP(EMPNO,ENAME) VALUES('JONES')
// Action: Check the number of items in each set and change the SQL statement to make them equal. //ORA-00928 missing SELECT keyword // Cause: A SELECT subquery must be included in a CREATE VIEW statement. // Action: Correct the syntax. Insert the required SELECT clause after the CREATE VIEW clause and then retry the statement.
int executeUpdate =
stmt.executeUpdate("INSERT INTO emp (7777,'ABC')VALUES('天府学院')");
System.out.println(executeUpdate);
rs = stmt.executeQuery("SELECT * FROM emp");
while (rs.next()) {
out.println(rs.getInt(1) + " " + rs.getString(2) +
" " + rs.getString(3) + "<br >");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException sqle) {
// TODO: Add catch code
sqle.printStackTrace();
} }
out.println("</body></html>");
out.println("<p>The servlet has received a POST or GET. This is the reply.</p>");
out.println("</body></html>");
out.close();
}
}
问题是:ava.sql.SQLSyntaxErrorException: ORA-00928: 缺失 SELECT 关键字 at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:91)
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1035)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:183)
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:942)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1223)
at oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1711)
at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1679)
at oracle.jdbc.driver.OracleStatementWrapper.executeUpdate(OracleStatementWrapper.java:275)
at servletshomework.AddServlets.service(AddServlets.java:69)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3594)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
--还有注意列的数量与插入值的数量对应
stmt.executeUpdate("INSERT INTO emp (此处是你的列) VALUES('天府学院')");