一、數(shù)據(jù)庫表結(jié)構(gòu):
首先,有兩個表:
分類表:category
create table category(
cid varchar(32) primary key,
cname varchar(100)
);
商品表:product
create table product(
pid varchar(32) primary key,
pname varchar(40),
price double,
category_id varchar(32)
);
其中:category為主表,"cid"為主鍵;product為從表,category_id為外鍵。
外鍵的特點(diǎn):
- 從表外鍵的值是對主表主鍵的引用;
- 從表外鍵類型,必須與主表外鍵類型一致。
使用外鍵的目的:保證數(shù)據(jù)完整性。
1.表與表之間的關(guān)系:
- 一對多關(guān)系:從表(多方)創(chuàng)建一個字段,作為外鍵指向主表(一方)的主鍵;
- 多對多關(guān)系:創(chuàng)建第三張表,中間表至少兩個字段,這兩個字段分別作為外鍵指向各自的主鍵;
- 一對一:外鍵唯一、外鍵是主鍵、笛卡爾積。
2.創(chuàng)建表實(shí)現(xiàn):
- 添加外鍵字段:
alter table product add category_id varchar(32);
-
添加約束:
alter table product add foreign key(category_id) references category(cid);
注意:
- 從表不能添加主表不存在的記錄;
- 主表不能刪除從表已引用的記錄。
多對多關(guān)系的實(shí)現(xiàn):
首先,有三個表:
- 商品表:product
create table product(
pid varchar(32) primary key,
pname varchar(40),
price double,
category_id varchar(32)
);
- 訂單表: orders
create table orders(
oid varchar(32) primary key,
totalprice double
);
- 訂單項(xiàng)表:orderitem
此表為建立商品表和訂單表的關(guān)系而建立
create table orderitem(
oid varchar(50),
pid varchar(50)
);
添加訂單表和訂單項(xiàng)表的主外鍵關(guān)系:
alter table orderitem add constraint oreritem_orders_fk foreign key(oid) refereneces orders(oid);
添加商品表和訂單項(xiàng)表的主外鍵關(guān)系:
alter table product add constraint orderitem_product_fk foreign key(pid) references orders(pid);
二、查詢:
兩個表:


1.交叉連接:
//基本不會使用,得到的是兩個表的笛卡爾乘積
select * from A,B;
2.內(nèi)連接:
- 隱式內(nèi)連接:
select * from A,B where 條件;

- 顯式內(nèi)連接:
//inner可以省略
select * from A inner join B on 條件;

3.外鏈接:
- 左外連接:
//out可以省略
select * from A left out join B on 條件;

- 右外連接:
select * from A right out join B on 條件;
3.子查詢:
例:查詢“化妝品”分類上架商品詳情:
select * from product where category_id=(select cid from category where name=‘化妝品’);
JDBC相關(guān)操作:
JDBCUtils_V3.java:
package com.zl.jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;
/**
* 提供獲取連接和釋放資源的 方法
*
*/
public class JDBCUtils_V3 {
private static String driver;
private static String url;
private static String username;
private static String password;
/**
* 靜態(tài)代碼塊加載配置文件信息
*/
static {
try {
// 1.通過當(dāng)前類獲取類加載器
ClassLoader classLoader = JDBCUtils_V3.class.getClassLoader();
// 2.通過類加載器的方法獲得一個輸入流
InputStream is = classLoader.getResourceAsStream("db.properties");
// 3.創(chuàng)建一個properties對象
Properties props = new Properties();
// 4.加載輸入流
props.load(is);
// 5.獲取相關(guān)參數(shù)的值
driver = props.getProperty("driver");
url = props.getProperty("url");
username = props.getProperty("username");
password = props.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 獲取連接方法
*
* @return
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
TestUtils.java
package com.zl.jdbc.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.zl.jdbc.JDBCUtils_V1;
import com.zl.jdbc.JDBCUtils_V2;
import com.zl.jdbc.JDBCUtils_V3;
/**
* 測試工具類
*
*/
public class TestUtils {
/**
* 根據(jù)id更新用戶信息方法
*/
@Test
public void testUpdateById() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1.獲取連接
conn = JDBCUtils_V3.getConnection();
// 2.編寫sql語句
String sql = "update tbl_user set upassword=? where uid=?";
// 3.獲取執(zhí)行sql語句對象
pstmt = conn.prepareStatement(sql);
// 4.設(shè)置參數(shù)
pstmt.setString(1, "999");
pstmt.setInt(2, 3);
// 5.執(zhí)行更新操作
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("更新成功!");
} else {
System.out.println("更新失敗!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6.釋放資源
JDBCUtils_V3.release(conn, pstmt, null);
}
}
/**
* 根據(jù)id刪除信息方法
*/
@Test
public void testDeleteById() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1.獲取連接
conn = JDBCUtils_V3.getConnection();
// 2.編寫sql語句
String sql = "delete from tbl_user where uid=?";
// 3.獲取執(zhí)行sql語句對象
pstmt = conn.prepareStatement(sql);
// 4.設(shè)置參數(shù)
pstmt.setInt(1, 4);
// 5.執(zhí)行刪除操作
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("刪除成功!");
} else {
System.out.println("刪除失敗!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6.釋放資源
JDBCUtils_V3.release(conn, pstmt, null);
}
}
/**
* 添加用戶信息方法
*/
@Test
public void testAdd() {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1.獲取連接
conn = JDBCUtils_V2.getConnection();
// 2.編寫sql語句
String sql = "insert into tbl_user values(null,?,?)";
// 3.獲取執(zhí)行sql語句對象
pstmt = conn.prepareStatement(sql);
// 4.設(shè)置參數(shù)
pstmt.setString(1, "lisi");
pstmt.setString(2, "hehe");
// 5.執(zhí)行插入操作
int row = pstmt.executeUpdate();
if (row > 0) {
System.out.println("添加成功!");
} else {
System.out.println("添加失敗!");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 6.釋放資源
JDBCUtils_V2.release(conn, pstmt, null);
}
}
/**
* 根據(jù)id查詢用戶信息
*/
@Test
public void testFindUserById() {
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 1.獲取連接
conn = JDBCUtils_V1.getConnection();
// 2.編寫sql語句
String sql = "select * from tbl_user where uid=?";
// 3.獲取執(zhí)行sql語句對象
pstmt = conn.prepareStatement(sql);
// 4.設(shè)置參數(shù)
pstmt.setInt(1, 2);
// 5.執(zhí)行查詢操作
rs = pstmt.executeQuery();
// 6.處理結(jié)果集
while (rs.next()) {
System.out.println(rs.getString(2) + "----" + rs.getString("upassword"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 7.釋放資源
JDBCUtils_V1.release(conn, pstmt, rs);
}
}
}
