eclipse(maven)与MySQL连接

eclipse(maven)与MySQL连接

    • 首先创建与数据库的连接
    • 然后创建表的实体

首先创建与数据库的连接

eclipse(maven)与MySQL连接_第1张图片

// An highlighted block
package com.graduation.can.utils;

import org.apache.commons.dbcp2.BasicDataSource;

import javax.sql.DataSource;

public class DbUtils {
	
	private static BasicDataSource dataSource;
	
	static {
		
		dataSource = new BasicDataSource();
		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
		dataSource.setUrl("jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf8");
		dataSource.setUsername("root");
		dataSource.setPassword("beanbean");
		
	}
	
	
	public static DataSource getDataSource() {
		
		return dataSource;
	}
	

}

然后创建表的实体

eclipse(maven)与MySQL连接_第2张图片

  1. 初始化
    eclipse(maven)与MySQL连接_第3张图片
  2. 创建类
    步骤一:在声明的类中,单击鼠标右键,选择source:;
    步骤二:选择Generate Getters and Setters;
    步骤三:选择Generate Constructor using Fields;
    步骤四:生成无参构造方法!!!!
    步骤五:根据需要配置,如位置,顺序等,在选择确定即可。
  3. 代码
// An highlighted block
package com.graduation.can.entity;

public class Admin {
	private int id;
	private String name;
	private String tele;
	private byte sex;
	private String address;
	private String pass;
	
	
	
	public Admin(int id, String name, String tele, byte sex, String address, String pass) {
		super();
		this.id = id;
		this.name = name;
		this.tele = tele;
		this.sex = sex;
		this.address = address;
		this.pass = pass;
	}
	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 getTele() {
		return tele;
	}
	public void setTele(String tele) {
		this.tele = tele;
	}
	public byte getSex() {
		return sex;
	}
	public void setSex(byte sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
}

你可能感兴趣的:(eclipse(maven)与MySQL连接)