文章目錄
- 軟體環境
- 個人博客(記錄了從零開始學習java的程序)
- 網址:https://hs-vae.com
- 一、準備作業
- 1.在DataGrip中建立一個ms_memer表
- 2.新建 jdbc.properties 組態檔
- 3.撰寫 Config 類:加載組態檔
- 4.撰寫 DBUtils工具類
- 二、批量插入100條資料
- 1.實作代碼
- 2.插入100條資料后的ms_memer表
- 三、查詢九月份登錄過系統的會員名稱和登錄時間
- 1.實作代碼
- 2.查詢結果
- 四、洗掉9月1日以前注冊的會員資訊
- 1.洗掉前的ms_memer表
- 2.實作代碼
- 3.洗掉后的ms_memer表
軟體環境
java編譯軟體:IDEA
資料庫:DataGrip
jdbc驅動(8.0.22版本):https://dev.mysql.com/downloads/connector/j/
個人博客(記錄了從零開始學習java的程序)
網址:https://hs-vae.com
一、準備作業
1.在DataGrip中建立一個ms_memer表
會員資訊表結構設計(ms_memer)
| 鍵 | 欄位描述 | 欄位名 | 資料型別 | 欄位長度 | 是否可空 | 備注 |
|---|---|---|---|---|---|---|
| P | 會員標識 | member_id | int | 11 | NOT NULL | 自動加1 |
| 會員名稱 | Uname | varchar | 50 | NOT NULL | ||
| 密碼 | Password | varchar | 50 | NOT NULL | ||
| 郵箱 | varchar | 50 | NOT NULL | |||
| 性別 | Sex | Smallint | 6 | NULL | ||
| 手機號碼 | Mobile | varchar | 30 | NULL | ||
| 注冊時間 | Regtime | timestamp | NOT NULL | |||
| 最后一次登錄時間 | Lastlogin | timestamp | NOT NULL | |||
| 頭像 | Image | Varchar | 500 | NULL |

