main方法:
private static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
boolean lage = true;
do {
System.out.println("**************手机管理****************");
System.out.println("1.增加手机类型\n2.修改手机类型\n3.查询手机类型\n4.删除手机类型\n5.退出");
System.out.println("**************************************");
System.out.print("请输入:");
int number =input.nextInt() ;
switch (number){
case 1:
new test().Insert();
break;
case 2:
new test().Save();
break;
case 3:
new test().Sel();
break;
case 4:
new test().Del();
break;
case 5:
System.out.println("已退出系统!");
lage = false;
}
}while (lage);
}
//查询
public void Sel(){
System.out.print("请输入编号:");
int id = input.nextInt();
Mester mester = new PetDB().FindOne(id);
System.out.println("编号:"+mester.getId());
System.out.println("品牌型号:"+mester.getName());
System.out.println("参考价格:"+mester.getPrice());
System.out.println("网络模式:"+mester.getWorkMode());
System.out.println("网络类型:"+mester.getWorkType());
System.out.println("外观设计:"+mester.getFacade());
System.out.println("主屏尺寸:"+mester.getSize());
System.out.println("触摸屏:"+mester.getFeelScreen());
}
//修改
public void Save(){
int id;
String name;
Double price;
String workmode,worktype,facade,size,feelscreen;
System.out.print("请输入要修改的编号:");
id=input.nextInt();
Mester mester = new PetDB().FindOne(id);
System.out.println("修改前的属性:"+mester.getName());
System.out.print("请输入修改后的品牌型号:");
name = input.next();
System.out.println("修改前的属性:"+mester.getPrice());
System.out.print("请输入修改后的参考价格:");
price = input.nextDouble();
System.out.println("修改前的属性:"+mester.getWorkMode());
System.out.print("请输入修改后的网络模式:");
workmode = input.next();
System.out.println("修改前的属性:"+mester.getWorkType());
System.out.print("请输入修改后的网络类型:");
worktype = input.next();
System.out.println("修改前的属性:"+mester.getFacade());
System.out.print("请输入修改后的外观设计:");
facade = input.next();
System.out.println("修改前的属性:"+mester.getSize());
System.out.print("请输入修改后的主屏尺寸:");
size = input.next();
System.out.println("修改前的属性:"+mester.getFeelScreen());
System.out.print("请输入修改后的触摸屏:");
feelscreen =input.next();
new PetDB().Update(id,name,price,workmode,worktype,facade,size,feelscreen);
}
//删除
public void Del(){
System.out.print("请输入删除手机型号的编号:");
int id = input.nextInt();
new PetDB().Del(id);
}
//添加
public void Insert(){
String name,workmode,worktype,facade,size,feelscreen;
Double price;
System.out.print("请输入添加的品牌型号:");
name = input.next();
System.out.print("请输入添加的参考价格:");
price = input.nextDouble();;
System.out.print("请输入添加的网络模式:");
workmode = input.next();
System.out.print("请输入添加的网络类型:");
worktype = input.next();
System.out.print("请输入添加的外观设计:");
facade = input.next();
System.out.print("请输入添加的主屏尺寸:");
size = input.next();
System.out.print("请输入添加的触摸屏:");
feelscreen =input.next();
new PetDB().Save(name,price,workmode,worktype,facade,size,feelscreen);
}
逻辑层:
import lombok.Setter;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Mester {
//编号
private int id;
//品牌
private String name;
//价格
private double price;
//配置
private String workMode;
//卡槽
private String workType;
//类型
private String facade;
//尺寸
private String size;
//触摸屏
private String feelScreen;
}
业务实现层
import java.sql.*;
import java.util.Scanner;
public class PetDB {
private Scanner input = new Scanner(System.in);
//增加
public void Save(String name,Double price,String workmode,String worktype,String facade,String size,String feelscreen){
String sql = String.format("insert into handset values(null,"+"‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘)",
name,price,workmode,worktype,facade,size,feelscreen);
Connection con = null;
try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//创建数据库连接
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phone","root","root");
Statement statement = con.createStatement();
int result = statement.executeUpdate(sql);
if (result==1){
System.out.println("增加成功!");
}else
System.out.println("增加失败!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//修改
public void Update(int id,String name,Double price,String workmode,String worktype,String facade,String size,String feelscreen){
String sql = String.format("update handset set nameType = ‘%s‘, price = ‘%s‘,networkMode = ‘%s‘,networkType = ‘%s‘,facade = ‘%s‘,screenSize = ‘%s‘,feelScreen = ‘%s‘ where hsId = %s",
name,price,workmode,worktype,facade,size,feelscreen,id);
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phone","root","root");
Statement statement = con.createStatement();
int result = statement.executeUpdate(sql);
if (result==1){
System.out.println("修改成功!");
}else
System.out.println("修改失败!");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//删除
public void Del(int id){
String sql = "delete from handset where hsId = "+id;
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phone","root","root");
Statement statement = con.createStatement();
int result = statement.executeUpdate(sql);
if (result==1){
System.out.println("删除成功!");
}else
System.out.println("删除失败!");
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询
public Mester FindOne(int id){
Mester mester = null;
String sql = String.format("select * from handset where hsId = %s",id);
Connection con = null;
try {
//加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//创建数据库连接
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phone","root","root");
Statement statement = con.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
mester = new Mester();
mester.setId(resultSet.getInt(1));
mester.setName(resultSet.getString(2));
mester.setPrice(resultSet.getDouble(3));
mester.setWorkMode(resultSet.getString(4));
mester.setWorkType(resultSet.getString(5));
mester.setFacade(resultSet.getString(6));
mester.setSize(resultSet.getString(7));
mester.setFeelScreen(resultSet.getString(8));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return mester;
}