javaweb学生表的增加、修改、删除

javaweb学⽣表的增加、修改、删除三层架构:
1.建⽴包cn.sdut.dao
建⽴类:
BaseDao.java/StudentDao.java
BaseDao.java:
创建数据库的连接。public Connection getConn()
关闭数据库。public void closeAll()
StudentDao.java:
数据库的增删改查函数。
2.建⽴包cn.sdut.po
建⽴类:
Student.java
Student.java:
定义各个属性。再利⽤⾃动补全实现
3.建⽴包cn.sdut.view
建⽴类:
Main.java
测试数据库的增删改查是否可以实现。
JSP界⾯:
⾸先将js那个⽂件夹考到WebRoot下⾯实现⽇历的⼩程序。影视烟火
index.jsp:对数据库的展⽰:
效果如下“
addStudent.jsp:
updateStudent.jsp:
doDelete.jsp:
删除学⽣
源码如下:BaseDao.java:
package cn.sdut.dao;
import java.sql.*;
public class BaseDao {
Connection con;
PreparedStatement pst;
ResultSet rs;
public Connection getConn(){
try {
Class.forName("sql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url="jdbc:mysql://localhost:3307/school?useUnicode=true&characterEncoding=utf-8"; try {
Connection(url,"root","usbw");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public void closeAll(){
try {
if(rs!=null)
rs.close();
if(pst!=null)
pst.close();
if(con!=null)
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
网络安全控制技术
BaseDao baseDao = new BaseDao();
Connection Conn();
if(conn!=null)
System.out.println("yes");
else System.out.println("no");
}
}
StudentDao.java:
package cn.sdut.dao;
import java.sql.SQLException;
import java.util.*;
import cn.sdut.po.Student;
public class StudentDao extends BaseDao {
public int add(Student stu)
{
int result=0;
con=getConn();
String sql="insert into student(name,birthday,score) values(?,?,?)";
try {
pst=con.prepareStatement(sql);
pst.setString(1, Name());
pst.setString(Birthday());
pst.setFloat(3, Score());
uteUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll();
}
青梅1H
return result;
废钯碳回收钯技术}
public int del(int id)
{
int result=0;
con=getConn();
String sql="delete from student where id=?";
try {
pst=con.prepareStatement(sql);
pst.setInt(1, id);
uteUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll();
}
return result;
}
public int update(Student stu)
{
int result=0;
con=getConn();
String sql="update student set name='"+Name()+"',birthday='"+Birthday()+"',score='"+Score()+"' where id="+Id(); System.out.Id());
try {
pst=con.prepareStatement(sql);
uteUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
closeAll();
}
return result;
}
public List<Student> queryAll()
{
List<Student> list=new ArrayList<Student>();
con=getConn();
String sql="select * from student";
try {
pst=con.prepareStatement(sql);
uteQuery();
())
{
Student stu= new Student();
stu.Int(1));
stu.String(2));
stu.String(3));
stu.Float(4));
list.add(stu);
}
挠性电路板
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
closeAll();
}
return list;
}
public Student queryStudentById(int id){
Student stu=new Student();
con=getConn();
String sql="select * from student where id="+id; try {
pst=con.prepareStatement(sql);
uteQuery(sql);
())
{
stu.Int(1));
stu.String(2));
stu.String(3));
stu.Float(4));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stu;
光盘标签纸}
}
Student.java:

本文发布于:2024-09-23 03:21:00,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/119561.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据库   实现   改查   增删   删除   修改   属性   烟火
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议