zoukankan      html  css  js  c++  java
  • dljd_016_jdbc中使用PreparedStatement执行DQL(查询)语句

    一、jdbc使用preparedStatement执行dql(查询)语句示例

      

    package edu.aeon.jdbc.crud;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import edu.aeon.aeonutils.AeonJdbcUtils;
    /**
     * [说明]:使用jdbc进行数据操作
     * @author aeon
     */
    public class JDBC_insert {
        /**
         * 使用jdbc进行增加操作(DML)
         */
        public static void jdbc_insert(){
            Connection connection=null;
            PreparedStatement preparedStatement = null;
            try {
                connection = AeonJdbcUtils.getMySqlConnection();
                String sql="insert into user(userid,username,userpw) values (?,?,?)";
                //将sql语句进行预编译然后保存到preparedStatement对象中
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, 10006);
                preparedStatement.setString(2, "aeon");
                preparedStatement.setString(3, "aeon");
                int rowCount = preparedStatement.executeUpdate();
                System.out.println(rowCount+"条数据被插入!");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                AeonJdbcUtils.closeDB(null, preparedStatement, connection);
            }
        }
        /**
         * 查询操作(DQL)
         * @param args
         */
        public static void jdbc_select(){
            Connection connection = null;
            PreparedStatement preparedStatement = null;
            ResultSet  resultSet = null;
            try {
                connection=AeonJdbcUtils.getMySqlConnection();
                String sql="select userid,username,userpw from user where userid > ?";
                preparedStatement=connection.prepareStatement(sql);
                preparedStatement.setInt(1, 10002);
                resultSet = preparedStatement.executeQuery();
                System.out.println("用户id	用户名	用户密码");
                while(resultSet.next()){
                int userid=resultSet.getInt("userid");
                String username=resultSet.getString("username");
                String userpw=resultSet.getString("userpw");
                System.out.println(userid+"	"+username+"	"+userpw);
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                AeonJdbcUtils.closeDB(resultSet, preparedStatement, connection);
            }
        }
        public static void main(String[] args) {
            //jdbc_insert();
            jdbc_select();
        }
    }

    数据库截图:

      

    执行结果截图:

      

    必须使用主键作为查询条件,启动的才是行级锁,否则依旧是表级锁,启动行级锁的目的是只有当前事务可以修改主键为x的某一条或几条记录,然后是结束事务,释放行级锁。
    如何在JDBC中使用行级锁,其步骤如下:
    1.关闭自动提交
    2.通过执行语句select * from table_name where id in(x,y) for update;引起事务,启动行级锁,锁住x,y这两条记录
    3.启动行级锁的目的就是只有当前事务才能修改x,y这两条记录。
    4.结束事务,释放行级锁。

    如有任何疑问可联系邮箱: 给我发邮件、或直接联系QQ:1584875179 || 点返回首页

  • 相关阅读:
    unity工厂模式
    unity对象池
    unity进阶项目------保卫萝卜(2)
    C#状态机
    unity进阶项目------保卫萝卜(1)
    OpenGL——外部读档+异常报错
    OpenGL入门之入门
    xlua build时 报错处理
    捕鱼达人Demo版下载
    UGUI-Text——自适应
  • 原文地址:https://www.cnblogs.com/aeon/p/10074712.html
Copyright © 2011-2022 走看看