数据库的连接及长用的SQL语句

package com.lyz.commodity.dao;

import com.lyz.commodity.commodityModel.CommodityModel;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class commoditySql {
    String url="jdbc:mysql://localhost/shopping";
    String name="root";
    String pass="123456";
    Connection connection=null;
    Statement statement=null;
    ResultSet resultSet=null;
    public Connection getConnection(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection(url,name,pass);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public Connection getConnection(String url,String name,String pass){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection(url,name,pass);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    //这个是查询所有
    public List<CommodityModel> selecCommodity() throws Exception{
        String sql="select * from commodity ";
        statement= getConnection().createStatement();
        resultSet=statement.executeQuery(sql);
        List<CommodityModel> list=new ArrayList<>();
        CommodityModel com=null;
        while (resultSet.next()){
            com=new CommodityModel();
            com.setCid(resultSet.getInt(1));
            com.setCname(resultSet.getString(2));
            com.setCcount(resultSet.getInt(3));
            com.setCtype(resultSet.getString(4));
            com.setCprice(resultSet.getDouble(5));
            com.setCurl(resultSet.getString(6));
            list.add(com);
        }
        connection.close();

        return list;

    }

    //这个是分页
    public List<CommodityModel> Commoditylimit(int page,int size) throws Exception{
        String sql="select * from commodity limit "+page+" , "+size;
        System.out.println(sql);
        statement= getConnection().createStatement();
        resultSet=statement.executeQuery(sql);
        ArrayList<CommodityModel> list = new ArrayList<>();

        while (resultSet.next()){
            CommodityModel com=new CommodityModel();
            com.setCid(resultSet.getInt(1));
            com.setCname(resultSet.getString(2));
            com.setCcount(resultSet.getInt(3));
            com.setCtype(resultSet.getString(4));
            com.setCprice(resultSet.getDouble(5));
            com.setCurl(resultSet.getString(6));
            list.add(com);
        }
        connection.close();
        for (CommodityModel commodityModel : list) {
            System.out.println(commodityModel.toString());
        }
        return list;

    }




    public void Addcommodity(CommodityModel commodityModel) throws Exception{
        String sql="insert into commodity (cname,ccount,ctype,cprice,curl) values ('"+commodityModel.getCname()+"','"+commodityModel.getCcount()+"'," +
                "'"+commodityModel.getCtype()+"','"+commodityModel.getCprice()+"','"+commodityModel.getCurl()+"')";
        System.out.println(sql);
        statement= getConnection().createStatement();
        statement.execute(sql);
        connection.close();

    }

    public void delete(int id)throws Exception{
        String sql="delete from commodity where cid="+id;
        System.out.println(sql);
        statement= getConnection().createStatement();
        statement.executeUpdate(sql);
        connection.close();


       /// resultSet=statement.executeQuery(sql);
    //    ArrayList<CommodityModel> list = new ArrayList<>();



    }

public CommodityModel byID(int id) throws Exception{
    String sql="select * from commodity where cid="+id;
    statement= getConnection().createStatement();
    resultSet=statement.executeQuery(sql);
    List<CommodityModel> list=new ArrayList<>();
    CommodityModel com=null;
    while (resultSet.next()){
        com=new CommodityModel();
        com.setCid(resultSet.getInt(1));
        com.setCname(resultSet.getString(2));
        com.setCcount(resultSet.getInt(3));
        com.setCtype(resultSet.getString(4));
        com.setCprice(resultSet.getDouble(5));
        com.setCurl(resultSet.getString(6));
        //list.add(com);
    }
    connection.close();

    return com;

}


    public void updataCom(CommodityModel commodityModel) throws Exception{
        String sql="update commodity set cname=?, ccount=?, ctype=?, cprice=?, curl=? where cid=?";
        PreparedStatement ppst = getConnection().prepareStatement(sql);
        ppst.setString(1,commodityModel.getCname());
        ppst.setInt(2,commodityModel.getCcount());
        ppst.setString(3,commodityModel.getCtype());
        ppst.setDouble(4,commodityModel.getCprice());
        ppst.setString(5,commodityModel.getCurl());
        ppst.setInt(6,commodityModel.getCid());
        ppst.executeUpdate();
        ppst.close();
        connection.close();
        System.out.println("修改成功!");
    }





}//这个是最后的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值