JDBC实现简单的增删改查(Mysql连接&servlet实现)

JDBC实现简单的增删改查(servlet实现)——数据库连接

项目中包 类等结构:

在这里插入图片描述

数据库数据:
在这里插入图片描述
在这里插入图片描述

dept(部门表)封装:

package com.hp.bean;

public class Dept {
    private Integer id;//部门编号
    private String name;//部门名称

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

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

    public Dept(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Dept() {
    }
}

emp(员工表)封装

package com.hp.bean;

import javafx.scene.DepthTest;

import java.util.Date;

public class Emp {

    private Integer id;//员工编号
    private String name;//员工姓名
    private String password;//密码
    private String birthday;//出生日期
    private String sex;//性别

    //如果要关联部门信息
    private Dept dept;

    public Emp(Integer id, String name, String password, String birthday, String sex, Dept dept) {
        this.id = id;
        this.name = name;
        this.password = password;
        this.birthday = birthday;
        this.sex = sex;
        this.dept = dept;
    }

    public Emp() {
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getBirthday() {
        return birthday;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", birthday='" + birthday + '\'' +
                ", sex='" + sex + '\'' +
                ", dept=" + dept +
                '}';
    }
}

/**
 * 连接数据库工具类
 */
public class DBHelper {

//连接到MySQL
    static String url="jdbc:mysql://localhost:3306/mydb"//链接地址+数据库名
    static String username="root";  // MySQL登录账号
    static String password="root";   //MySQL登录密码
    static String driver="com.mysql.jdbc.Driver"; //驱动

    //通过静态代码块 加载驱动
    static {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    //拿到数据库连接对象
    public Connection getConnetion(){
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(url, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

//执行main方法   测试数据库连接  如果后台打印一个地址说明连接成功
    public static void main(String[] args) {
        System.out.println(getConnetion());
    }

}

连接成功:
在这里插入图片描述
连接失败:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值