java JDBC连接数据库 实现数据的增删改查

在这里举例 对一个student数据库的数据进行增删改查

import Bean.Student;
import com.mysql.cj.jdbc.Driver;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
//主类 测试运行结果
public class Util {
     
    public static void main(String[] args){
     
        try {
     
        //导入mysql库
            Class.forName("com.mysql.jdbc.Driver");
            //获得对应连接
            GetDataServer();
            //插入操作 修改和删除雷同 只需要修改函数中的sql语句段即可
            Student student=new Student(87,"lisiwu",99,"2020-04-08");
            InsertStudent(student);
            //定义一个数组存储查询结果
            List<Student> list=selectStudent();
            if (list != null) {
     
            //输出查询结果
                System.out.println(list.toString());
            }
        } catch (ClassNotFoundException e) {
     
            e.printStackTrace();
        }

    }
//连接数据库 方法
    private static Connection GetDataServer() {
     

        try {
     
        //获得Connection对象 student为数据库名 UTC是数据库时间 后面两个参数分别是数据库的账号和密码,一般账号都默认为root
            Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/student?serverTimezone=UTC","root","123456");
            //输出con对象
            System.out.println(con);
            //返回con
            return con;
        } catch (SQLException e) {
     
            e.printStackTrace();
        }
        return null;
    }
    //对数据库进行查询 并且将对应的结果返回到一个list数组中
    public static List<Student> selectStudent(){
     
    //获得连接
        Connection connection=GetDataServer();
        //编辑数据库语句
        String sql="select * from student";
        //声明一个数组 来存取查询结果
        List<Student> list=new ArrayList<Student>();
        //建立statement对象和resuleset结果对象
        Statement statement=null;
        ResultSet resultSet=null;
        try {
     
            if (connection != null) {
     
            //建立声明对象
                statement=connection.createStatement();
            }
            if (statement != null) {
     
            //执行相关sql语句并且返回给结果参数
                resultSet=statement.executeQuery(sql);
            }
            //用ResultSet的next方法判断是否有下一个
            while (resultSet.next()){
     
            //将相关的属性值引出来 并且存入到student对象中
                int id=resultSet.getInt("id");
                String s_name=resultSet.getString("s_name");
                int age=resultSet.getInt("a_age");
                String birthday=resultSet.getString("birthday");
                Student student=new Student(id,s_name,age,birthday);
                list.add(student);
            }
            //返回list数组
            return list;
        } catch (SQLException throwables) {
     
            throwables.printStackTrace();
        }finally {
     
            try {
     
            //关闭资源
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
     
                throwables.printStackTrace();
            }
        }
        return null;
    }
//增加记录
    public static void InsertStudent(Student student){
     
    //建立连接
        Connection connection=GetDataServer();
        //编辑sql语句
        String sql="INSERT INTO student VALUES("+student.getId()+",'"+student.getName()+"',"+student.getAge()+",'"+student.getBirthday()+"')";
        Statement statement=null;
        int resultSet;
        try {
     
        //建立声明对象 和结果参数 注意 这里的结果参数返回的是一个int类型
                statement=connection.createStatement();
                //执行sql语句 更新数据库
                resultSet=statement.executeUpdate(sql);
                System.out.println(resultSet);
        } catch (SQLException throwables) {
     
            throwables.printStackTrace();
        }finally {
     
            try {
     
            //关闭资源
                statement.close();
                connection.close();
            } catch (SQLException throwables) {
     
                throwables.printStackTrace();
            }
        }

    }
}

//javabean 用来存储对应的数据库属性值,并且要有无参和有参两种构造方法 重写tostring方便输出查看结果
package Bean;

public class Student {
     
    private int id;
    private String name;
    private int age;
    private String birthday;

    public Student() {
     
    }

    public Student(int id, String name, int age, String birthday) {
     
        this.id = id;
        this.name = name;
        this.age = age;
        this.birthday = birthday;
    }

    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 int getAge() {
     
        return age;
    }

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

    public String getBirthday() {
     
        return birthday;
    }

    public void setBirthday(String birthday) {
     
        this.birthday = birthday;
    }

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

你可能感兴趣的:(java,java,mysql,jdbc)