zoukankan      html  css  js  c++  java
  • 使用JDBC实现CRUD(增删改查)

    使用JDBC进行增删改查是一切框架的基础,本文主要讲述使用JDBC进行增删改查,方便日后复习

    1)获取连接(Connection),包括获取连接的四要素(用户名,使用的Driver具体类、密码、URL),Connection是获取其他信息的基础,但是这个连接是怎么获取的,还存在疑问。

    public static Connection getConnection() throws Exception {
    		Connection connection;
    		//获取连接的四要素
    		String driver = "com.mysql.jdbc.Driver";
    		String username = "root";		
    		String password = "root";
    		String url = "jdbc:mysql://localhost:3306/university";
    
    		Class.forName(driver);
    
    		connection = DriverManager.getConnection(url, username, password);
    
    		return connection;
    	}
    

    2)通过Connection获取一些连接信息:

    public static void main(String[] args) throws Exception {
    Connection connection = InsertTest.getConnection();
    //输出数据库名称
    System.out.println(connection.getCatalog());
    //是否自动提交
    System.out.println(connection.getAutoCommit());
    //这个事物隔离级别必须是1、2、4、8,不然的话会抛出异常(为啥不直接做成枚举呢?)
    connection.setTransactionIsolation(2);
    //获取事物隔离级别,1,2,4,8(事物隔离级别也是一大块,需要)
    System.out.println(connection.getTransactionIsolation());

    //判断当前连接是否断开,connection.close();可以手动断开连接
    System.out.println(connection.isClosed());

    //是否只读模式
    System.out.println(connection.isReadOnly());

    //验证连接是否有效,参数为0时,表示不用参数
    System.out.println(connection.isValid(0));

    }

    3)插入数据,其中使用PreparedStatement比使用Statement好的原因,可以参考《PreparedStatement与Statement的区别》,PreparedStatement的API函数可以参考《PreparedStatement的使用》

    public static void main(String[] args) throws Exception {
    		//插入数据
    		Connection connection = InsertTest.getConnection();
    		
    		String sql = "insert into department (dept_name,building, budget) values(?,?,?)";
    		//使用PreparedStatement比使用Statement好的原因,可以参考《PreparedStatement与Statement的区别》
    		PreparedStatement ps;
    		int i = 0;
    		
    		ps = connection.prepareStatement(sql);
    		//parameterIndex the first parameter is 1, the second is 2, ...
    		ps.setString(1, "Chinese");
    		ps.setString(2, "Tech and math");
    		ps.setDouble(3, 7692444.3332);
    		
    		//执行插入,PreparedStatement的API函数可以参考《PreparedStatement的使用》
    		i = ps.executeUpdate();
    		System.out.println(i);
    		//测试程序,简单地进行关闭资源
    		ps.close();
    		connection.close();
    	}
    

    4)执行查询操作

    	public static void main(String[] args) throws Exception {
    		Connection connection;
    		String sql = "select dept_name,building, budget from department";
    		PreparedStatement ps;
    
    		connection = getConnection();
    		ps = connection.prepareStatement(sql);
    		
    		//ResultSet关于某个表的信息或一个查询的结果。您必须逐行访问数据行,但是您可以任何顺序访问列。 
    		ResultSet set = ps.executeQuery();
    		
    		//这样获取的是一行结果数据包含了多少个字段
    		//set.getMetaData().getColumnCount();
    		
    		//获取到了ResultSet之后,ResultSet将会指向查询结果中的第一行
    		while(set.next()){
    			//通过ResultSetMetaData获取ResultSet的行名称
    			String deptName = set.getMetaData().getColumnName(1);
    			String buildingName = set.getMetaData().getColumnName(2);
    			String budgetName = set.getMetaData().getColumnName(3);
    			String dept_name = set.getString(deptName);
    			String building = set.getString(buildingName);
    			Double budget = set.getDouble(budgetName);
    			
    			
    			System.out.println(dept_name + "---" + building + "---" + budget);
    		}
    
    		ps.close();
    		connection.close();
    	}
    

    5)执行修改操作

    public static void main(String[] args) throws Exception {
    		
    		Connection connection = getConnection();
    		//同样是使用一般的sql语句
    		String sql = "update department set budget = ? where dept_name = ?";
    		PreparedStatement statement = connection.prepareStatement(sql);
    		statement.setDouble(1, 1002.00);
    		statement.setString(2, "Math");
    		int i = statement.executeUpdate();
    		System.out.println(i);
    		
    	}
    

    6)执行删除操作

    public static void main(String[] args) throws Exception {
    		Connection connection = InsertTest.getConnection();
    		String sql = "delete from department where dept_name = ?";
    		
    		PreparedStatement ps = connection.prepareStatement(sql);
    		//删除dept_name为Chinese的列
    		ps.setString(1, "Chinese");
    		//connection.prepareStatement(sql);进行了预编译,那么executeUpdate是否还需要传入sql参数?
    		ps.executeUpdate();
    		
    		ps.close();
    		connection.close();
    	}

    总结:JDBC中的增删改查大体如此,但是还有很多细节的问题需要仔细了解一下,比如说,预编译的过程,PreparedStatement的executeUpdate()方法带参数和不带参数的区别,都需要琢磨一下。

  • 相关阅读:
    PHP基础1
    U2-Net网络学习笔记(记录)
    C++贪吃蛇游戏
    实习期间学习基础学习整理
    week 2020.1.10-2020.1.15
    week 2021.1.04-2021.1.08
    week 2020.12.21-2020.12.31
    周记 week 2020-12.14-12.18
    几种读取图片和标签的方法
    图像风格转换(Style Transfer | 风格迁移综述)
  • 原文地址:https://www.cnblogs.com/dingcx/p/8797480.html
Copyright © 2011-2022 走看看