千鋒逆戰班,mysql小練習及jdbc的應用

mysql代碼

# 創建用戶表
CREATE TABLE `user`(
	userid INT PRIMARY KEY AUTO_INCREMENT,
	username VARCHAR(20) NOT NULL,
	`password` VARCHAR(18) NOT NULL,
	address VARCHAR(100),
	phone VARCHAR(11)

)CHARSET=utf8;

#創建分類表

CREATE TABLE category(
	cid VARCHAR(32) PRIMARY KEY,
	cname VARCHAR(100) NOT NULL

)CHARSET=utf8;


# 商品表
CREATE TABLE products(
	pid VARCHAR(32) PRIMARY KEY,
	`name` VARCHAR(40),
	price DOUBLE(7,2),
	category_id VARCHAR(32),
	CONSTRAINT FOREIGN KEY(category_id) REFERENCES category(cid)

)CHARSET=utf8;

#訂單表

CREATE TABLE `order`(
	oid VARCHAR(32) PRIMARY KEY,
	totalprice DOUBLE(12,2),
	userid INT,
	CONSTRAINT FOREIGN KEY(userid) REFERENCES `user`(userid)

)CHARSET=utf8;

# 訂單項表
CREATE TABLE orderitem(
	oid VARCHAR(32),
	pid VARCHAR(32),
	num INT,
	PRIMARY KEY(oid,pid),
	FOREIGN KEY(oid) REFERENCES `order`(oid),
	FOREIGN KEY(pid) REFERENCES products(pid)


)CHARSET=utf8;

#-----------------------------------------------
#初始化數據

#用戶表添加數據
INSERT INTO USER(username,PASSWORD,address,phone) VALUES('張三','123','北京昌平沙河','13812345678');
INSERT INTO USER(username,PASSWORD,address,phone) VALUES('王五','5678','北京海淀','13812345141');
INSERT INTO USER(username,PASSWORD,address,phone) VALUES('趙六','123','北京朝陽','13812340987');
INSERT INTO USER(username,PASSWORD,address,phone) VALUES('田七','123','北京大興','13812345687');

#給商品表初始化數據
INSERT INTO products(pid,`name`,price,category_id) VALUES('p001','聯想',5000,'c001');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p002','海爾',3000,'c001');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p003','雷神',5000,'c001');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p004','JACK JONES',800,'c002');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p005','真維斯',200,'c002');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p006','花花公子',440,'c002');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p007','勁霸',2000,'c002');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p008','香奈兒',800,'c003');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p009','相宜本草',200,'c003');
INSERT INTO products(pid,`name`,price,category_id) VALUES('p010','梅明子',200,NULL);


#給分類表初始化數據
INSERT INTO category VALUES('c001','電器');
INSERT INTO category VALUES('c002','服飾');
INSERT INTO category VALUES('c003','化妝品');
INSERT INTO category VALUES('c004','書籍');

#添加訂單
INSERT INTO `order` VALUES('o6100',18000.50,1);
INSERT INTO `order` VALUES('o6101',7200.35,1);
INSERT INTO `order` VALUES('o6102',600.00,2);
INSERT INTO `order` VALUES('o6103',1300.26,4);

#訂單詳情表
INSERT INTO orderitem VALUES('o6100','p001',1),('o6100','p002',1),('o6101','p003',1);

#1>查詢所有用戶的訂單
SELECT * FROM `user` AS u INNER JOIN `order` AS o
ON u.`userid` = o.`userid` ;

#2>查詢用戶id爲 1 的所有訂單詳情
SELECT * FROM `user` AS u INNER JOIN `order` AS o
ON u.`userid`= o.`userid` INNER JOIN orderitem AS ad
ON o.`oid`=ad.`oid`

#15.2 綜合練習2-【子查詢】

#1>查看用戶爲張三的訂單
SELECT * FROM (SELECT * FROM `user` WHERE username='張三') AS u INNER JOIN `order` AS o
ON u.`userid` = o.`userid`;

#2>查詢出訂單的價格大於800的所有用戶信息。
#方法1
SELECT * FROM (SELECT * FROM `order` AS o WHERE totalprice > 800) AS tt INNER JOIN `user` AS u
ON u.`userid` = tt.`userid`;
#方法2
SELECT * FROM `user` AS u INNER JOIN (SELECT * FROM `order` AS o WHERE totalprice > 800) AS o
ON u.`userid` = o.`userid`;

#15.3 綜合練習3-【分頁查詢】

#1>查詢所有訂單信息,每頁顯示5條數據
SELECT * FROM `order` LIMIT 0,5;




jdbc 添加數據的代碼,沒有添加事務

