java编译软件:IDEA
数据库:DataGrip
jdbc驱动(8.0.22版本):https://dev.mysql.com/downloads/connector/j/
会员信息表结构设计(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 |
在src目录下创建一个jdbc.properties文件
里面包含(根据个人数据库用户和密码不同进行修改,我的jdbc驱动是8.0.22版本,所以driver这样写):
url=jdbc:mysql:///mobile_shop
user=root
password=123456
driver=com.mysql.cj.jdbc.Driver
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();
}
}
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;
}
}
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","[email protected]","1","18872748895",s1,s1,"hs-vae"});
}else {
//插入后50条9月1日之前最后一次登录的数据
db.executeUpdate(sql,new String[]{null,"vae","123456","[email protected]","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
插入100条数据成功,分两批插入的,可以查看代码详解
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();
}
}
}
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
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
删除成功!留下来的都是9月以后注册的会员信息