java中CRUD(增删查改)底层代码的实现
java中CRUD(增删查改)底层代码的实现:
package com.station.dao;
import com.station.model.Product;
import java.sql.*;
public class ProductDao {
//增加产品:
public void add(String product_name, double sale_price, double cost_price) {
System.out.println("product_name=" + product_name + "," + "sale_price=" + sale_price + "," + "cost_price=" + cost_price);
try {
//加载:
Class.forName("com.mysql.jdbc.Driver");
//连接:
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin");
//创建预编译语句:
String sql = "INSERT INTO product(product_name,sale_price,cost_price) VALUE (?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, product_name);
preparedStatement.setDouble(2, sale_price);
preparedStatement.setDouble(3, cost_price);
//执行语句:
preparedStatement.executeUpdate();
//释放资源:
preparedStatement.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//删除产品:
public void delete(int id) {
System.out.println("id=" + id);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin");
String sql = "DELETE FROM product WHERE id=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//修改产品:
public void update(int id, String product_name, double cutoff, double cost_price) {
System.out.println("id=" + id + "," + "product_name=" + product_name);
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin");
String sql = "UPDATE product SET product_name=?,cutoff=?,cost_price=? WHERE id=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, product_name);
preparedStatement.setDouble(2, cutoff);
preparedStatement.setDouble(3, cost_price);
preparedStatement.setInt(4, id);
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
//查询产品:
public Product select(int id) {
Product product = new Product();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin");
String sql = "SELECT id,product_name,sale_price FROM product WHERE id=?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,id);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
int id1 = resultSet.getInt("id");
String product_name = resultSet.getString("product_name");
double sale_price = resultSet.getDouble("sale_price");
product.setId(id1);
product.setProductName(product_name);
product.setSalePrice(sale_price);
}
resultSet.close();
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
return product;
}
}
java中CRUD(增删查改)底层代码的实现的更多相关文章
- js中数组增删查改unshift、push、pop、shift、slice、indexOf、concat、join
js中数组增删查改unshift.push.pop.shift.slice.indexOf.concat.join
- MongoDB在Java下的增删查改
我们总不能一直使用cmd对数据库操作,数据库总是要在程序中使用的.今天来说一下怎么通过Java调用MongoDB. 学习一下最基本也是最常用的增删查改语句,这是使用数据库的基础. 注意事项: 1.要打 ...
- MongoDB 学习笔记(二):shell中执行增删查改
一.查 1.查询集合中所有文档:db.集合名.find(). 2.查询集合中第一个文档:db.集合名.findOne(). 3.指定查询条件:第一个参数就是指定查询条件 查询全部文档:db.集合名.f ...
- Ubuntu 中 iptables 增删查改
iptables是linux系统自带的防火墙,功能强大.如果iptables不熟悉的话可以用apf,是一款基于iptables的防墙. 一.安装并启动防火墙 $ /etc/init.d/iptable ...
- c#中xml增删查改
/// <summary> /// xml转list /// </summary> /// <typeparam name="T">目标对象&l ...
- Nhibernate入门篇连接Sqlserver的增删查改
第一步:创建数据库 create table Emp( EmpId int primary key identity, EmpName ), EmpDate date ) 第二步:去官网下载:http ...
- backbonejs mvc框架的增删查改实例
一:开发环境 coffeescript和nodejs需要先安装,没装网上自己查安装步骤. 代码编写环境及esp框架下载: esp框架下载地址:https://github.com/nonocast/e ...
- EasyUI的增删查改(后台ASP.NET)
转自:http://www.cnblogs.com/dedeyi/archive/2013/04/22/3035057.html 某某人曾经跟我说,你们做系统不就是增删查改吗. 是啊,很多时候我们就是 ...
- jdbc的实例应用:增删查改实现
//在jdbc中进行增删查改 //查看所有 public static void findAll() { String url = "jdbc:mysql://localhost:3306/ ...
随机推荐
- ACDsee的安装过程
http://www.ddooo.com/softdown/76175.htm ACDSee 18中文版安装教程: 1.ACDSee 18分为32位和64位版本,我们先选择合适系统的中文版本开始安装, ...
- Windows系统下PHP使用Redis
参考链接:https://www.cnblogs.com/lhat/p/6402472.html 环境:windows 10 64位操作系统 php 5.4 redis 3.0 1.redis ...
- IDEA 操作及快捷键总结
一.设置IDEA使用Eclipse快捷键 File->Settings->Keymap->选择Eclipse,就可以使用Eclipse的快捷键了,但是不能修改.如果想要修改,需要点击 ...
- 一个很初级的错误 Destructor忘记override导致内存泄露
TxxObj= class public Destructor Destroy(); override;!!!此处若无override,将导致内存泄露 end; Destru ...
- Bootstrap图像
前面的话 图像在网页制作中也是常要用到的元素,本文将详细介绍Bootstrap图像 响应式图片 通过为图片添加 .img-responsive 类可以让图片支持响应式布局.其实质是为图片设置了 max ...
- Luogu4782 【模板】2-SAT 问题(2-SAT)
模板.注意若x=y不一定是废话,x=0或x=0表示x必须为0.以及数组开2n. #include<iostream> #include<cstdio> #include< ...
- MarkdownPad 注册码 Version 2.5.0.27920
[注册码] 还望多多支持正版 邮箱地址: Soar360@live.com 授权秘钥: GBPduHjWfJU1mZqcPM3BikjYKF6xKhlKIys3i1MU2eJHqWGImDHzWdD6 ...
- Partition Numbers的计算
partition numbers的定义 A000041 就是将正整数n分为k(\(1\le k\le n)\)个正整数相加,即\(n=a_1+a_2+...+a_k\)且\(a_1\le a_2\l ...
- 1.Zabbix报错信息:It probably means that the systems requires more physical memory.
点击返回:自学Zabbix之路 1.Zabbix报错信息:It probably means that the systems requires more physical memory. 1.报错信 ...
- 自学Zabbix11.2 Zabbix SNMP安装
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 自学Zabbix11.2 Zabbix SNMP安装 1. yum安装snmp 1 # yum i ...