一、功能描述
创建一个熟食包点的数据库,我的任务是通过名称修改数据库内容的功能;对eatmarket中的eat数据表进行修改;通过查找名称(eatname)对其他信息如价格和库存进行修改(eatprice spare),将数据表中的内容实现更新;在UI界面创建一个与用户交互的菜单,会有提示告知用户需要进行的具体操作,如输入特定数字进入修改功能,然后输入修改名称,选择要修改的某个内容,输入想要修改的新内容,即可在数据表当中实现内容的更改。
二、功能实现
JDBCUtils.java
package foodmarketMis;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCUtils {
public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/foodmarket?characterEncoding=UTF-8";
public static final String USERNAME = "root";
public static final String PASSWORD = "321";
public static Connection conn=null ;
//使用静态代码块给静态常量成员赋初值
static{
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection(){
return conn;
}
}
UserBean.java
public class UserBean {
private String uid;
private String eatname;//用户名
private String eatprice;//密码
private String spare;
//构造方法,带参数
public UserBean(String uid, String eatname, String eatprice,String spare) {
this.uid = uid;
this.eatname = eatname;
this.eatprice = eatprice;
this.spare = spare;
}
//set和get
public String getNumber() {
return uid;
}
public void setNumber(String uid) {
this.uid = uid;
}
public String geteatname() {
return eatname;
}
public void setName(String eatname) {
this.eatname = eatname;
}
public String geteatprice() {
return eatprice;
}
public void seteatprice(String eatprice) {
this.eatprice = eatprice;
}
public String getspare() {
return spare;
}
public void setspare(String spare) {
this.spare = spare;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return uid+"\t---\t"+eatname+"\t---\t"+eatprice+"\t---\t"+spare;
}
}
UserDAO.java
//import com.mysql.jdbc.Connection;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class UserDAO {
//Connection conn=(Connection) JDBCUtlis.getConnection();
Connection conn = (Connection) JDBCUtils.getConnection();
public void searchByupdatename(UserBean user) throws SQLException {
//SET Address = 'Zhongshan 23', City = 'Nanjing' WHERE LastName = 'Wilson'
//String sql ="update eat(uid,eatname,eatprice,spare) VALUES(?,?,?,?)";
String sql ="update eat set uid=?,eatprice=?,spare=? where eatname=?";
PreparedStatement pstmt =(PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, user.getNumber());
pstmt.setString(2, user.geteatprice());
pstmt.setString(3, user.getspare());
pstmt.setString(4, user.geteatname());
pstmt.executeUpdate();
}
}
UserSerivce.java
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserService {
UserDAO dao = new UserDAO();
public void updatename(UserBean user) throws SQLException {
// TODO Auto-generated method stub
dao.searchByupdatename(user);
}
}
UserUI.java
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class UserUI {
static UserService service = new UserService();
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
boolean flag=true;
while (flag) {
//菜单
System.out.println("-----熟食包点------");
System.out.println("--1-添加 ------");
System.out.println("--2-删除 ------");
System.out.println("--3-修改 ------");
System.out.println("--4-查询 ------");
System.out.println("--5-退出 ------");
System.out.print("请输入数字,选择菜单功能:");
// 输入选项
int inputNumber;
Scanner input = new Scanner(System.in);
inputNumber = input.nextInt();// 输入整数
switch (inputNumber) {
case 1:
addUser();
break;
case 2:
deletezhong();
break;
case 3:
updatazhong();
break;
case 4:
searchAllUserxian();
break;
case 5:
flag=false;
break;
}
}
}
// 修改中间
private static void updatazhong() throws SQLException {
System.out.println("--1-按照编号修改 ------");
System.out.println("--2-按照名称修改 ------");
int inputNumber;
Scanner input = new Scanner(System.in);
inputNumber = input.nextInt();// 输入整数
switch (inputNumber) {
case 1:
updateid();
break;
case 2:
updatename();
break;
}
}
//按照名称修改
private static void updatename() throws SQLException {
// TODO Auto-generated method stub
System.out.println("输入要修改的食物名称:");
Scanner in = new Scanner(System.in);
String inputeatname=in.next();
UserBean user= service.searchByname(inputeatname);
if(user!=null)
{
System.out.println(user.getNumber()+"\t"+user.geteatname()+"\t"+user.geteatprice()+"\t"+user.getspare());
System.out.println("输入新的食物编号:");
String inputnewid=in.next();
user.setNumber(inputnewid);
// System.out.println("输入新的食物名称:");
// String inputnewname=in.next();
// user.setName(inputnewname);
System.out.println("输入新的食物价格:");
String inputnewprice=in.next();
user.seteatprice(inputnewprice);
System.out.println("输入新的食物库存:");
String inputnewspare=in.next();
user.setspare(inputnewspare);
service.updatename(user);
}
else
{
System.out.println("没有找到相关信息!");
}
}
}
三、运行结果
开始截图
结束
四、总结
在实验过程中,有一些因为不够细心导致的错误,添加中文符号等,也有一些语法错误,通过仔细对比代码,网上搜索,观看老师的课程回放以及向小组内的同学们询问均得以解决;通过这次实验,使我的Java基础更加牢固,掌握了以前没有学会的知识点,也学会了通过Java来对数据库进行增删改查。