request.setCharacterEncoding("UTF-8");
String id=request.getParameter("id");
String name=request.getParameter("name");
System.out.println(name);
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String major=request.getParameter("major");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/orcl";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
rs=stmt.executeQuery("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%>
把executeQuery改成executeUpdate也不行 因为要返回一个ResultSet
String id=request.getParameter("id");
String name=request.getParameter("name");
System.out.println(name);
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String major=request.getParameter("major");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/orcl";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
rs=stmt.executeQuery("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%>
把executeQuery改成executeUpdate也不行 因为要返回一个ResultSet
解决方案 »
- web.xml
- 关于JXL导出后 的 跳转问题
- 关于图片文件上传的问题
- 请教高手:jsp页面中数据库操作提示空指针异常
- ====急问请求高手====关于ssl连接submit提交失败的问题
- 多表查询问题?表里主键字段相同?而又要以这个字段做为参数传给另一个页面?请教
- 一个表单,里面的一个下拉框,里面的三种类别,当我选中一种类别时,表单生成数目不同的text
- 如何根据不同的条件使下拉菜单中的某一项被选中?
- 如何将BigDecimal型转化成Float型而不改变数值?
- 如何在网页中显示从数据库里面读出的图片啊,谢谢
- <frameset></frameset>怎么相互获取参数,急急急!
- 大家过来看看我的问题,非常有意思
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>插入学生信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String id=request.getParameter("id");
String name=request.getParameter("name");
System.out.println(name);
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String major=request.getParameter("major");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/orcl";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
rs=stmt.executeQuery("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%>
<center>
<%
if(rs.next())
{
out.print("<br><h3>成功输入!</h3>");
}
else{
out.print("<br><h3>输入失败!</h3>");
}
%>
<br>
<a href=submit.jsp>返回信息输入页面</a> <a href=layout.jsp>进入信息查询页面</a>
</center>
<%
if(rs!=null)
{
rs.close();
}
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
%>
</body>
</html>
严重: Servlet.service() for servlet jsp threw exception
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.Statement.checkForDml(Statement.java:417)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1140)
at org.apache.jsp.insert_jsp._jspService(insert_jsp.java:102)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>插入学生信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String id=request.getParameter("id");
String name=request.getParameter("name");
System.out.println(name);
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String major=request.getParameter("major");
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/orcl";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
rs=stmt.executeQuery("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%>
<center>
<%
if(rs.next())
{
out.print("<br><h3>成功输入!</h3>");
}
else{
out.print("<br><h3>输入失败!</h3>");
}
%>
<br>
<a href=submit.jsp>返回信息输入页面</a> <a href=layout.jsp>进入信息查询页面</a>
</center>
<%
if(rs!=null)
{
rs.close();
}
if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
}
%>
</body>
</html>
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.Statement.checkForDml(Statement.java:417)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1140)
at org.apache.jsp.insert_jsp._jspService(insert_jsp.java:102)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
这里你用的是executeQuery这个API,但是SQL语句却是insert语句,导致了出错
如果你像插入数据,返回结果肯定不是ResultSet,你试下下面的JSP可以么<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>插入学生信息</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String id=request.getParameter("id");
String name=request.getParameter("name");
System.out.println(name);
String age=request.getParameter("age");
String gender=request.getParameter("gender");
String major=request.getParameter("major"); try{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/orcl";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement(); boolean result=stmt.execute("insert into student (id,name,age,gender,major)values("+id+"','"+name+"','"+age+",'"+gender+"','"+major+")");%><center>
<%
if(result)
{
out.print("<br><h3>成功插入!</h3>");
}
else{
out.print("<br><h3>输入失败!</h3>");
} %> <br>
<a href=submit.jsp>返回信息输入页面</a> <a href=layout.jsp>进入信息查询页面</a>
</center>
<%
if(rs!=null)
{
rs.close();
} if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
} }catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>
又报错了 ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''dfg )' at line 1
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`NAME` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`gender` varchar(20) DEFAULT NULL,
`major` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
)
下面是测试可以通过的JSP页面,我的JDBC的URL连接是MSDB数据库,你改成你自己的ORCL就可以了
还有我只是跑通了一个自己给定的值,你可以改成你其他页面传过来的各种属性,但是id age需要从
String转化为int类型<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%
// 这个JSP有待于测试
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>插入学生信息</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page"> <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<%
// request.setCharacterEncoding("UTF-8");
// String id=request.getParameter("id");
// String name=request.getParameter("name");
// System.out.println(name);
// String age=request.getParameter("age");
// String gender=request.getParameter("gender");
// String major=request.getParameter("major"); String idStr="1";
int id=Integer.parseInt(idStr);
String name="name";
// System.out.println(name);
String ageStr="20";
int age=Integer.parseInt(ageStr);
String gender="gender";
String major="major"; try{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/msdb";
String user="root";
String password="root";
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement(); String insert="insert into student (id,name,age,gender,major)values('"+id+"','"+name+"','"+age+"','"+gender+"','"+major+"')";
System.out.println("SQL:"+insert);
int result=stmt.executeUpdate(insert);%><center>
<%
if(result==1)
{
out.print("<br><h3>成功插入!</h3>");
}
else{
out.print("<br><h3>输入失败!</h3>");
} %> <br>
<a href=submit.jsp>返回信息输入页面</a> <a href=layout.jsp>进入信息查询页面</a>
</center>
<%
if(rs!=null)
{
rs.close();
} if(stmt!=null)
{
stmt.close();
}
if(conn!=null)
{
conn.close();
} }catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>