2.新建 jdbc.properties 組態檔
在src目錄下創建一個jdbc.properties檔案
里面包含(根據個人資料庫用戶和密碼不同進行修改,我的jdbc驅動是8.0.22版本,所以driver這樣寫):
url=jdbc:mysql:///mobile_shop
user=root
password=123456
driver=com.mysql.cj.jdbc.Driver
3.撰寫 Config 類:加載組態檔
package Advanced.JDBC.Util;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
//配置類Config
public class Config {
private static Properties p=null;
static {
try {
//加載組態檔
p=new Properties();
p.load(new FileInputStream("src/jdbc.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
// 獲取鍵對應的值
public static String getValue(String key){
return p.get(key).toString();
}
}
4.撰寫 DBUtils工具類
- 包含獲取連接的方法getConnection()
- 包含釋放資源的方法closeAll()
- 包含執行SQL陳述句中查詢的executeQuery(String preparedSql , String[] param)方法
- 包含執行SQL陳述句中"增、刪、改"的executeUpdate(String preparedSql , String[] param)方法
package Advanced.JDBC.Util;
import java.sql.*;
public class DBUtils {
Connection conn=null;
ResultSet rs=null;
PreparedStatement ptmt=null;
// 2.獲取連接
public Connection getConnection() throws SQLException {
String url= Config.getValue("url");
String user=Config.getValue("user");
String password=Config.getValue("password");
String driver=Config.getValue("driver");
try {
Class.forName(driver);
conn=DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
// 3.釋放資源
public void closeAll(){
if(rs!=null){
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(ptmt!=null){
try {
ptmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
// 4.執行SQL陳述句,可以進行查詢
public ResultSet executeQuery(String preparedSql , String[] param){
try {
// 創建執行sql的PrepareStatement物件
ptmt=conn.prepareStatement(preparedSql);
if(param!=null){
for (int i = 0; i < param.length; i++) {
//為預編譯的sql設定引數,這里注意起點的編號是1,和陣列索引不一樣,所以要加1
ptmt.setString(i+1,param[i]);
}
}
// 獲取執行sql物件,執行sql陳述句
rs=ptmt.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return rs;
}
// 5.執行SQL陳述句,可以進行增,刪,改的操作
public int executeUpdate(String preparedSql , String[] param){
int count=0;
try {
// 創建執行sql的PrepareStatement物件
ptmt=conn.prepareStatement(preparedSql);
if(param!=null){
for (int i = 0; i < param.length; i++) {
//為預編譯的sql設定引數,這里注意起點的編號是1,和陣列索引不一樣,所以要加1
ptmt.setString(i+1,param[i]);
}
}
count=ptmt.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return count;
}
}
二、批量插入100條資料
1.實作代碼
package Experiment.Demo2;
import Advanced.JDBC.Util.DBUtils;
import java.sql.SQLException;
public class Demo1A {
public static void main(String[] args) {
DBUtils db=new DBUtils();
//設定9月份的注冊時間s1
String s1="2020-09-12 16:29:58";
//設定9月1日以前的注冊時間s2
String s2="2020-08-06 15:32:31";
int sum=0;
try {
//1.連接驅動
db.getConnection();
//2.定義sql陳述句
String sql="insert into ms_memer(member_id,Uname,Password,Email,Sex,Mobile,Regtime,Lastlogin,Image) values (?,?,?,?,?,?,?,?,?)";
//3.使用回圈對表插入資料,呼叫工具類中的executeUpdate方法
for (int i = 0; i < 100; i++) {
if(i<49){
//插入前50條9月份最后一次登錄的資料
db.executeUpdate(sql,new String[]{null,"hs","123456","hs@qq.com","1","18872748895",s1,s1,"hs-vae"});
}else {
//插入后50條9月1日之前最后一次登錄的資料
db.executeUpdate(sql,new String[]{null,"vae","123456","hs@qq.com","0","18872748895",s2,s2,"hs-vae"});
}
++sum;
}
System.out.println("已成功插入:"+sum+"條資料");
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
db.closeAll();
}
}
}
//輸出結果
已成功插入:100條資料
Process finished with exit code 0
2.插入100條資料后的ms_memer表

插入100條資料成功,分兩批插入的,可以查看代碼詳解
三、查詢九月份登錄過系統的會員名稱和登錄時間
1.實作代碼
package Experiment.Demo2;
import Advanced.JDBC.Util.DBUtils;
import java.sql.ResultSet;
import java.sql.SQLException;
/*
要求:撰寫存盤程序查詢出九月份有登陸過本系統會員資訊要求查詢出具體的會員名稱和登陸時間
*/
public class Demo3C {
public static void main(String[] args) {
DBUtils db=new DBUtils();
try {
db.getConnection();
String sql="select * from ms_memer where timestamp(Lastlogin) between ? and ?";
ResultSet rs=db.executeQuery(sql,new String[]{"2020-08-31 23:59:59","2020-09-30 23:59:59"});
System.out.println("---------9月份登錄過本系統的會員資訊-----------");
System.out.println(" "+"會員名稱"+" "+"注冊時間"+" "+"最后一次登錄時間");
while (rs.next()){
System.out.println(rs.getRow()+":"+"\t"
+rs.getString(2)+"\t"
+rs.getString(7)+"\t"
+rs.getString(8));
}
System.out.println("--------------------------------");
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
db.closeAll();
}
}
}
2.查詢結果
Experiment.Demo2.Demo3C
---------9月份登錄過本系統的會員資訊-----------
會員名稱 注冊時間 最后一次登錄時間
1: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
2: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
3: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
4: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
5: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
6: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
7: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
8: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
9: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
10: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
11: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
12: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
13: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
14: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
15: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
16: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
17: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
18: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
19: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
20: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
21: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
22: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
23: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
24: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
25: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
26: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
27: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
28: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
29: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
30: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
31: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
32: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
33: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
34: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
35: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
36: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
37: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
38: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
39: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
40: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
41: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
42: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
43: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
44: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
45: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
46: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
47: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
48: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
49: hs 2020-09-12 16:29:58 2020-09-12 16:29:58
--------------------------------
Process finished with exit code 0
四、洗掉9月1日以前注冊的會員資訊
1.洗掉前的ms_memer表

2.實作代碼
package Experiment.Demo2;
import Advanced.JDBC.Util.DBUtils;
import java.sql.SQLException;
public class Demo2B {
public static void main(String[] args) {
DBUtils db=new DBUtils();
try {
//獲取連接
db.getConnection();
//定義sql陳述句,要求洗掉9月1日之前注冊的會員資訊
String sql="delete from ms_memer where Regtime<? ";
db.executeUpdate(sql,new String[]{"2020-08-31 23:59:59"});
System.out.println("已成功洗掉9月1日前注冊的會員資訊");
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
db.closeAll();
}
}
}
//輸出結果
已成功洗掉9月1日前注冊的會員資訊
Process finished with exit code 0
3.洗掉后的ms_memer表

洗掉成功!留下來的都是9月以后注冊的會員資訊
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/208449.html
標籤:其他
上一篇:資料庫四種事物的基本性質
