栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

用java的类 来添加数据库的数据 添加自定义参数

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

用java的类 来添加数据库的数据 添加自定义参数

package com.sky.jdbc;

import Utils.uitls;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

//用类注册数据库
public class Demo1 {
    public static void main(String[] args) throws Exception {
        User u = new User("zhang","123456","zhangsan","119","shenf",50);

        int i = addUser(u);
        if (i==1) {
            System.out.println("注册成功");
            System.out.println(u.toString());
        }else {
            System.out.println("注册失败");
        }
    }
    public static  int  addUser(User user) throws Exception {
        Connection conn = uitls.getCollection() ;
        String sql = "insert into user values(null,?,?,?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1,user.getUsername());
        pstmt.setString(2,user.getPwd());
        pstmt.setString(3,user.getName());
        pstmt.setString(4,user.getPhone());
        pstmt.setString(5,user.getCard());
        pstmt.setInt(6,user.getMoney());
        int i = pstmt.executeUpdate();
        uitls.close(pstmt,conn);

        return i;
    }
}

用户类
package com.sky.jdbc;

public class User {
    private String username;
    private String  pwd;
    private String name;
    private String phone;
    private String  card;
    private int money;

    public User() {
    }

    public User(String username, String pwd, String name, String phone, String card, int money) {
        this.username = username;
        this.pwd = pwd;
        this.name = name;
        this.phone = phone;
        this.card = card;
        this.money = money;
    }

    public String getUsername() {
        return username;
    }

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

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCard() {
        return card;
    }

    public void setCard(String card) {
        this.card = card;
    }

    public int getMoney() {
        return money;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + ''' +
                ", pwd='" + pwd + ''' +
                ", name='" + name + ''' +
                ", phone='" + phone + ''' +
                ", card='" + card + ''' +
                ", money=" + money +
                '}';
    }
}



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/855338.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号