忙活两天了一点头绪也没有```高手进来帮帮忙``
为什么这个程序会报这样的错语
cannot is issue data manipulation statements with executeQuery();请问什么原因哦``
package beans;
import java.sql.*;
public class connDB {
Connection conn=null;
Statement stmt =null;
ResultSet rs=null;
public connDB()
{
try {
Class.forName( "com.mysql.jdbc.Driver ");
} catch (Exception e) {
// TODO: handle exception
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
try {
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/luntan ", "root ",
"12345678 ");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (Exception ex) {
// TODO: handle exception
System.err.println(ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql)
{
int result=0;
try {
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/luntan ", "root ",
"12345678 ");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (Exception ex) {
// TODO: handle exception
System.err.println(ex.getMessage());
result=0;
}
return result;
}
}
<%@ page language= "java " contentType= "text/html;charset=gb2312 "%>
<%@ page import= "java.sql.* " %>
<jsp:useBean id= "connBean " scope= "page " class= "beans.connDB "/>
<html>
<head>
<title> My JSP 'reg_ok.jsp ' starting page </title>
</head>
<body>
<%
String id=request.getParameter( "id ");
String name=request.getParameter( "name ");
String password=request.getParameter( "password ");
String sex=request.getParameter( "sex ");
ResultSet rs=connBean.executeQuery( "select * from register where username= ' "+name+ " ' ");
System.out.println(name);System.out.println(password);
System.out.println(id);System.out.println(sex);
if(rs.next())
{
%>
<h3> 该用户名己经存在!!请重新 <a href> 注册 </a> </h3>
<%
}
else
{
String sql= "insert into register values( ' "+id+ " ', ' "+name+ " ', ' "+password+ " ', ' "+sex+ " ') ";
int ret=0;
ret=connBean.executeUpdate(sql);
out.println(id);
if(ret!=0)
{
%>
<h3> 用户注册成功!!! </h3>
<jsp:forward page= "login.jsp "> </jsp:forward>
<%
}
else
{
%>
<h3> 用户注册失败!!请重新 <a href= "reg.jsp "> 注册 </a> </h3>
<%
}
}
%>
</body>
</html>
<%@ page contentType= "text/html;charset=gb2312 "%>
<html>
<head> <title> 注册页面 </title> </head>
<body>
<center>
<h1> 欢迎你来注册本网站! </h1>
<br> <br>
<form action= "regok.jsp " method= "post ">
ID: <input type= "text " name= "id "> <br>
用户名: <input type= "text " name= "name "> <br>
密 码: <input type= "password " name= "password "> <br>
性 别: <input type= "text " name= "sex "> <br>
<td> <input type= "submit " value= "提交 "/> </td>
<td> <input type= "reset " value= "重置 "/> </td>
</form>
</center>
</body>
</html>
[解决办法]
String sql= "insert into register values( ' "+id+ " ', ' "+name+ " ', ' "+password+ " ', ' "+sex+ " ') ";
你这四个参数中有为空的字段,但是数据库该字段不为空,导致插入数据库失败的,可以输出获取的值看看;或者给这四个变量赋值,看是否会报错,)
[解决办法]
看看这个
http://www.wy186.cn/wlbc/3/2007/8183.html
可能有帮助