package controller;
import model.Cars;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
/**
* 汽车dao类
*/
public class CarDao {
/**
* 获取所有汽车信息
* @param connection
* @return
* @throws SQLException
*/
public List<Cars> getAllCars(Connection connection) throws SQLException {
List<Cars> resCar = new ArrayList<Cars>();
Cars currentCar = null;
String sql = "select * from cars";
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery(sql);
while(res.next()){
String plate = res.getString("plate");
String model = res.getString("model");
String owner = res.getString("owner");
String tel = res.getString("tel");
String color = res.getString("color");
currentCar = new Cars(plate,model,owner,tel,color);
resCar.add(currentCar);
}
return resCar;
}
/**
* 插入汽车数据,汽车入库
* @param connection
* @param cars
* @throws SQLException
*/
public void addCar(Connection connection, Cars cars) throws SQLException {
String sql = "insert into cars values (\"plate\",\"model\",\"owner\",\"tel\",\"color\")";
sql = sql.replace("plate",cars.getPlate());
sql = sql.replace("model",cars.getModel());
sql = sql.replace("owner",cars.getOwner());
sql = sql.replace("tel",cars.getTel());
sql = sql.replace("color",cars.getColor());
System.out.println(sql);
Statement statement = connection.createStatement();
statement.executeUpdate(sql);
}
/**
* 删除汽车数据
* @param connection
* @param plate
* @return
* @throws SQLException
*/
public boolean subCar(Connection connection,String plate) throws SQLException {
String sql = "delete from cars where plate = \"xxx\";";
sql = sql.replace("xxx",plate);
Statement statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(sql);
if(row!=0){
return true;
}else {
return false;
}
}
/**
* 查询汽车数据
* @param connection
* @param condition
* @param type
* @return
* @throws SQLException
*/
public List<Cars> findCar(Connection connection, String condition, int type) throws SQLException {
String sql = null;
List<Cars> resCar = new ArrayList<>();
Cars currentCar = null;
switch (type){
case 1:
sql = "select *from cars where plate like \"%condition%\"";
break;
case 2:
sql = "select *from cars where model like \"%condition%\"";
break;
case 3:
sql = "select *from cars where owner like \"%condition%\"";
break;
case 4:
sql = "select *from cars where tel like \"%condition%\"";
break;
case 5:
sql = "select *from cars where color like \"%condition%\"";
break;
default:
return resCar;
}
sql = sql.replace("condition",condition);
System.out.println(sql);
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery(sql);
while(res.next()){
String plate = res.getString("plate");
String model = res.getString("model");
String owner = res.getString("owner");
String tel = res.getString("tel");
String color = res.getString("color");
currentCar = new Cars(plate,model,owner,tel,color);
resCar.add(currentCar);
}
return resCar;
}
/**
* 查询单条学生信息 用于修改
* @param connection
* @param p
* @return
* @throws SQLException
*/
public Cars findModifyTarget(Connection connection, String p) throws SQLException {
Cars TargetCar = null;
String sql = "select *from cars where plate = \"???\";";
sql = sql.replace("???",p);
System.out.println(sql);
Statement statement = connection.createStatement();
ResultSet res = statement.executeQuery(sql);
while(res.next()){
String plate = res.getString("plate");
String model = res.getString("model");
String owner = res.getString("owner");
String tel = res.getString("tel");
String color = res.getString("color");
TargetCar = new Cars(plate,model,owner,tel,color);
}
return TargetCar;
}
/**
* 更新数据
* @param connection
* @param handledTarget
* @return
* @throws SQLException
*/
public boolean modifyTarget(Connection connection, Cars handledTarget) throws SQLException {
String sql = "update cars set model=\"@\",owner=\"#\",tel=\"$\",color=\"%\" where plate=\"^\";";
sql = sql.replace("@",handledTarget.getModel());
sql = sql.replace("#",handledTarget.getOwner());
sql = sql.replace("$",handledTarget.getTel());
sql = sql.replace("%",handledTarget.getColor());
sql = sql.replace("^",handledTarget.getPlate());
System.out.println(sql);
Statement statement = connection.createStatement();
//执行更新返回影响行数
int res = statement.executeUpdate(sql);
if(res>0){
return true;
}else {
return false;
}
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// Connection connection =new DButil().getCon();
// List<Cars> cars = findCar(connection,"车",2);
// for(int i=0; i<cars.size(); i++){
// System.out.println(cars.get(i).getPlate());
// }
String sql = "update cars set model=\"@\",owner=\"#\",tel=\"$\",color=\"%\" where plate=\"^\";";
sql = sql.replace("@","1");
sql = sql.replace("#","####");
sql = sql.replace("$","$$$$");
System.out.println(sql);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于Java+Swing+Mysql实现汽车信息管理系统
共52个文件
class:23个
java:17个
xml:5个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 6 下载量 161 浏览量
2022-06-09
17:01:16
上传
评论 4
收藏 5.07MB RAR 举报
温馨提示
系统实现汽车入库,汽车出库,汽车查询,汽车信息修改和管理员登录等功能 详情:https://blog.csdn.net/qq_33037637/article/details/125206696
资源推荐
资源详情
资源评论
收起资源包目录
基于Java+Swing+Mysql实现汽车信息管理系统.rar (52个子文件)
基于Java+Swing+Mysql实现汽车信息管理系统
car-master
jdbc
mysql-connector-java-8.0.28.jar 2.36MB
mysql-connector-java-8.0.21.jar 2.29MB
src
Entry.java 146B
dao
UserDao.java 1024B
CarDao.java 6KB
model
User.java 720B
Cars.java 1KB
asset
login.png 372KB
views
QueryCar.java 2KB
MainTable.java 4KB
Login.java 4KB
ModifyCar.java 4KB
AddCar.java 5KB
Actions
RemoveCarListener.java 2KB
ExitListener.java 569B
SearchCarListener.java 2KB
ModifyCarListener.java 2KB
SubmitAddCarListener.java 2KB
utils
DButil.java 1KB
VarifyInput.java 208B
.idea
uiDesigner.xml 9KB
misc.xml 273B
modules.xml 256B
workspace.xml 4KB
.gitignore 176B
libraries
mysql_connector_java_8_0_21.xml 253B
out
production
carMange
Entry.class 392B
model
User.class 982B
Cars.class 1KB
asset
login.png 372KB
views
ModifyCar$1.class 1KB
AddCar.class 4KB
MainTable.class 5KB
AddCar$5.class 1KB
AddCar$1.class 1KB
AddCar$3.class 1KB
QueryCar.class 2KB
AddCar$2.class 1KB
AddCar$4.class 1KB
ModifyCar.class 5KB
Actions
ModifyCarListener.class 3KB
RemoveCarListener.class 2KB
SubmitAddCarListener.class 3KB
SearchCarListener.class 3KB
ExitListener.class 1KB
Login.class 5KB
controller
UserDao.class 2KB
CarDao.class 5KB
utils
VarifyInput.class 556B
DButil.class 2KB
carMange.iml 508B
车辆管理系统.sql 3KB
共 52 条
- 1
五星资源
- 粉丝: 5525
- 资源: 1533
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页