09_MySQL多表 & JDBC

2017-06-01  本文已影响0人  AndroidCat
MySQL多表
外键
-- 分类表
create table category(
    cid varchar(32) primary key,
    cname varchar(100)
);

-- 商品表
create table product(
    pid varchar(32) primary key,
    pname varchar(40),
    price double,
    category_id varchar(32)
);

-- 添加外键
alter table 从表 add [constraint] [外键名] foreign key (从表外键字段名) references 主表 (主表主键);
alter table product add foreign key(category_id) references category(cid);
alter table product add constraint product_fk foreign key(category_id) references category(cid);
表与表之间关系
创建表实现
--多对多
-- 订单表
CREATE TABLE orders(
    oid VARCHAR(32) PRIMARY KEY,
    totalprice DOUBLE
);

-- 订单项表
CREATE TABLE orderitem(
    oid VARCHAR(50),
    pid VARCHAR(50)
);

-- 商品表
create table product(
    pid varchar(32) primary key,
    pname varchar(40),
    price double,
    category_id varchar(32)
);

-- 联合主键(可省略)
alter table orderitem add primary key(oid,pid);

-- 订单表和订单项表的主外键关系
alter table orderitem add constraint orderitem_orders_fk foreign key(oid) references orders(oid);

-- 商品表和订单项表的主外键关系
alter table orderitem add constraint orderitem_product_fk foreign key(pid) references product(pid);
查询操作
-- 隐式内链接查询
SELECT * FROM category,product WHERE category_id=cid;
SELECT * FROM category AS c,product AS p WHERE c.cid = p.category_id;
-- 显示内链接查询
SELECT * FROM category INNER JOIN product ON cid = category_id;
SELECT * FROM category AS c INNER JOIN product AS p ON c.cid = p.category_id;
-- 左右外连接查询
SELECT * FROM category LEFT OUTER JOIN product ON cid = category_id;
SELECT * FROM category AS c LEFT OUTER JOIN product AS p ON c.cid = p.category_id;

SELECT * FROM category RIGHT OUTER JOIN product ON cid = category_id;
SELECT * FROM category AS c RIGHT OUTER JOIN product AS p ON c.cid = p.category_id;
子查询
JDBC
抽取工具类
public class JDBCUtils {
    private static Connection conn = null;
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/mybase";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("加载数据库驱动失败!");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException("连接数据库异常");
        }
    }
    private JDBCUtils(){}
    public static Connection getConnection() {
        return conn;
    }
}
使用properties配置文件
db.properties文件内容:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/dbname
user=root
password=root
通过ResourceBundle加载配置文件
ResourceBundle rb = ResourceBundle.getBundle("fileName");//不需要加后缀
String value = rb.getString("key");
通过Properties加载配置文件
//获取类加载器
static {
    InputStream is = currentClass.class.getClassLoader().getRescoreAsStream("fileName.propertyes");
    Properties prop = new Propertyes();
    prop.load(is);
    String value = prop.getProperties("key");
}
上一篇下一篇

猜你喜欢

热点阅读