1. 程式人生 > >JDBC(2)Statement

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語句時比較麻煩

所以一般不使用該方法。

這裡基本上講完了。