• 【JDBC】CRUD操作


    JDBC的CRUD操作

    • 向数据库中保存记录
    • 修改数据库中的记录
    • 删除数据库中的记录
    • 查询数据库中的记录

    保存代码的实现

    package demo1;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.junit.Test;
    
    public class JDBCDemo3 {
    	
    	@Test
    	/**
    	 * 保存代码的实现
    	 */
    	public void demo3() {
    		Connection conn = null;
    		Statement stmt = null;
    		
    		try {
    			//注册驱动
    			Class.forName("com.mysql.cj.jdbc.Driver");
    			//获得连接
    			conn = DriverManager.getConnection("jdbc:mysql:///jdbctest?serverTimezone=Hongkong", "root", "1234");
    			//获得执行SQL语句的对象
    			stmt = conn.createStatement();
    			//编写SQL
    			String sql = "insert user values(null,'ddd','444','赵六')";
    			//执行SQL
    			int i = stmt.executeUpdate(sql);
    			if(i > 0) {
    				System.out.println("保存成功!");
    			}
    		}catch(Exception e) {
    			e.printStackTrace();
    		}finally{
    			//释放资源
    			if(stmt != null) {
    				try {
    					stmt.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				stmt = null;
    			}
    			if(conn != null) {
    				try {
    					conn.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				conn = null;
    			}
    		}
    	}
    }
    

    修改代码的实现

    package demo1;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.junit.Test;
    
    public class JDBCDemo3 {
    	
    	@Test
    	/**
    	 * 修改代码的实现
    	 */
    	public void demo2() {
    		Connection conn = null;
    		Statement stmt = null;
    		
    		try {
    			//注册驱动
    			Class.forName("com.mysql.cj.jdbc.Driver");
    			//获得连接
    			conn = DriverManager.getConnection("jdbc:mysql:///jdbctest?serverTimezone=Hongkong", "root", "1234");
    			//获得执行SQL语句的对象
    			stmt = conn.createStatement();
    			//编写SQL
    			String sql = "update user set username='qqq',password='1234',name='或无言' where uid=4";
    			//执行SQL
    			int i = stmt.executeUpdate(sql);
    			if(i > 0) {
    				System.out.println("修改成功!");
    			}
    		}catch(Exception e) {
    			e.printStackTrace();
    		}finally{
    			//释放资源
    			if(stmt != null) {
    				try {
    					stmt.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				stmt = null;
    			}
    			if(conn != null) {
    				try {
    					conn.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				conn = null;
    			}
    		}
    	}
    }
    

    删除代码的实现

    package demo1;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.junit.Test;
    
    public class JDBCDemo3 {
    	
    	@Test
    	/**
    	 * 删除代码的实现
    	 */
    	public void demo1() {
    		Connection conn = null;
    		Statement stmt = null;
    		
    		try {
    			//注册驱动
    			Class.forName("com.mysql.cj.jdbc.Driver");
    			//获得连接
    			conn = DriverManager.getConnection("jdbc:mysql:///jdbctest?serverTimezone=Hongkong", "root", "1234");
    			//获得执行SQL语句的对象
    			stmt = conn.createStatement();
    			//编写SQL
    			String sql = "delete from user where uid=4";
    			//执行SQL
    			int i = stmt.executeUpdate(sql);
    			if(i > 0) {
    				System.out.println("删除成功!");
    			}
    		}catch(Exception e) {
    			e.printStackTrace();
    		}finally{
    			//释放资源
    			if(stmt != null) {
    				try {
    					stmt.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				stmt = null;
    			}
    			if(conn != null) {
    				try {
    					conn.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				conn = null;
    			}
    		}
    	}
    }
    

    查询代码的实现

    package demo1;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    import org.junit.Test;
    
    public class JDBCDemo1 {
    	
    	@Test
    	/**
    	 * 查询代码的实现
    	 */
    	public void demo1() {
    		Connection conn = null;
    		Statement stmt = null;
    		ResultSet rs = null;
    		try {
    			//1.加载驱动
    			Class.forName("com.mysql.cj.jdbc.Driver");
    			
    			//2.获得连接
    			conn = DriverManager.getConnection("jdbc:mysql:///jdbctest?serverTimezone=Hongkong", "root", "1234");
    			
    			//3创建执行SQL语句的对象,并且执行SQL
    			//3.1创建执行SQL语句的对象
    			String sql = "select * from user";
    			stmt = conn.createStatement();
    			//3.2执行SQL
    			rs = stmt.executeQuery(sql);
    			while(rs.next()) {
    				int uid = rs.getInt("uid");
    				String username = rs.getString("username");
    				String password = rs.getString("password");
    				String name = rs.getString("name");
    				
    				System.out.println(uid + "-" + username + "-" + password + "-" + name);
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			//释放资源
    			if(rs != null) {
    				try {
    					rs.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				rs = null;
    			}
    			if(stmt != null) {
    				try {
    					stmt.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				stmt = null;
    			}
    			if(conn != null) {
    				try {
    					conn.close();
    				} catch (SQLException e) {
    					e.printStackTrace();
    				}
    				conn = null;
    			}
    		}
    	}
    }
    
  • 相关阅读:
    ajax上传文件按钮显示小手
    javascript函数执行前期变量环境初始化过程
    WinForm组件之多线程组件
    SQLServer 2008的数据库镜像实施笔记
    Reporting Service 在打印或预览时可能会导致出现空白页的原因
    C#程序实现动态调用DLL的研究
    人生值得学习的81句话
    asp.net中如何打印ReportViewer报表
    将 BLOB 值写入 SQL Server 时保留资源
    [翻译]使用ASP.NET2.0的ReportViewer查看RDLC报表
  • 原文地址:https://www.cnblogs.com/huowuyan/p/11623651.html
Copyright © 2020-2023  润新知