jsp中用JavaBean接收表单数据生成SqL语句增添到数据库怎么做
获取表单数据Do.javapackagedoi;publicclassDo{privateStringsno;privateStringname;privateStrin...
获取表单数据 Do.java
package doi;
public class Do
{
private String sno;
private String name;
private String sex;
private String college;
private String grade;
private String major;
public void setSno(String s)
{sno=s;}
public String getSno()
{return sno;}
public void setName(String s1)
{name=s1;}
public String getName()
{return name;}
public void setSex(String s2)
{sex=s2;}
public String getSex()
{return sex;}
public void setCollege(String s3)
{college=s3;}
public String getCollege()
{return college;}
public void setGrade(String s4)
{grade=s4;}
public String getGrade()
{return grade;}
public void setMajor(String s5)
{major=s5;}
public String getMajor()
{return major;}
}
数据库连接:
package doi;
import java.sql.*;
public class DataBase{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:student";
Connection conn= null;
ResultSet rs = null;
static Statement stmt =null;
public DataBase(){
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("DataBase();"+e.getMessage());
}
}
public void executeInsert(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DataBase.executeUpdate"+ex.getMessage());
}
}
public void closeAll()
{
try
{
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(SQLException ex1)
{
System.err.println(ex1.getMessage());
}
}
}
用另一个JVo.java,获取数据写入数据库
package doi;
public class JVo {
public void vo()
{
String s;
String sqlQuery,state,sqlQuerydetail;
Do do1=new Do();
String sno1=do1.getSno();
String name1=do1.getName();
String sex1=do1.getSex();
String college1=do1.getCollege();
String grade1=do1.getGrade();
String major1=do1.getMajor();
DataBase DbBean=new DataBase();
s="INSERT INTO student(sno,name,sex,college,grade,major) VALUES('"+sno1+"','"+name1+"','"+sex1+"','"+college1+"','"+grade1+"','"+major1+"')";
DbBean.executeInsert(s);
//sqlQuery = "SELECT * FROM student where sno='"+sno1+"'";
//ResultSet RS = db.executeQuery(sqlQuery);
}
}
jsp页面代码:
<body>
<jsp:useBean id="do5" class="doi.Do" scope="page" />
<jsp:setProperty name="do5" property="*" />
<jsp:useBean id="do2" class="doi.JVo" scope="page" />
<%
do2.vo();
%>
</body>
jsp和代码编译后没有问题,可表单填写后不能写到数据库里,有高手能帮我看看哪里错了吗,非常感谢啊,在线等啊,QQ:26901082
可以填到数据库了,但都是空值,不是表单的内容,怎么回事? 展开
package doi;
public class Do
{
private String sno;
private String name;
private String sex;
private String college;
private String grade;
private String major;
public void setSno(String s)
{sno=s;}
public String getSno()
{return sno;}
public void setName(String s1)
{name=s1;}
public String getName()
{return name;}
public void setSex(String s2)
{sex=s2;}
public String getSex()
{return sex;}
public void setCollege(String s3)
{college=s3;}
public String getCollege()
{return college;}
public void setGrade(String s4)
{grade=s4;}
public String getGrade()
{return grade;}
public void setMajor(String s5)
{major=s5;}
public String getMajor()
{return major;}
}
数据库连接:
package doi;
import java.sql.*;
public class DataBase{
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:student";
Connection conn= null;
ResultSet rs = null;
static Statement stmt =null;
public DataBase(){
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("DataBase();"+e.getMessage());
}
}
public void executeInsert(String sql)
{
try
{
conn = DriverManager.getConnection(sConnStr);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
}
catch(SQLException ex)
{
System.err.println("DataBase.executeUpdate"+ex.getMessage());
}
}
public void closeAll()
{
try
{
if(stmt!=null)
stmt.close();
if(conn!=null)
conn.close();
}
catch(SQLException ex1)
{
System.err.println(ex1.getMessage());
}
}
}
用另一个JVo.java,获取数据写入数据库
package doi;
public class JVo {
public void vo()
{
String s;
String sqlQuery,state,sqlQuerydetail;
Do do1=new Do();
String sno1=do1.getSno();
String name1=do1.getName();
String sex1=do1.getSex();
String college1=do1.getCollege();
String grade1=do1.getGrade();
String major1=do1.getMajor();
DataBase DbBean=new DataBase();
s="INSERT INTO student(sno,name,sex,college,grade,major) VALUES('"+sno1+"','"+name1+"','"+sex1+"','"+college1+"','"+grade1+"','"+major1+"')";
DbBean.executeInsert(s);
//sqlQuery = "SELECT * FROM student where sno='"+sno1+"'";
//ResultSet RS = db.executeQuery(sqlQuery);
}
}
jsp页面代码:
<body>
<jsp:useBean id="do5" class="doi.Do" scope="page" />
<jsp:setProperty name="do5" property="*" />
<jsp:useBean id="do2" class="doi.JVo" scope="page" />
<%
do2.vo();
%>
</body>
jsp和代码编译后没有问题,可表单填写后不能写到数据库里,有高手能帮我看看哪里错了吗,非常感谢啊,在线等啊,QQ:26901082
可以填到数据库了,但都是空值,不是表单的内容,怎么回事? 展开
2个回答
展开全部
我用你的代码试一下,首先,你数据库连接是没问题的。写法对我也有些启发。
你看看conn = DriverManager.getConnection(sConnStr);用户名,密码用不用加。
你没吧form表单发上,
<jsp:useBean id="do5" class="doi.Do" scope="page" />
<jsp:setProperty name="do5" property="*" />
<jsp:useBean id="do2" class="doi.JVo" scope="page" />这里表单中个个name必须跟do.class中的那些sno等字段名完全相同。给你个建议,给JVo.class加个main方法把那些值输出来看看,是否是空。最好新建个test.java来测试。
最后,那个数据源你不会没设置吧!
你看看conn = DriverManager.getConnection(sConnStr);用户名,密码用不用加。
你没吧form表单发上,
<jsp:useBean id="do5" class="doi.Do" scope="page" />
<jsp:setProperty name="do5" property="*" />
<jsp:useBean id="do2" class="doi.JVo" scope="page" />这里表单中个个name必须跟do.class中的那些sno等字段名完全相同。给你个建议,给JVo.class加个main方法把那些值输出来看看,是否是空。最好新建个test.java来测试。
最后,那个数据源你不会没设置吧!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询