dljd_007_jdbc程式設計中的statement執行DML/DDL
阿新 • • 發佈:2018-12-05
一、使用statement的executeUpdate執行DML(增刪改)語句示例
package edu.aeon.jdbc; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * [說明]:使用statement的executeUpdate執行DML(增刪改) * statement的executeQuery用於執行DQL(select ...)語句。 *@author aeon */ public class TestStatment { public static void main(String[] args) { Connection connection=null; Statement statement=null; try { //2.1載入/註冊驅動 //driver是jdbc宣告的標準介面、com.mysql.jdbc.Driver是mysql資料庫廠商根據這個標準做的實現、注意這兩個Driver是有區別的。 Driver driver=newcom.mysql.jdbc.Driver(); //介面宣告引用指向實現類的物件 DriverManager.registerDriver(driver); //2.2獲取和資料庫伺服器的連線(java程式是客戶端 mysql是個伺服器) String username="root"; //使用者名稱 String password="root"; //密碼 //url中的jdbc:mysql說明:jdbc是由sun公司制定的一套網路協議 jdbc:mysql是指jdbc協議下的mysql子協議。String url="jdbc:mysql://localhost:3306/db_test"; //2.3連線伺服器 Connection是jdbc規範中宣告的介面 connection=DriverManager.getConnection(url, username, password); //2.4通過連線物件獲取執行sql語句的物件 statement=connection.createStatement(); String dml_sql="insert into user(userid,username,userpw) values(10006,'aeon','aeon')"; //2.6輸出結果 int rows=statement.executeUpdate(dml_sql); System.out.println(rows+"行資料插入成功!"); } catch (SQLException e) { e.printStackTrace(); }finally{ if(null!=statement){ try { statement.close(); } catch (SQLException e) { System.out.println("關閉流失敗!--->statement"); e.printStackTrace(); } } if(null!=connection){ try { connection.close(); } catch (SQLException e) { System.out.println("關閉流失敗!--->connection"); e.printStackTrace(); } } } } }
結果截圖:
我們再來看看資料庫插入前和插入後截圖:
注意:使用statement執行DML返回的結果是int型別的、這個int型別的具體值代表你執行後影響資料庫的行數。
二、使用statement的executeUpdate執行DDL(建立)語句
package edu.aeon.jdbc; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; /** * [說明]:使用statement的executeUpdate執行DDL(建立) * statement的executeQuery用於執行DQL(select ...)語句。 * @author aeon */ public class TestStatment { public static void main(String[] args) { Connection connection=null; Statement statement=null; try { //2.1載入/註冊驅動 //driver是jdbc宣告的標準介面、com.mysql.jdbc.Driver是mysql資料庫廠商根據這個標準做的實現、注意這兩個Driver是有區別的。 Driver driver=new com.mysql.jdbc.Driver(); //介面宣告引用指向實現類的物件 DriverManager.registerDriver(driver); //2.2獲取和資料庫伺服器的連線(java程式是客戶端 mysql是個伺服器) String username="root"; //使用者名稱 String password="root"; //密碼 //url中的jdbc:mysql說明:jdbc是由sun公司制定的一套網路協議 jdbc:mysql是指jdbc協議下的mysql子協議。 String url="jdbc:mysql://localhost:3306/db_test"; //2.3連線伺服器 Connection是jdbc規範中宣告的介面 connection=DriverManager.getConnection(url, username, password); //2.4通過連線物件獲取執行sql語句的物件 statement=connection.createStatement(); String ddl_sql="create table test(id int primary key,name varchar(10))";//簡單的DDL測試語句 //2.6輸出結果 int result=statement.executeUpdate(ddl_sql); System.out.println("返回的結果是:"+result); } catch (SQLException e) { e.printStackTrace(); }finally{ if(null!=statement){ try { statement.close(); } catch (SQLException e) { System.out.println("關閉流失敗!--->statement"); e.printStackTrace(); } } if(null!=connection){ try { connection.close(); } catch (SQLException e) { System.out.println("關閉流失敗!--->connection"); e.printStackTrace(); } } } } }
執行結果截圖:
我們來看一下資料庫執行前後的資料: