Sql Api里面的executeQuery(),executeUpdate(),execute()的使用

来源:互联网 发布:php绑定域名 编辑:程序博客网 时间:2024/06/05 05:38

Statement提供了三种执行SQL语句的方法:executeQuery、executeUpdate和 execute。使用哪一个方法由 SQL 语句所产生的内容决定。
一、方法executeQuery()用户产生单个结果集的语句,使用最多的的执行SQL语句方法是executeQuery,这个方法用来执行select语句

public static selectSql(){     Connection connection=null;     PreparedStatement preparedStatement=null;     Result SetresultSet = null;     String className=”com.microsoft.sqlserver.jdbc.SQLServerDriver”;     String url=”jdbc:sqlserver://localhost:1433;databaseName=Test”;     String user=”sa”;     String password=”123”;     try {         connection=DriverManager.getConnection(url,user, password);         Class.forName(className);         Stringsql=”select * from Test_table”;         preparedStatement=connection.prepareStatement(sql);         resultSet=preparedStatement.executeQuery();         while (resultSet.next()) {             System.out.println(resultSet.getString(“name”));         }     }catch(SQLException e) {         // TODO Auto-generated catch block         e.printStackTrace();     }catch(ClassNotFoundException e) {         // TODO Auto-generated catch block         e.printStackTrace();     }finally{        try {            if (resultSet!=null) {            resultSet.close();            }            if (preparedStatement!=null) {                preparedStatement.close();            }            if (connection!=null) {                connection.close();            }        }catch(SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }} 

二、方法executeUpdate()用于执行insert,delete,update等SQL语句,executeupdate返回的值是一个整数,表示受影响的行数,对于create table等不操作行的语句,executeupdate的返回值为0, 下面示例是将“张三”的名字改为“张三1”:

public staic updateSql(){    Connection connection=null;    PreparedStatement preparedStatement=null;    String className="com.microsoft.sqlserver.jdbc.SQLServerDriver";    String url="jdbc:sqlserver://localhost:1433;databaseName=Test";    String user="sa";    String password="123";    try {        connection=DriverManager.getConnection(url,user,password);        Class.forName(className);        Stringsql="update Test_table set name='张三1' where name='张三'";        preparedStatement=connection.prepareStatement(sql);        if(preparedStatement.executeUpdate()>0) {            System.out.println("更新成功");        }else{        System.out.println("更新失败");    }    }catch(SQLException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }catch(ClassNotFoundException e) {        // TODO Auto-generated catch block        e.printStackTrace();    }finally{        try {            if (preparedStatement!=null) {                preparedStatement.close();            }            if (connection!=null) {                connection.close();            }        }catch(SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }}

三、execute()方法
execute方法应该仅在语句能返回多个ResultSet对象、多个更新计数或ResultSet对象与更新计数的组合时使用。
其返回值为布尔类型:是true时,表示执行的是查询语句,可以通过getResultset方法获取结果;返回值为false时,执行的是更新语句或DDL语句,getUpdateCount方法获取更新的记录数量
execute()方法处理的是较复杂的SQL语句或结果。
当某个过程返回两个结果集,则在使用方法 execute 执行该过程后,必须调用方法getResultSet()获得第一个结果集,然后调用适当的getXXX方法获取其中的值。要获得第二个结果集,需要先调用getMoreResults()方法,然后再调用getResultSet方法。
当某个过程返回两个更新计数,则首先调用方法getUpdateCount(),然后调用getMoreResults(),并再次调用getUpdateCount()。
对于不知道返回内容,则情况更为复杂。如果结果是ResultSet对象,则方法execute 返回 true;如果结果是 Java int,则返回 false。如果返回int,则意味着结果是更新计数或执行的语句是 DDL 命令。在调用方法 execute 之后要做的第一件事情是调用getResultSet或getUpdateCount。调用方法getResultSet可以获得两个或多个ResultSet对象中第一个对象;或调用方法getUpdateCount可以获得两个或多个更新计数中第一个更新计数的内容。
例:

public static updateSql1(){        Connection connection = null;        PreparedStatement preparedStatement = null;        String className = "com.microsoft.sqlserver.jdbc.SQLServerDriver";        String url = "jdbc:sqlserver://localhost:1433;databaseName=Test";        String user = "sa";        String password = "123";        try {            //select id from Test_table            connection= DriverManager.getConnection(url, user, password);            Class.forName(className);            Stringsql = "select id from Test_table";            preparedStatement= connection.prepareStatement(sql);            if(preparedStatement.execute()) {                System.out.println("获取的结果集是");                ResultSetresultSet=preparedStatement.getResultSet();                while (resultSet.next()) {                    System.out.println(resultSet.getString("id"));                }            }else {                System.out.println("更新的结果是:"+preparedStatement.getUpdateCount());            }        }catch(SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }catch(ClassNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }finally{            try {                if (preparedStatement != null) {                    preparedStatement.close();                }                if (connection != null) {                    connection.close();                }            }catch(SQLException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        }}
阅读全文
0 0
原创粉丝点击