jsp提交数据错误
JavaBean程序代码如下:packageabc;importjava.sql.*;publicclassConDB{publicConDB(){}publicvoid...
JavaBean程序代码如下:
package abc;
import java.sql.*;
public class ConDB{
public ConDB(){
}
public void OpenConn(String dsn,String uid,String pwd){
try{
dsn="jdbc:mysql:"+dsn;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=DriverManager.getConnection(dsn,uid,pwd);
stmt=conn.createStatement();
}catch(Exception e){
}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
rs=stmt.executeQuery(sql);
}catch(Exception e){
}
return rs;
}
public int executeUpdate(String sql){
int num=-1;
rs=null;
try{
num=stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(Exception e){
}
return num;
}
public void closeStmt(){
try{
stmt.close();
}catch(Exception e){
}
}
public void closeConn(){
try{
conn.close();
}catch(Exception e){
}
}
private Statement stmt=null;
ResultSet rs=null;
private Connection conn=null;
}
Jsp程序代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>提交</title>
<jsp:useBean id="condb" class="abc.ConDB" scope="page"/>
</head>
<body>
<%
//定义纪录变量
String id="";
String name="";
condb.OpenConn("//localhost/abc","root","");
String sql="insert into a(id,name) values('','adsfsafasfasf')";
int a=condb.executeUpdate(sql);%>
<%=sql%><br>
<%=a%>
</body>
</html>
Mysql数据库:
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(2) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | | |
+-------+-------------+------+-----+---------+----------------+
问题如下:
用insert into a(id,name) values('','adsfsafasfasf');直接在DOS下操作,没有问题.
但是用JavaBean在jsp中就用出问题.
显示如下:
insert into a(id,name) values('','adsfsafasfasf')
-1
-1就说明condb.executeUpdate(sql);操作失败.但是同样的javaBean程序在以前的jsp中运行正常.
请朋友帮助分析一下.能解决问题的,再加分. 展开
package abc;
import java.sql.*;
public class ConDB{
public ConDB(){
}
public void OpenConn(String dsn,String uid,String pwd){
try{
dsn="jdbc:mysql:"+dsn;
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn=DriverManager.getConnection(dsn,uid,pwd);
stmt=conn.createStatement();
}catch(Exception e){
}
}
public ResultSet executeQuery(String sql){
rs=null;
try{
rs=stmt.executeQuery(sql);
}catch(Exception e){
}
return rs;
}
public int executeUpdate(String sql){
int num=-1;
rs=null;
try{
num=stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(Exception e){
}
return num;
}
public void closeStmt(){
try{
stmt.close();
}catch(Exception e){
}
}
public void closeConn(){
try{
conn.close();
}catch(Exception e){
}
}
private Statement stmt=null;
ResultSet rs=null;
private Connection conn=null;
}
Jsp程序代码如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>提交</title>
<jsp:useBean id="condb" class="abc.ConDB" scope="page"/>
</head>
<body>
<%
//定义纪录变量
String id="";
String name="";
condb.OpenConn("//localhost/abc","root","");
String sql="insert into a(id,name) values('','adsfsafasfasf')";
int a=condb.executeUpdate(sql);%>
<%=sql%><br>
<%=a%>
</body>
</html>
Mysql数据库:
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(2) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | | |
+-------+-------------+------+-----+---------+----------------+
问题如下:
用insert into a(id,name) values('','adsfsafasfasf');直接在DOS下操作,没有问题.
但是用JavaBean在jsp中就用出问题.
显示如下:
insert into a(id,name) values('','adsfsafasfasf')
-1
-1就说明condb.executeUpdate(sql);操作失败.但是同样的javaBean程序在以前的jsp中运行正常.
请朋友帮助分析一下.能解决问题的,再加分. 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询