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){
int len = employees.size();
if(len!=0){
Employee emp = new Employee();
for(int i=0;ilen;i++){
Employee e = employees.get(i);
if(e.getId() == id){
emp = e;
}
}
return emp;
}else{
return null;
}
}
public static Employee selectEmployeeByName(String name){
int len = employees.size();
if(len!=0){
Employee emp = new Employee();
for(int i=0;ilen;i++){
Employee e = employees.get(i);
if(e.getName().equals(name)){
emp = e;
}
}
return emp;
}else{
return null;
}
}
public static void printEmployees(){
int len = employees.size();
if(len != 0){
for(int i=0;ilen;i++){
System.out.println(employees.get(i).getId()+":\t"+
employees.get(i).getName()+"\t"+
employees.get(i).getAge()+"\t"+
employees.get(i).getEmail());
}
}else{
System.out.println("無員工");
}
}
}
測試EmpManaTest類:
public class EmpManaTest {
public static void main(String[] args) {
Employee e = new Employee();
e.setId(1);
e.setName("zz");
e.setAge(20);
e.setEmail("zz@163.com");
Manager.addEmployee(e);
Employee e2 = new Employee();
e2.setId(2);
e2.setName("scof");
e2.setAge(18);
e2.setEmail("scofield@mail.com");
Manager.addEmployee(e2);
System.out.println("添加員工后:");
Manager.printEmployees();
System.out.println("----------------------------------------------");
Employee emp = new Employee();
emp.setId(1);
emp.setName("virus");
emp.setAge(30);
emp.setEmail("virus@163.com");
Manager.updateEmployee(emp);
System.out.println("修改員工后:");
Manager.printEmployees();
System.out.println("----------------------------------------------");
System.out.println("查詢員工ByID:");
Employee empSelectId = Manager.selectEmployeeById(1);
System.out.println(empSelectId.getName());
System.out.println("----------------------------------------------");
System.out.println("查詢員工ByName:");
Employee empSelectName = Manager.selectEmployeeByName("virus");
System.out.println(empSelectName.getEmail());
System.out.println("----------------------------------------------");
System.out.println("刪除一個員工后:");
Manager.deleteEmployee(emp);
Manager.printEmployees();
}
}
這個是不是你想要的...........
第一步:新建數據庫
連接的是本地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");
這里需要修改自己本機的連接信息,不然會出現連接失敗
最后的執行結果
從無到有這么編寫比較好。我老師給了我一堆東西。叫我往里面添加。我現在都弄不明白里面哪里有錯。eclipse太高級了太難學了。
對數據庫進行增刪改查?
以下是 sql server 2013+java.實現的是對MSC對象的增刪改查.
需要下載連接驅動程序:com.microsoft.sqlserver.jdbc.SQLServerDriver
網上搜一下就行
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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 dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=gsm"; //連接服務器和數據庫sample
String userName = "sa"; //默認用戶名
String userPwd = "123"; //密碼
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信息");
try {
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
e.printStackTrace();
}
System.out.println("打印完成\n");
}
public void delete(String MscID){
Connection con = getConnection();
String sql = "delete from MSC信息 where MscID = " + MscID;
PreparedStatement pst;
System.out.println("刪除表格MSC信息中 ID = "+MscID+"的記錄");
try {
pst = con.prepareStatement(sql);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("記錄刪除失敗!!!");
}
System.out.println("記錄刪除成功!!!\n");
}
public void insert(MSC msc){
Connection con = getConnection();
String sql = "insert into MSC信息 values(?,?,?,?,?,?)";
PreparedStatement pst;
System.out.println("插入一條記錄");
try {
pst = con.prepareStatement(sql);
pst.setString(1, msc.MscID);
pst.setString(2, msc.MscName);
pst.setString(3, msc.MscCompany);
pst.setFloat(4, msc.MscLongitude);
pst.setFloat(5, msc.MscLatitude);
pst.setFloat(6, msc.MscAltitude);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("插入失敗!!!");
}
System.out.println("插入成功!!!\n");
}
//更新MscID的MscName
public void updateMscName(String MscID, String MscName){
Connection con = getConnection();
String sql = "update MSC信息 set MscName = ? where MscID = ?";
PreparedStatement pst;
System.out.println("修改一條記錄");
try {
pst = con.prepareStatement(sql);
pst.setString(1, MscName);
pst.setString(2, MscID);
pst.execute();
pst.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("修改失敗!!!");
}
System.out.println("修改成功!!!\n");
}
public static void main(String args[]){
sqlserverjdbc sql = new sqlserverjdbc();
sql.printUserInfo();
sql.delete("1111");
sql.printUserInfo();
sql.updateMscName("5215", "聯想");
sql.printUserInfo();
sql.insert(new MSC("1111", "中興" ," 中興", (float)12.2, (float)3.4,(float)45.5));
sql.printUserInfo();
}
}
傳不上去。文字太多。
我把代碼寫到博客上了,你到博客看吧。
jsp+servlet
當前名稱:增加刪除員工java代碼,增加刪除員工java代碼的方法
文章來源:http://m.kartarina.com/article12/heiddc.html
成都網站建設公司_創新互聯,為您提供網站設計、營銷型網站建設、域名注冊、自適應網站、ChatGPT、品牌網站建設
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