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();
}
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: