大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
用Java编写一个员工类程序:1.属性:员工编号,员工姓名,基本工资,奖金,2.构造方法:至少两个。如下:
创新互联为企业级客户提高一站式互联网+设计服务,主要包括成都网站制作、成都网站建设、外贸营销网站建设、app软件定制开发、小程序定制开发、宣传片制作、LOGO设计等,帮助客户快速提升营销能力和企业形象,创新互联各部门都有经验丰富的经验,可以确保每一个作品的质量和创作周期,同时每年都有很多新员工加入,为我们带来大量新的创意。
package com.test;
public class Employee {
/**
* 员工编号
*/
private String number;
/**
* 员工姓名
*/
private String name;
/**
* 员工薪水
*/
private double salary;
/**
* 无参数构造函数
*/
public Employee() {
}
/**
* 给属性赋值构造函数
* @param number
* @param name
* @param salary
*/
public Employee(String number, String name, double salary) {
super();
this.number = number;
this.name = name;
this.salary = salary;
}
public static void main(String[] args) {
//员工一,并且构造函数里设置值
Employee e1 = new Employee("e0001", "xiaoming", 5000.0);
System.out.println("员工一:" + e1);
//员工二,用set设置值,get的话可以获取到员工某个属性
Employee e2 = new Employee();
e2.setName("小二");
e2.setNumber("e0002");
e2.setSalary(5500.1);
System.out.println("员工二:" + e2);
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [number=" + number + ", name=" + name + ", salary=" +
salary + "]";
}
}
运行结果:
员工一:Employee [number=e0001, name=xiaoming, salary=5000.0]
员工二:Employee [number=e0002, name=小二, salary=5500.1]
第一步:新建数据库
连接的是本地localhost,新建一个新的数据库名是jdbctest
然后建表t_emp
不会的话可通过执行下方的sql语句建表
CREATE TABLE `t_emp` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`salary` double DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
第二步:新建java项目
新建完以后添加mysql驱动的jar包,jar包自己下载
在项目上右键鼠标属性,然后
添加jar包,我这里已经加载过了
第三步:编写代码
package com.gf;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class Test {
public static void main(String[] args) throws Exception {
int flag=0;
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取连接
Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest?user=rootpassword=123456useUnicode=truecharacterEncoding=UTF-8");
//3.创建statement
Statement sm=(Statement) conn.createStatement();
//4.执行sql语句
flag=sm.executeUpdate("insert into t_emp(name,salary) values('菲菲',34.9)");
if(flag!=0) {
System.out.println("员工信息增加成功");
}else {
System.out.println("添加失败");
}
}
}
注意点:
---------------------------------------------------------------------------------
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdbctest?user=rootpassword=123456useUnicode=truecharacterEncoding=UTF-8");
这里需要修改自己本机的连接信息,不然会出现连接失败
最后的执行结果
public class Test0 {
public static void main(String[] args) {
Employee ZhangSan = new Employee("001", "张三", '男', "销售部", 6000, 1000, "普通员工");
Employee LiSi = new Employee("002", "李四", '女', "人事部", 7000, 2000, "超级员工");
System.out.println(ZhangSan.toString()); //打印张三信息
System.out.println(LiSi.toString()); //打印李四信息
}
}
class Employee{
private String id; //员工ID
private String name; //姓名
private char sex; //性别
private String department; //部门
private int basic_salary; //基本工资
private int extra_salary; //薪金
private String classify; //类别
/**
* 构造方法
*/
public Employee(String id, String name, char sex, String department,
int basic_salary, int extra_salary, String classify) {
this.id = id;
this.name = name;
this.sex = sex;
this.department = department;
this.basic_salary = basic_salary;
this.extra_salary = extra_salary;
this.classify = classify;
}
public Employee(){
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public int getBasic_salary() {
return basic_salary;
}
public void setBasic_salary(int basic_salary) {
this.basic_salary = basic_salary;
}
public int getExtra_salary() {
return extra_salary;
}
public void setExtra_salary(int extra_salary) {
this.extra_salary = extra_salary;
}
public String getClassify() {
return classify;
}
public void setClassify(String classify) {
this.classify = classify;
}
public String toString(){
String str ="[员工ID:"+id+"|姓名:"+name+"|性别:"+sex+
"|部门:"+department+"|基本工资:+"+basic_salary+
"|薪金:"+extra_salary+"|类别:"+classify+"]";
return str;
}
}