• 【使用JDBC对数据库表进行操作】


    使用JDBC对数据库表进行操作(创建表、插入数据、更新数据、删除数据)

    编程题
    1、写一个JDBC类,包括3个方法,分别实现对用户表的如下操作。
    在这里插入图片描述
    JDBC应用程序大概有以下六个步骤
    1、创建连接对象
    2、创建执行SQL语句的命令对象
    3、执行sql语句
    4、获取结果:
    如果是查询获取结果集如果是更新 获取 影响的行数
    5、如果是查询 需要从结果集中获取数据
    6、关闭所有资源 先创建的对象后关闭

    DBUtil.java

    package util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class DBUtil {
    	static {
    		try {
    			Class.forName("com.mysql.jdbc.Driver");
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    	 
    	//   获取连接
    	public static Connection getConnection() {
    		Connection conn=null;
    		String url="jdbc:mysql://localhost:3306/java01?useUnicode=true&characterEncoding=utf-8";
    		String username="****";
    		String password="****";
    		try {
    			conn=DriverManager.getConnection(url,username,password);
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		
    		return conn;
    		
    	}
    	
    	
    	//  释放资源
    	public static void close(Connection conn,Statement st,ResultSet rs) {
    		try {
    			if(rs!=null) {
    				rs.close();
    			}
    			if(st!=null) {
    				st.close();
    			}
    			if(conn!=null) {
    				conn.close();
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    		
    	}
    	
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    方法接口

    public interface User1dao {
    	public int doAdd(User1 u);
    	public int doUpdate(User1 u);
    	public int doDelete(User1 u);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    建立一个实体类User

    package AA1;
    
    import java.sql.Date;
    
    public class User1 {
    	private String name;
    	private String pwd;
    	private String email;
    	private Date birthday;
    	
    	public User1() {
    		super();
    	}
    	
    	public User1(String name, String pwd, String email, Date birthday) {
    		super();
    		this.name = name;
    		this.pwd = pwd;
    		this.email = email;
    		this.birthday = birthday;
    	}
    	
    	@Override
    	public String toString() {
    		return "User1 [name=" + name + ", pwd=" + pwd + ", email=" + email + ", birthday=" + birthday + "]";
    	}
    
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getPwd() {
    		return pwd;
    	}
    	public void setPwd(String pwd) {
    		this.pwd = pwd;
    	}
    	public String getEmail() {
    		return email;
    	}
    	public void setEmail(String email) {
    		this.email = email;
    	}
    	public Date getBirthday() {
    		return birthday;
    	}
    	public void setBirthday(Date birthday) {
    		this.birthday = birthday;
    	}
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    方法实现类

    package AA1;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.sql.PreparedStatement;
    
    import util.DBUtil;
    
    public class Test1_table implements User1dao {
    	
    	//创建表
    	public static void doCreatetable(){
    		Connection conn=null;
    		Statement st=null;
    		ResultSet rs=null;
    		
    		try {
    			conn=DBUtil.getConnection();
    			String sql="create table User1(name varchar(20) primary key,pwd varchar(6) not null,email varchar(64),birthday date)";
    			st=conn.createStatement();
    			int i=st.executeUpdate(sql);    //执行更新语句
    			System.out.println("CreateTable ok,i="+i);
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally {
    			DBUtil.close(conn, st, rs);
    		}
    		
    	}
    	
    	//插入数据
    	public int doAdd(User1 u) {
    		Connection conn=null;
    		PreparedStatement st=null;
    		ResultSet rs=null;
    		int i=0;
    		
    		try {
    			conn=DBUtil.getConnection();
    			String sql="insert into User1(name,pwd,email,birthday) values(?,?,?,?)";
    			st=conn.prepareStatement(sql);
    			st.setString(1, u.getName());
    			st.setString(2, u.getPwd());
    			st.setString(3, u.getEmail());
    			st.setDate(4, u.getBirthday());
    			i=st.executeUpdate();    //执行更新语句
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally {
    			DBUtil.close(conn, st, rs);
    		}
    		return i;
    	}
    	
    //	更新数据
    	public int doUpdate(User1 u) {
    		Connection conn=null;
    		Statement st=null;
    		ResultSet rs=null;
    		int i=0;
    		try {
    			conn=DBUtil.getConnection();
    			String sql="UPDATE user1 SET birthday=CURRENT_TIMESTAMP WHERE name='张三'";
    			st=conn.createStatement();
    			i=st.executeUpdate(sql);    //执行更新语句
    			System.out.println("update ok,i="+i);
    			
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally {
    			DBUtil.close(conn, st, rs);
    		}
    		return i;
    	}
    	
    //	删除数据
    	public int doDelete(User1 u) {
    		Connection conn=null;
    		Statement st=null;
    		ResultSet rs=null;
    		int i=0;
    		try {
    			conn=DBUtil.getConnection();
    			String sql="delete from User1 where name='李四'";
    			st=conn.createStatement();
    			i=st.executeUpdate(sql);    //执行更新语句
    			System.out.println("delete ok,i="+i);
    			
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally {
    			DBUtil.close(conn, st, rs);
    		}
    		return i;
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    在写生日更新为当前时间一开始想的有点复杂,之后查了一下MySQL语句更新当前时间的方法,就使用了current_timestamp来获取当前时间。
    timestamp 时间戳
    时间戳字段定义主要影响两类操作:

    插入记录时,时间戳字段包含DEFAULT CURRENT_TIMESTAMP,如插入记录时未指定具体时间数据则将该时间戳字段值设置为当前时间
    更新记录时,时间戳字段包含ON UPDATE CURRENT_TIMESTAMP,如更新记录时未指定具体时间数据则将该时间戳字段值设置为当前时间

    测试类

    package AA1;
    
    import java.sql.Date;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    
    import dao.UserDao;
    import dao.UserDaoImpl;
    
    public class Test01a {
    	private static User1dao dao = new Test1_table();
    
    	public static void main(String[] args) {
    //		inserttest();
    //		updatetest();
    		deletetest();	
    	}
    
    	public static void inserttest() {
    		SimpleDateFormat sdf = new SimpleDateFormat("YY-MM-DD");
    		java.util.Date date;
    		int i=0;
    		try {
    			date = sdf.parse("1986-10-11");
    			User1 u1 = new User1("张三", "888888", "zhangsan@126.com", new Date(date.getTime()));
    			i = dao.doAdd(u1);
    			date = sdf.parse("1988-10-23");
    			User1 u2 = new User1("李四", "999999", "lisi@126.com", new Date(date.getTime()));
    			i = dao.doAdd(u2);
    			date = sdf.parse("1990-9-11");
    			User1 u3= new User1("王五", "777777", "wangwu@126.com", new Date(date.getTime()));
    			i = dao.doAdd(u3);
    
    		} catch (ParseException e) {
    			e.printStackTrace();
    		}
    
    		System.out.println("insert ok" + i);
    
    	}
    
    	public static void updatetest() {
    		int i=0;
    		User1 up=new User1();
    		i=dao.doUpdate(up);
    
    	}
    
    	public static void deletetest() {
    		int i=0;
    		User1 ud=new User1();
    		i=dao.doDelete(ud);
    		
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
  • 相关阅读:
    基于 Socket 的网络编程
    【水果派不吃灰】半小时搭建Raspberry Pi树莓派可运行环境(不需要显示器,不需要网线)
    关于使用RT-Thread系统读取stm32的adc无法连续转换的问题解决
    Taobao Proxy Purchasing System
    【知识专栏丨python数分实战】电商数据分析案例
    FastInst: A Simple Query-Based Model for Real-Time Instance Segmentation 代码解析
    《第一行代码》核心知识点:Activity(活动)
    y79.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(十)
    [Mac] 安装paddle-pipelines出现 ERROR: Failed building wheel for lmdb
    用哈希简单封装unordered_map和unordered_set
  • 原文地址:https://blog.csdn.net/qq_45891676/article/details/126146346