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