jsp-mysql总结表的操作合集
jsp+mysql进行简单的增删改查总结
第一步创建一个新的包用于封装学生属性
在新包里面创建新的类存入学生属性
publicclass shuxing {
String name;
int age;
String sex;
int id;
右击鼠标选择soure-gennerategettersand setters来封装数据
第二步创建另一个新的包用于主体serlvet的操作
Servlet里面操作:
1.因为doget比dopost拥有更强的保密性,所以把dopost的语句也沿用doget的形式
doPost(request,response);注意删除的时候是不是把}也删除了,会报错。2.将设置请求的编码格式为’UTF-8’防止后面输入学生信息的时候出现乱码,之所以放在前面是怕后面写的话就不可以public公用了。
3.读取后面表中按钮的名称,用一个新的名字来获取随后跳转需要调用的语句
String opeationName = request.getParameter("openration");//取出条件的OPENRATION,看执行哪一种语句
4.查看取出的名称是否为空。
opeationName = opeationName==null?"":opeationName.trim();//取出来看下是不是空,不是空去掉空格
5.用if()else()语句来写调用的程序用前面取出的名字
一:删除方法:opeationName.equals("delete")
(1).提取要删除的ID String id=request.getParameter("id");
(2).查看是否为空,为空输出“”if(id==null)
{
id="";
}
(3).try{}catch()语句用于报错
try{
int result = doDelete(id);//调用到doDelete的方法
List
qw = getData();//获取数据
request.setAttribute("qw" ,qw);//缓存范围中设置具体的属性:第一个参数是属性名,字符串类型的数据。,也是以后获取的依据条件,第二个是插入的数据,支持对象属性。
(4)跳转进入最开始的jsp页面即查询语句的JSP界面
二.增加opeationName.equals("add")
(1).增加不需要提取什么属性。直接获取在JSP中提交的数据即可,但是要观察是否为空String newname=request.getParameter("name");
if(newname==null)
{
newname="";
}
String newid=request.getParameter("id");
if(newid==null)
{
newid="";
}
String newsex=request.getParameter("sex");
if(newsex==null)
{
newsex="";
}
String newage=request.getParameter("age");
if(newage==null)
{
newage="";
}
(2).一样的try语句
需要注意的是在DOADD后面写上属性的名称方便后面调用
int result = doAdd(newname,newid,newage,newsex);
(3).跳转到查询页面
三.查询页面
(1)创建一个新的集合用于存放数据List
ArrayList
Try用于报错try{
qw = getData();
}catch(Exception e){
e.printStackTrace();}
缓存数据
request.setAttribute("qw" ,qw);
跳转request.getRequestDispatcher("index.jsp").forward(request, response);
四:开始查询的主程序:
public List
List
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="123456";
//数据库名
String dbName="xueshenbiao";
//表名
String tableName="tubiao";
//联结字符串
String
url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+u serPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection
connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM tubiao ";
ResultSetrs = statement.executeQuery(sql);
//获得数据结果集合
ResultSetMetaDatarmeta = rs.getMetaData();
//确定数据集的列数,亦字段数
int numColumns=rmeta.getColumnCount();
以上都是连接数据库的操作
下面的是获取数据的操作
while(rs.next())//循环操作
{
shuxing a1=new shuxing();
int Id = rs.getInt(1);
String Name = rs.getString(2);
String Sex = rs.getString(3);
int Age = rs.getInt(4);
a1.setId(Id);
a1.setAge(Age);
a1.setName(Name);
a1.setSex(Sex);
qw.add(a1);
}
return qw;
}
五:删除主程序:
int result =0;//用个result来做参数现在用不到以后可以在删除不成功的时候用于页面的跳转
String deleteAll="DELETE FROM tubiao WHERE id "+" = "+"'"+id+"'";
result = statement.executeUpdate(deleteAll);}
return result;
}
六.增加主程序:
publicint doAdd(String
newname,Stringnewid,Stringnewage,Stringnewsex ) throws Exception{ //注意对应
//驱动程序名
String condition1= "INSERT INTO tubiao(name,id,age,sex) VALUES"+"("+"'"+newname+"',"+newid+","+newage+",'"+newsex+"')";
//注意一一对应。不对应就在tubiao后面写参数
int result = statement.executeUpdate(condition1);
七:修改
之所以放在最后是因为步骤比较多
(1)。首先需要查找要修改的原始数据的值,opeation不直接调用修改的方法而是首先找到需要修改的学生的信息;步骤和查询信息差不多shuxing a1 = new shuxing();
String id = request.getParameter("id");
//查这个ID的学生信息
//驱动程序名
String driverName="com.mysql.jdbc.Driver";
//数据库用户名
String userName="root";
//密码
String userPasswd="123456";
//数据库名
String dbName="xueshenbiao";
//表名
String tableName="tubiao";
//联结字符串
try{
String
url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+u serPasswd;
Class.forName("com.mysql.jdbc.Driver").newInstance();
java.sql.Connection
connection=DriverManager.getConnection(url);
Statement statement = connection.createStatement();
String sql="SELECT * FROM tubiao where id="+id;
ResultSetrs = statement.executeQuery(sql);
//获得数据结果集合
ResultSetMetaDatarmeta = rs.getMetaData();
//确定数据集的列数,亦字段数
int numColumns=rmeta.getColumnCount();
while(rs.next())
{
int Id = rs.getInt(1);
String Name = rs.getString(2);
String sex = rs.getString(3);
int Age = rs.getInt(4);
a1.setId(Id);
a1.setAge(Age);
a1.setName(Name);
a1.setSex(sex);
}
rs.close();
statement.close();
connection.close();
}catch(Exception e){
e.printStackTrace();
}
(2).缓存数据request.setAttribute("a1", a1);
(3).跳入修改的JSP进行修改的操作
(4).由JSP跳转进入调用修改的方法与前面的类似
opeationName.equals("modify")
String id = request.getParameter("id");
//调用修改方法
if(id==null)
{
id="";
}
String newname=request.getParameter("name");
if(newname==null)
{
newname="";
}
String newage=request.getParameter("age");
if(newage==null)
{
newage="";
}
String newsex=request.getParameter("sex");
if(newsex==null)
{
newsex="";
}
try{
int result = doModify(id,newname,newage,newsex);
List
qw = getData();
request.setAttribute("qw" ,qw);
}catch(Exception e){
e.printStackTrace();
}
request.getRequestDispatcher("index.jsp").forward(request, response);
注意的是修改主程序中在插入update的时候分开来写
String condition1= "UPDATE tubiao SET name = '"+newname+"' WHERE id ="+id;
String condition2= "UPDATE tubiao SET sex = '"+newsex+"' WHERE id ="+id;
String condition3= "UPDATE tubiao SET age = "+newage+" WHERE id ="+id; int result = statement.executeUpdate(condition1);
int result2 = statement.executeUpdate(condition2);
int result3 = statement.executeUpdate(condition3);
//获得数据结果集合
八.JSP中的操作
增加的JSP:注意action对应的东西就好