java连接数据库实现基本的增删改查

java连接数据库

步骤

  • 1.加载数据库

    Class.forName("com.mysql.jdbc.Driver");
    
  • 2.链接数据库

    public static final String url="jdbc:mysql://localhost:3306/Product?userUnicode=true&characterEncoding=utf8&useSSL=false";
    		public static final String user ="root";
    		public static final String password="root";
    Connection conn = DriverManager.getConnection(url, user, password);
    
  • 3.数据库操纵对象

    String sql="SQL语句";	
    PreparedStatement pst=conn.prepareStatement(sql);
    //表示预编译的SQL语句的对象。 
    //SQL语句已预编译并存储在PreparedStatement对象中。 然后可以使用该对象多次有效地执行此语句
    
  • 4.执行SQL语句

    pst.execute();
    
  • 5.关闭数据库

    conn.close();
    pst.close();
    

基础的增删改查

思路: 先将基本的框架写出来 ,增删改查都在方法里面写

创建商品类

商品类 大概就是创建属性,get set 方法,构造有参无参方法,toString方法等几个

package com.po;
//重写equals和HashCode两个方法
public class Product {
	private int id;
	private String name;
	private double price;
    
    
    
	public Product(int id, String name, double price) {
		super();
		this.id = id;
		this.name = name;
		this.price = price;
	}
	public Product(String name, double price) {
		super();
		this.name = name;
		this.price = price;
	}
	private String info;
	private String param;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	public String getParam() {
		return param;
	}
	public void setParam(String param) {
		this.param = param;
	}
	public Product() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Product(int id, String name, double price, String info, St
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值