2022-5-6作业

2022-5-6作业,第1张

用JDBC完成注册功能,从0开始,从新搭建项目,熟悉下过程(切记切记)。提交注册成功的截图即可

答:

1.创建一个库,过程就不展示了,直接贴结果图

代码:08、综合案例-使用JDBC完成商城项目的CRUD_排骨玉米汤的博客-CSDN博客_jdbc商城 

2.建包

app 测试包 用于对DAO代码进行测试

dao dao包 数据访问层,包含所有对数据库的相关 *** 作的类

entity 实体包 保存根据数据库表 对应创建的JavaBean类

utils 工具包

建好上述的包 ,然后导入相关jar包和准备resource

 druid.properties

#配置驱动
driverClassName=com.mysql.jdbc.Driver
#配置URl 用户名 密码
url=jdbc:mysql://localhost:3306/zy?characterEncoding=UTF-8&rewriteBatchedStatements=true
username=root
password=heiheiehei

 DruidUtils.java

package com.qiku.zy.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/*
* 连接池工具类
* */
public class DruidUtils {
    //1.定义成员变量
    public static DataSource dataSource;//链接对象
    //2.使用静态代码块 配置信息
    static {
        try {
            //创建 properties 对象
            Properties p = new Properties();
            //加载配置文件
            InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            //使用load 方法将inputstream 加载到 p 中
            p.load(inputStream);
            //使用工厂类 获取连连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    获取连接的方法
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    //释放资源
    public static void close(Connection conn, Statement st){
        if (conn != null && st != null){
            try {
                st.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void close(Connection conn, Statement st ,ResultSet rs){
        if (conn != null && st != null && rs != null){
            try {
                rs.close();
                st.close();
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }




//    测试 properties
    @Test
    public void testProperties() throws IOException {
        //创建Properties对象
        Properties p = new Properties();
        //加载配置文件
//        InputStream inputStream = new FileInputStream("resource/druid.properties");
        InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");
        //使用load 方法将inputstream 加载到 p 中
        p.load(inputStream);
        //使用p 读取文件中的信息
        System.out.println(p.getProperty("driverClassName"));
        System.out.println(p.getProperty("url"));
        System.out.println(p.getProperty("username"));
        System.out.println(p.getProperty("password"));
    }

}

 User类

package com.qiku.zy.entity;

public class User {
    private String uid;
    private String username;
    private String password;
    private String telephone;
    private String birthday;
    private String sex;


    @Override
    public String toString() {
        return "User{" +
                "uid='" + uid + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", telephone='" + telephone + '\'' +
                ", birthday='" + birthday + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

Orders类
package com.qiku.zy.entity;

public class Orders {
    private String oid;
    private String ordertime;
    private double total;
    private String telephone;
    private String adress;
    private int state ;


    @Override
    public String toString() {
        return "Orders{" +
                "oid='" + oid + '\'' +
                ", ordertime='" + ordertime + '\'' +
                ", total=" + total +
                ", telephone='" + telephone + '\'' +
                ", adress='" + adress + '\'' +
                ", state=" + state +
                '}';
    }

    public String getOid() {
        return oid;
    }

    public void setOid(String oid) {
        this.oid = oid;
    }

    public String getOrdertime() {
        return ordertime;
    }

    public void setOrdertime(String ordertime) {
        this.ordertime = ordertime;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }
}

Category类

package com.qiku.zy.entity;

public class Category {
    private String cid;
    private String cname;


    @Override
    public String toString() {
        return "Category{" +
                "cid='" + cid + '\'' +
                ", cname='" + cname + '\'' +
                '}';
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }
}

Product类

package com.qiku.zy.entity;

public class Product {
    private String pid;
    private String pname;
    private double price;
    private String pdesc;
    private int pflag;

    private String cid;
    private Category category;


    @Override
    public String toString() {
        return "Product{" +
                "pid='" + pid + '\'' +
                ", pname='" + pname + '\'' +
                ", price=" + price +
                ", pdesc='" + pdesc + '\'' +
                ", pflag=" + pflag +
                ", cid='" + cid + '\'' +
                ", category=" + category +
                '}';
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getPdesc() {
        return pdesc;
    }

    public void setPdesc(String pdesc) {
        this.pdesc = pdesc;
    }

    public int getPflag() {
        return pflag;
    }

    public void setPflag(int pflag) {
        this.pflag = pflag;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }
}

OrdersItem

package com.qiku.zy.entity;

public class OrdersItem {
    private String itemid;
    private String pid;
    private Product product;
    private String oid;
    private Orders orders;


    @Override
    public String toString() {
        return "OrdersItem{" +
                "itemid='" + itemid + '\'' +
                ", pid='" + pid + '\'' +
                ", product=" + product +
                ", oid='" + oid + '\'' +
                ", orders=" + orders +
                '}';
    }

    public String getItemid() {
        return itemid;
    }

    public void setItemid(String itemid) {
        this.itemid = itemid;
    }

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }

    public String getOid() {
        return oid;
    }

    public void setOid(String oid) {
        this.oid = oid;
    }

    public Orders getOrders() {
        return orders;
    }

    public void setOrders(Orders orders) {
        this.orders = orders;
    }
}

UserDo

package com.qiku.zy.dao;

import com.qiku.zy.entity.User;
import com.qiku.zy.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;

import java.sql.SQLException;

public class UserDao {
    public int register(User user) throws SQLException {
        QueryRunner qr = new QueryRunner(DruidUtils.dataSource);
        String sql = "insert into user values(?,?,?,?,?,?)";
        int update = qr.update(sql, user.getUid(), user.getUsername(), user.getPassword(), user.getTelephone(), user.getBirthday(), user.getSex());
        return update;

    }
}

测试

package com.qiku.zy.app;

import com.qiku.zy.dao.UserDao;
import com.qiku.zy.entity.User;
import org.junit.Test;

import java.sql.SQLException;

public class TestUseDaoZY {
    private UserDao userDao = new UserDao();
    @Test
    public void tsetregister() throws SQLException {
        User user = new User();
        user.setUid("007");
        user.setUsername("kero");
        user.setPassword("123456");
        user.setBirthday("2222-2-22");
        user.setTelephone("987654321");
        user.setSex("女");
        int register = userDao.register(user);
        if (register != 0){
            System.out.println("注册成功,Welcome:" + user.getUsername());
        }else{
            System.out.println("注册失败!!!");
        }

    }
}

运行结果

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/871423.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存