Java Web实现MySQL数据库增删改查

JavaWeb案例:实现数据库增删改查功能

一、创建数据库与表

Java Web实现MySQL数据库增删改查_第1张图片

创建JavaWeb项目TestStudent:

1.在WEB-INF里创建lib目录,添加数据库驱动jar包:
Java Web实现MySQL数据库增删改查_第2张图片
2.在web目录里创建META-INF子目录,在里面创建数据源配置文件context.xml
Java Web实现MySQL数据库增删改查_第3张图片
3.在src里创建net.lcn.dbutil包,在里面创建ConnectionManager类

package net.lcn.dbutil;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import java.sql.Connection;

import java.sql.SQLException;

public class ConnectionManager {
    /**
     * 私有化方法,拒绝实例化
     */
    private ConnectionManager(){
    }

    /**
     * 获取数据库连接静态方法
     * @return
     */
    public static Connection getConnection(){
        //定义数据库连接
        Connection conn = null;
        try {
            // 定义、初始化上下文
            Context ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/student");
            conn = ds.getConnection();
        } catch (NamingException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //返回数据库连接
        return conn;
    }
}

4.用MVC模式来改造TestStudent项目,添加栏目实体类Student,添加栏目数据访问接口StudentDao及其实现类StudentDaoImpl等操作。
(1)添加栏目实体类Student

package net.lcn.bean;

public class Student {

    private int id;
    private String name;
    private String gender;
    private int age;
    private int telephone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getTelephone() {
        return telephone;
    }

    public void setTelephone(int telephone) {
        this.telephone = telephone;
    }

    @Override
    public String toString() {
        return "stu{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", telephone=" + telephone +
                '}';
    }
}

(2)添加栏目数据访问接口StudentDao:

package net.lcn.dao;

import net.lcn.bean.Student;
import net.lcn.dbutil.ConnectionManager;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {
    /**
     * 查询全部学生
     */
    public List findAllStudents(){
        List students = new ArrayList<>();
        try {
            Connection conn = ConnectionManager.getConnection();
            String strSQL = "select * from student";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(strSQL);
            while (rs.next()){
                Student student = new Student();
                student.setId(rs.getInt("id"));
                student.setName(rs.getString("name"));
                student.setGender(rs.getString("gender"));
                student.setAge(rs.getInt("age"));
                student.setTelephone(rs.getString("telephone"));
                //将学生实体类添加到栏目列表
                students.add(student);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //返回栏目列表
        return students;
    }
    public static void main(String[] args){
        StudentDao studentDao = new StudentDao();
        List students = studentDao.findAllStudents();
        if (students.size() == 0){
            System.out.println("没有学生");
        }else {
            System.out.println("学生数为:"+ students.size());
        }

    }
}

你可能感兴趣的:(Java Web实现MySQL数据库增删改查)