Employee employee1 = new
Employee();
List employees = new ArrayList();
// add employee
employees.add(employee1);
// delete employee
employees.remove(employee1);
// search employee
for each ( emp : employee){
if (emp.name.equals("Ken")){
print("cool");
}
Employee类:
public class Employee {
private int id;
private String name;
private int age;
private String email;
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 getEmail() {
return email;
public void setEmail(String email) {
this.email = email;
Manager类:
import java.util.ArrayList;
import java.util.List;
public class Manager {
public static ListEmployee employees = new ArrayListEmployee();
public static void addEmployee(Employee employee){
employees.add(employee);
public static void deleteEmployee(Employee employee){
employees.remove(employee);
public static void updateEmployee(Employee employee){
int len = employees.size();
for(int i=0;ilen;i++){
Employee e = employees.get(i);
if(e.getId() == employee.getId()){
deleteEmployee(e);
addEmployee(employee);
public static Employee selectEmployeeById(int id){
if(len!=0){
Employee emp = new Employee();
if(e.getId() == id){
emp = e;
return emp;
}else{
return null;
public static Employee selectEmployeeByName(String name){
if(e.getName().equals(name)){
public static void printEmployees(){
if(len != 0){
System.out.println(employees.get(i).getId()+":\t"+
employees.get(i).getName()+"\t"+
employees.get(i).getAge()+"\t"+
employees.get(i).getEmail());
System.out.println("无员工");
测试EmpManaTest类:
public class EmpManaTest {
public static void main(String[] args) {
Employee e = new Employee();
e.setId(1);
e.setName("zz");
Manager.addEmployee(e);
System.out.println("添加员工后:");
Manager.printEmployees();
System.out.println("----------------------------------------------");
Employee emp = new Employee();
emp.setId(1);
emp.setName("virus");
Manager.updateEmployee(emp);
System.out.println("修改员工后:");
System.out.println("查询员工ByID:");
Employee empSelectId = Manager.selectEmployeeById(1);
System.out.println(empSelectId.getName());
System.out.println("查询员工ByName:");
Employee empSelectName = Manager.selectEmployeeByName("virus");
System.out.println(empSelectName.getEmail());
System.out.println("删除一个员工后:");
Manager.deleteEmployee(emp);
这个是不是你想要的...........
第一步:新建数据库
连接的是本地localhost,新建一个新的数据库名是jdbctest
然后建表t_emp
不会的话可通过执行下方的sql语句建表
CREATE TABLE +t_emp+ (
+id+ int(11) NOT NULL AUTO_INCREMENT,
+salary+ double DEFAULT NULL,
PRIMARY KEY (+id+)
第二步:新建java项目
在项目上右键鼠标属性,然后
添加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");
Statement sm=(Statement) conn.createStatement();
if(flag!=0) {
System.out.println("员工信息增加成功");
}else {
System.out.println("添加失败");
注意点:
---------------------------------------------------------------------------------
这里需要修改自己本机的连接信息,不然会出现连接失败
最后的执行结果
从无到有这么编写比较好.我老师给了我一堆东西.叫我往里面添加.我现在都弄不明白里面哪里有错.eclipse太高级了太难学了.
对数据库进行增删改查?
网上搜一下就行
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
class MSC
{
public String MscID;
public String MscName;
public String MscCompany;
public float MscLongitude;
public float MscLatitude;
public float MscAltitude;
public MSC(String MscID, String MscName, String MscCompany,
float MscLongitude, float MscLatitude,float MscAltitude){
this.MscID = MscID;
this.MscName = MscName;
this.MscCompany = MscCompany;
this.MscLongitude =MscLongitude;
this.MscLatitude = MscLatitude;
this.MscAltitude = MscAltitude;
public class sqlserverjdbc {
public Connection getConnection(){
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String userName = "sa"; //默认用户名
Connection dbConn = null;
try {
Class.forName(driverName);
dbConn =DriverManager.getConnection(dbURL, userName, userPwd);
} catch (Exception e) {
e.printStackTrace();
return dbConn;
public void printUserInfo(){
Connection con = getConnection();
Statement sta = null;
ResultSet rs = null;
System.out.println("打印表格MSC信息");
sta = con.createStatement();
rs = sta.executeQuery("select * from MSC信息");
System.out.println("MscID\tMscName\tMscCompany\tMscLongitude\tMscLatitude\tMscAltitude");
while(rs.next()){
System.out.println(rs.getString("MscID")+"\t"+
rs.getString("MscName")+"\t"+
rs.getString("MscCompany")+"\t"+
rs.getFloat("MscLongitude")+"\t"+
rs.getFloat("MscLatitude")+"\t"+
rs.getFloat("MscAltitude"));
con.close();
sta.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
System.out.println("打印完成\n");
public void delete(String MscID){
String sql = "delete from MSC信息 where MscID = " + MscID;
PreparedStatement pst;
System.out.println("删除表格MSC信息中 ID = "+MscID+"的记录");
pst = con.prepareStatement(sql);
pst.execute();
pst.close();
System.out.println("记录删除失败!!!");
System.out.println("记录删除成功!!!\n");
public void insert(MSC msc){
String sql = "insert into MSC信息 values(?,?,?,?,?,?)";
System.out.println("插入一条记录");
pst.setString(1, msc.MscID);
System.out.println("插入失败!!!");
System.out.println("插入成功!!!\n");
//更新MscID的MscName
public void updateMscName(String MscID, String MscName){
String sql = "update MSC信息 set MscName = ? where MscID = ?";
System.out.println("修改一条记录");
pst.setString(1, MscName);
System.out.println("修改失败!!!");
System.out.println("修改成功!!!\n");
public static void main(String args[]){
sqlserverjdbc sql = new sqlserverjdbc();
sql.printUserInfo();
sql.delete("1111");
传不上去.文字太多.
我把代码写到博客上了,你到博客看吧.
jsp+servlet
以上就是土嘎嘎小编为大家整理的增加删除员工java代码相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!