package com.qf.day1.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class TestjdbcDml {
    public static void main(String[] args) throws  Exception{
        Class.forName("com.mysql.jdbc.Driver");

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/compar?useUnicode=true&characterEncoding=utf8","root","740056981");
        //3.獲取發送SQL語句的對象   Statement
        Statement sta = conn.createStatement();
        //增加,修改,刪除用戶
        String usql1 = "insert into user(username,password,address,phone) values('阿耀','123','山東省濟南市','123456')";
        String usql2 = "update user set username = '阿軍',passwprd = '456' where userid = 1";
        String usql3 = "delete from user where userid = 5";
        //增加,修改,刪除商品分類
        String csql1 = "insert into category(cid,cname) values('c005','牀上用品')";
        String csql2 = "update category set cname = '辦公用品' where cid='c005'";
        String csql3 = "delete from catgory where cid = 'c005'";
        //增加,修改,刪除商品
        String psql1 = "insert into products(pid,`name`,price,category_id) values('p011','黑豆',5555.55,'c004')";
        String psql2 = "update products set name = '大米' where pid = 'p011'";
        String psql3 = "delete from producats where pid = 'p011'";
        //增加,修改,刪除訂單
        String osql1 = "insert into `order`(oid,totalprice,userid) values('o6104',18000,2)";
        String osql2 = "update order set userid = 3 where oid = 'o6104'";
        String osql3 = "delete from order where oid = 'o6104'";
        //增加,修改,刪除訂單詳情
        String oisql1 = "insert into orderitem(oid,pid,num) values('o6104','p011',2)";
        String oisql2 = "update orderitem set num = 3 where oid = 'o6104'";
        String oisql3 = "delete from orderitem where oid = 'o6104'and pid='p011'";

        int user1 = sta.executeUpdate(usql1);//增加一個用戶
        if (user1 > 0){
            System.out.println("用戶新增成功");
        }else {
            System.out.println("用戶新增失敗");
        }
        int catgroy1 = sta.executeUpdate(csql1);//增加一個商品分類
        if ( catgroy1 > 0){
            System.out.println("商品分類新增成功");
        }else {
            System.out.println("商品分類新增失敗");
        }
        int products1 = sta.executeUpdate(psql1);//增加一個商品信息
        if (products1 > 0){
            System.out.println("商品新增成功");
        }else {
            System.out.println("商品新增失敗");
        }
        int order1 = sta.executeUpdate(osql1);//增加一個訂單信息
        if ( order1 > 0 ){
            System.out.println("訂單新增成功");
        }else {
            System.out.println("訂單新增失敗");
        }
        int orderitem1 = sta.executeUpdate(oisql1);//增加一個訂單詳情信息

        if (orderitem1 > 0){
            System.out.println("訂單詳情新增成功");
        }else {
            System.out.println("訂單詳情新增失敗");
        }

        sta.close();
        conn.close();
    }
}

jdbc 修改數據代碼,有事務的

package com.qf.day1.jdbc;



import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class TestUpdate {
    public static void main(String[] args) throws  Exception{
        Class.forName("com.mysql.jdbc.Driver");//1.啓動驅動
        //2.設置數據庫連接,用戶名,密碼 鏈接數據庫
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/compar?useUnicode=true&characterEncoding=utf8","root","740056981");
        //3.獲取發送SQL語句的對象   Statement
        Statement sta = conn.createStatement();
        //增加,修改,刪除用戶
        String usql1 = "insert into user(username,password,address,phone) values('阿耀','123','山東省濟南市','123456')";
        String usql2 = "update user set username = '阿軍',password = '456' where userid = 9";
        String usql3 = "delete from user where userid = 5";
        //增加,修改,刪除商品分類
        String csql1 = "insert into category(cid,cname) values('c005','牀上用品')";
        String csql2 = "update category set cname = '辦公用品' where cid='c005'";
        String csql3 = "delete from catgory where cid = 'c005'";
        //增加,修改,刪除商品
        String psql1 = "insert into products(pid,`name`,price,category_id) values('p011','黑豆',5555.55,'c004')";
        String psql2 = "update products set `name` = '大米' where pid = 'p011'";
        String psql3 = "delete from producats where pid = 'p011'";
        //增加,修改,刪除訂單
        String osql1 = "insert into `order`(oid,totalprice,userid) values('o6104',18000,2)";
        String osql2 = "update `order` set userid = 3 where oid = 'o6104'";
        String osql3 = "delete from order where oid = 'o6104'";
        //增加,修改,刪除訂單詳情
        String oisql1 = "insert into orderitem(oid,pid,num) values('o6104','p011',2)";
        String oisql2 = "update orderitem set num = 3 where oid = 'o6104'";
        String oisql3 = "delete from orderitem where oid = 'o6104'and pid='p011'";
        try {
            conn.setAutoCommit(false);//關閉自動提交
            int user1 = sta.executeUpdate(usql2);//修改一個用戶
            int catgroy1 = sta.executeUpdate(csql2);//修改一個商品分類
            int products1 = sta.executeUpdate(psql2);//修改一個商品信息
            int order1 = sta.executeUpdate(osql2);//修改一個訂單信息
            int orderitem1 = sta.executeUpdate(oisql2);//修改一個訂單詳情信息

            //上述語句執行無異常我們,提交
            conn.commit();//手動提交

            System.out.println("修改成功");
        } catch(Exception e){
            System.out.println("修改失敗,回滾了");
            conn.rollback();//回滾
            e.printStackTrace();
        }finally {
            //釋放資源
            sta.close();
            conn.close();
        }


    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章