JDBC系列:(2)使用Statement執行sql語句

來源:互聯網
上載者:User

標籤:jdbc   statement   

執行sql語句的介面
介面 作用
Statement介面 用於執行靜態sql語句
PreparedStatement介面 用於執行先行編譯sql語句
CallableStatement介面 用於執行預存程序的sql語句(call xxx)


1、執行DDL語句
package com.rk.db.b_statement;import java.sql.DriverManager;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;/** * 使用Statement執行DDL語句(建立表) * @author RK * */public class Demo01{static//靜態程式碼片段{try{//1.驅動註冊程式Class.forName("com.mysql.jdbc.Driver");}catch (ClassNotFoundException e){e.printStackTrace();throw new RuntimeException(e);}}public static void main(String[] args){Connection conn = null;Statement stmt = null;String url = "jdbc:mysql://localhost:3306/testdb";String user = "root";String password = "root";try{//2.擷取連線物件conn = DriverManager.getConnection(url, user, password);//3.建立Statementstmt = conn.createStatement();//4.準備sqlString sql = "CREATE TABLE T_Persons(Id INT PRIMARY KEY AUTO_INCREMENT, UserName VARCHAR(20), Pwd VARCHAR(20))";//5.發送sql語句,執行sql語句,得到返回結果int count = stmt.executeUpdate(sql);//6.輸出System.out.println("影響了"+count+"行!");//影響了0行!}catch (SQLException e){e.printStackTrace();}finally{//7.關閉串連(順序:後開啟的先關閉)closeQuietly(stmt);closeQuietly(conn);}}/** * 安靜的關閉 */private static void closeQuietly(AutoCloseable ac){if(ac != null){try{ac.close();}catch (Exception e){e.printStackTrace();}}}}


2、執行DML語句
package com.rk.db.b_statement;import java.sql.DriverManager;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;/** * 使用Statement執行DML語句 * @author RK * */public class Demo02{static{try{//1.驅動註冊程式Class.forName("com.mysql.jdbc.Driver");}catch (ClassNotFoundException e){throw new RuntimeException(e);}}public static void main(String[] args){Connection conn = null;Statement stmt = null;String url = "jdbc:mysql://localhost:3306/testdb";String user = "root";String password = "root";try{//2.擷取連線物件conn = DriverManager.getConnection(url, user, password);//3.建立Statementstmt = conn.createStatement();//4.準備sqlString sql = "INSERT INTO T_Persons(UserName,Pwd) VALUES(‘地球人‘,‘123456‘)";//增加//String sql = "UPDATE T_Persons SET UserName=‘火星人‘ WHERE Id=1";//修改//String sql = "DELETE FROM T_Persons WHERE Id=1";//刪除//5.發送sql語句,執行sql語句,得到返回結果int count = stmt.executeUpdate(sql);//6.輸出System.out.println("影響了"+count+"行!");//影響了1行!}catch (SQLException e){e.printStackTrace();}finally{closeQuietly(stmt);closeQuietly(conn);}}/** * 安靜的關閉 */private static void closeQuietly(AutoCloseable ac){if(ac != null){try{ac.close();}catch (Exception e){e.printStackTrace();}}}}



3、執行DQL語句
package com.rk.db.b_statement;import java.sql.DriverManager;import java.sql.Connection;import java.sql.SQLException;import java.sql.Statement;import java.sql.ResultSet;/** * 使用Statement執行DQL語句(查詢操作) * @author RK * */public class Demo03{static{try{//1.驅動註冊程式Class.forName("com.mysql.jdbc.Driver");}catch (ClassNotFoundException e){throw new RuntimeException(e);}}public static void main(String[] args){Connection conn = null;Statement stmt = null;ResultSet rs = null;String url = "jdbc:mysql://localhost:3306/testdb";String user = "root";String password = "root";try{//2.擷取連線物件conn = DriverManager.getConnection(url, user, password);//3.建立Statementstmt = conn.createStatement();//4.準備sqlString sql = "SELECT * FROM T_Persons";//5.發送sql語句,執行sql語句,得到返回結果rs = stmt.executeQuery(sql);//6.輸出while(rs.next()){int id = rs.getInt("Id");String userName = rs.getString("UserName");String pwd = rs.getString("Pwd");System.out.println(id + "\t" + userName + "\t" + pwd);}}catch (SQLException e){e.printStackTrace();}finally{closeQuietly(rs);closeQuietly(stmt);closeQuietly(conn);}}/** * 安靜的關閉 */private static void closeQuietly(AutoCloseable ac){if(ac != null){try{ac.close();}catch (Exception e){e.printStackTrace();}}}}



4、思維導圖


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/7F/F8/wKioL1czKejhkEiuAADsVB6cT98372.png" title="JDBC.png" alt="wKioL1czKejhkEiuAADsVB6cT98372.png" />






JDBC系列:(2)使用Statement執行sql語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.