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(增删查改)底层代码的实现的更多相关文章

  1. js中数组增删查改unshift、push、pop、shift、slice、indexOf、concat、join

    js中数组增删查改unshift.push.pop.shift.slice.indexOf.concat.join

  2. MongoDB在Java下的增删查改

    我们总不能一直使用cmd对数据库操作,数据库总是要在程序中使用的.今天来说一下怎么通过Java调用MongoDB. 学习一下最基本也是最常用的增删查改语句,这是使用数据库的基础. 注意事项: 1.要打 ...

  3. MongoDB 学习笔记(二):shell中执行增删查改

    一.查 1.查询集合中所有文档:db.集合名.find(). 2.查询集合中第一个文档:db.集合名.findOne(). 3.指定查询条件:第一个参数就是指定查询条件 查询全部文档:db.集合名.f ...

  4. Ubuntu 中 iptables 增删查改

    iptables是linux系统自带的防火墙,功能强大.如果iptables不熟悉的话可以用apf,是一款基于iptables的防墙. 一.安装并启动防火墙 $ /etc/init.d/iptable ...

  5. c#中xml增删查改

    /// <summary> /// xml转list /// </summary> /// <typeparam name="T">目标对象&l ...

  6. Nhibernate入门篇连接Sqlserver的增删查改

    第一步:创建数据库 create table Emp( EmpId int primary key identity, EmpName ), EmpDate date ) 第二步:去官网下载:http ...

  7. backbonejs mvc框架的增删查改实例

    一:开发环境 coffeescript和nodejs需要先安装,没装网上自己查安装步骤. 代码编写环境及esp框架下载: esp框架下载地址:https://github.com/nonocast/e ...

  8. EasyUI的增删查改(后台ASP.NET)

    转自:http://www.cnblogs.com/dedeyi/archive/2013/04/22/3035057.html 某某人曾经跟我说,你们做系统不就是增删查改吗. 是啊,很多时候我们就是 ...

  9. jdbc的实例应用:增删查改实现

    //在jdbc中进行增删查改 //查看所有 public static void findAll() { String url = "jdbc:mysql://localhost:3306/ ...

随机推荐

  1. Java与JavaScript 完美实现字符串拆分(利用数组存储)与合并的互逆操作

    Java: String typeStr = "1,2"; String[] typeArray = typeStr.split(","); typeStr = ...

  2. Docker安装指定版本

    今天新增一个Docker服务器,Docker安装顺利,启动hello-world测试的时候却出现了问题: $ docker run hello-worldUnable to find image 'h ...

  3. 封装caffe版的deeplab为库供第三方使用

    1.解决deeplab编译问题 http://m.2cto.com/kf/201612/579545.html

  4. codeforces472B

    Design Tutorial: Learn from Life CodeForces - 472B One way to create a task is to learn from life. Y ...

  5. codeforces369A

    Valera and Plates CodeForces - 369A Valera is a lazy student. He has m clean bowls and k clean plate ...

  6. 一本通1619【例 1】Prime Distance

    1619: [例 1]Prime Distance 题目描述 原题来自:Waterloo local,题面详见 POJ 2689 给定两个整数 L,R,求闭区间 [L,R] 中相邻两个质数差值最小的数 ...

  7. VMware下Centos7快速搭建vsftpd

    最简单快捷的实现ftp的功能,不考虑安全问题. 1.配置防火墙和selinux vi /etc/selinux/config # This file controls the state of SEL ...

  8. Ant Trip HDU - 3018(欧拉路的个数 + 并查集)

    题意: Ant Tony和他的朋友们想游览蚂蚁国各地. 给你蚂蚁国的N个点和M条边,现在问你至少要几笔才能所有边都画一遍.(一笔画的时候笔不离开纸) 保证这M条边都不同且不会存在同一点的自环边. 也就 ...

  9. mybatis There is no getter for property named '*' in 'class java.lang.String

    1.原因 server层     xxxx.get("1234") map <if test="aaa != null and aaa.id != null and ...

  10. MT【32】内外圆(Apollonius Circle)的几何证明

    另一方面,如果 M 满足(1)式,那么M必然在以PQ为直径的圆上.事实上当M为P或者Q时,这是显然的.当M异于P,Q时,由$\frac{|MB|}{|MC|}=\frac{|PB|}{|PC|}=\l ...