• JDBC(2)Statement


    Statement:

    用于执行SQL语句的对象

    通过Connection的createStatement()方法得到一个Statement对象

    只有在获得了Statement对象之后才能执行SQL对象

    Statement常用的方法:

    ->ResultSet executeQuery() throws SQLException:用于执行查询语句,并返回查询结果对应的ResultSet,该方法只能用于执行查询语句

    ->int executeUpdate(String sql) throws SQLException:用于执行DML语句,返回首影响的行数。

    ->Boolean execute(String sql) throws SQLException:可以执行任何SQL语句。

    在执行结束之后必须要关闭Statement对象 

    executeUpdate:可以执行的SQL语句,可以是insert,update,delete,不可以是select

    执行SQL一般使用executeUpdate,execute使用的次数相对比较少

    现在代码跑起来:

    主要用于获取数据库的连接方法:

        public Connection getConnection() throws Exception{
            Properties properties = new Properties();
            InputStream in =
                    getClass().getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(in);
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String jdbcUrl = properties.getProperty("jdbcUrl");
            String driver = properties.getProperty("driver");
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(jdbcUrl, user, password);
            System.out.println(connection);
            return connection;
        }

    执行executeUpdate(sql)方法

    @Test
        public void InsertStatementTestJdbc() throws Exception{
        
            //1.获取数据库的连接
            Connection conn = getConnection();
            //2.准备的sql语句
            String sql = "insert into student(sname,sclass)" +  
                    "values('MrChengs','1111')"//3.执行插入操作
            //4.获取操作SQL语句的Statement对象
            //调用Connection的createStatement()方法来获取
            Statement statement = (Statement) conn.createStatement();
            //5.调用Statement对象的executeUpdate(sql)执行SQL语句的插入
            statement.executeUpdate(sql);
            
            //6.关闭Statement对象
            statement.close();
            //7.关闭连接
            conn.close();
        }

    会发现这个程序有点问题,关闭是不是应该放在finally?

    此时程序会更加完美一点

        @Test
        public void StatementTestInSert() throws Exception{
            Connection conn = null;
            Statement statement = null;
            try {
                conn = null;
                conn = getConnection();
                String sql = "insert into student(sname,sclass)" + "values('MrChengs','1111');";
                statement = null;
                statement = (Statement) conn.createStatement();
                statement.executeUpdate(sql);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if(statement !=null){
                        statement.close();
                    }
                } catch (Exception e) {        
                    e.printStackTrace();
                }finally {
                    if(conn != null){
                        conn.close();
                    }
                }
            }
        }

    一般使用到的insert/delete/update

    使用这个方法,我们一般要进行拼串

    在拼串的时候,我们需要注意拼串的一些规范

    防止在拼串的时候出错

    //插入
    //String sql = "insert into student(sname,sclass)" + "values('MrChengs','1111')";
    
    //删除
    //String sql = "DELETE From student where id=1";
    
    //修改
    String sql = "update student set sname = 'MrChengs' where id = 3";
    statement = null;

    简单的说一下:

    execute()方法几乎可以执行所有的SQL语句,但是执行SQL语句时比较麻烦

    所以一般不使用该方法。

    这里基本上讲完了。

  • 相关阅读:
    #Hadoop集群部署
    #Linux Keepalived 负载均衡
    #Linux LVS 负载均衡
    #Kafka 彻底删除topic
    #Linux Keepalived 双机热备
    Python #图片验证码
    Linux #tar
    MongoDB #$set的问题
    Django #CSRF
    keystone环境搭建(源码方式+yum方式)(ocata版本)
  • 原文地址:https://www.cnblogs.com/Mrchengs/p/9780241.html
Copyright © 2020-2023  润新知