mysql分配学生_简洁学生管理系统mysql(增删改查)

mysql配置:  建立sql库

root:root

password:root

表名:student

eb2ba1c5192b5484b04c31b9bef3f4f1.png

id  int

name varchar

age int

85c6c7e1af4d1d9441b7464d69dd6269.png

MyEclipse项目目录

e5096738df728fcb7f209f6770961af8.png

database.properties配置

2f4fa4d99b19dc3c7b9e26f8bdd49b35.png

student.java

package mybean;

public class Student {

private String name;

private int age;

private int id;

public Student(){

super();

}

public String getName() {

return name;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

}

studentDao.java

package my.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Iterator;

import com.mysql.jdbc.Statement;

import mybean.Student;

import util.DBInit;

import util.JDBCUtil;

public class StudentDao {

Student student=null;

Connection con=null;

PreparedStatement ps=null;

ResultSet rs=null;

ArrayList list=new ArrayList();

//获取数据库的所有学生信息

public ArrayList getAllStudent(){

con=JDBCUtil.getConnection();

try {

String sql="select * from student";

ps=con.prepareStatement(sql);

rs=ps.executeQuery();

while(rs.next()){

student=new Student();

student.setName(rs.getString("name"));

student.setAge(rs.getInt("age"));

student.setId(rs.getInt("id"));

list.add(student);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

JDBCUtil.release(con, ps, rs);

}

return list;

}

//添加学生

public int addstudent(Student student) throws Exception{

con=JDBCUtil.getConnection();

int result=0;

String sql="insert into student(name,age) values(?,?)";

try {

ps = con.prepareStatement(sql);

ps.setString(1, student.getName());

ps.setLong(2, student.getAge());

result=ps.executeUpdate();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

JDBCUtil.release(con, ps, rs);

}

return result;

}

//删除

public int delete(int id) throws Exception{

con=JDBCUtil.getConnection();

int result=0;

String sql="delete from student where id=?";

try {

ps=con.prepareStatement(sql);

ps.setInt(1, id);

result=ps.executeUpdate();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

JDBCUtil.release(con, ps, rs);

}

return result;

}

//返回一个学生的信息

public ResultSet select(Student student){

con=JDBCUtil.getConnection();

ResultSet rs=null;

String sql="select * from student where id=?";

try {

ps=con.prepareStatement(sql);

ps.setInt(1,student.getId());

//ps.setString(1, student.getName());

//ps.setInt(2, student.getAge());

rs=ps.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

//JDBCUtil.release(con, ps, rs);

}

return rs;

}

//删除

public int updata(Student student)throws Exception{

con=JDBCUtil.getConnection();

int result=0;

String sql="update student set name=?,age=? where id=?";

try {

ps=con.prepareStatement(sql);

ps.setString(1, student.getName());

ps.setInt(2, student.getAge());

ps.setInt(3, student.getId());

ps.executeUpdate();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

JDBCUtil.release(con, ps, rs);

}

return result;

}

//public static void main(String[] args) {

//StudentDao stu=new StudentDao();

//ArrayList list1=new ArrayList();

//list1=stu.getAllStudent();

//for (int i = 0; i < list1.size(); i++) {

//Student s= (Student)list1.get(i);

//System.out.println(s.getName()+" "+s.getAge());

//}

//}

}

DBInit.java

package util;

import java.io.IOException;

import java.io.InputStream;

import java.util.Properties;

public class DBInit {

//获取database

public static Properties initDB(){

Properties pro=new Properties();

try {

Class c=DBInit.class;

ClassLoader cl=c.getClassLoader();

InputStream in=cl.getResourceAsStream("database.properties");

pro.load(in);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return pro;

}

}

JDBCUtil.java

package util;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Properties;

public class JDBCUtil {

//连接数据库

public static Connection getConnection(){

Properties pro=DBInit.initDB();

String driver=pro.getProperty("driver");

String username=pro.getProperty("userName");

String password=pro.getProperty("password");

String url=pro.getProperty("url");

Connection con=null;

try {

Class.forName(driver);

con=DriverManager.getConnection(url, username, password);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return con;

}

//关闭连接

public static void release(Connection con,PreparedStatement ps,ResultSet rs){

try {

if(rs!=null){

rs.close();

}

} catch (Exception e) {

// TODO: handle exception

}

try {

if(ps!=null){

ps.close();

}

} catch (Exception e) {

// TODO: handle exception

}

try {

if(con!=null){

con.close();

}

} catch (Exception e) {

// TODO: handle exception

}

}

}

登录页面login.jsp

name
age

主页 Main.jsp

StudentDao stuDao=new StudentDao();

List list=stuDao.getAllStudent();

int i=1;

%>

用户管理系统
idnameage

删除     

修改

%>

增加学生

增加doadd.jsp

request.setCharacterEncoding("utf-8");

String name=request.getParameter("name");

int age=Integer.parseInt(request.getParameter("age"));

Student student=new Student();

student.setName(name);

student.setAge(age);

StudentDao stuDao=new StudentDao();

int result=stuDao.addstudent(student);

%>

删除dodelete.jsp

int id=Integer.parseInt(request.getParameter("id"));

StudentDao stuDao=new StudentDao();

int result=stuDao.delete(id);

%>

修改update.jsp

request.setCharacterEncoding("utf-8");

String name=request.getParameter("name");

int age=Integer.parseInt(request.getParameter("age"));

int id=Integer.parseInt(request.getParameter("id"));

Student student=new Student();

student.setName(request.getParameter("name"));

student.setAge(age);

student.setId(id);

StudentDao stuDao=new StudentDao();

int rs=stuDao.updata(student);

%>

doupdata.jsp

request.setCharacterEncoding("utf-8");

StudentDao stuDao=new StudentDao();

Student student=new Student();

int id=Integer.parseInt(request.getParameter("id"));

student.setId(id);

ResultSet re=stuDao.select(student);

if(re.next()){%>

">
name" name="name">
age" >

登录页面

49ce5e115dbe2abd1416e8d259605d2f.png

主页面

71a59ed5b01941e54afa486d96b137d6.png

去我博客里应该有源码文件、、找找吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值