1. 程式人生 > 實用技巧 >JDBC的增刪改查操作

JDBC的增刪改查操作

JDBC API:

DriverManager類:

registerDriver(Driver driver) ;註冊驅動

getConnection(String url, String user, String password) ;與資料庫建立連線

Connection介面:

createStatement() ;建立執行sql語句物件

prepareStatement(String sql) ;建立預編譯執行sql語句的物件

Statement介面:

ResultSet executeQuery(String sql) 根據查詢語句返回結果集。只能執行select語句。

int executeUpdate(String sql) 根據執行的DML(insert update delete)語句,返回受影響的行數。

boolean execute(String sql) 此方法可以執行任意sql語句。返回boolean值.

  • true: 執行select有查詢的結果
  • false: 執行insert, delete,update, 執行select沒有查詢的結果

ResultSet介面:

  • boolean next();將游標從當前位置向下移動一行

  • int getInt(int colIndex)以int形式獲取ResultSet結果集當前行指定列號值

  • int getInt(String colLabel)以int形式獲取ResultSet結果集當前行指定列名值

  • float getFloat(int colIndex)以float形式獲取ResultSet結果集當前行指定列號值

  • float getFloat(String colLabel)以float形式獲取ResultSet結果集當前行指定列名值

  • String getString(int colIndex)以String 形式獲取ResultSet結果集當前行指定列號值

  • String getString(String colLabel)以String形式獲取ResultSet結果集當前行指定列名值

  • Date getDate(int columnIndex); 以Date 形式獲取ResultSet結果集當前行指定列號值

  • Date getDate(String columnName);以Date形式獲取ResultSet結果集當前行指定列名值

  • void close()關閉ResultSet 物件

增加資料:

public class demo01 {
    @Test
    public void insert() throws Exception {
        //載入驅動
        Class.forName("com.mysql.jdbc.Driver");
        //獲得連線
        String url = "jdbc:mysql://localhost:3306/zw02";
        String user = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, user, password);
        //建立執行sql語句
        Statement statement = connection.createStatement();
        //執行sql語句,處理結果
        String sql = "insert into user values(null,'zl','123456','趙六')";
        int rows = statement.executeUpdate(sql);
        System.out.println("受影響的行數:" + rows);
        //釋放資源
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

修改資料:

@Test
    //修改資料庫的user表中的趙六的密碼為654321
    public void update()throws Exception {
        //載入驅動
        Class.forName("com.mysql.jdbc.Driver");
        //獲得連線
        String url = "jdbc:mysql://localhost:3306/zw02";
        String user="root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, user, password);
        //建立執行sql語句物件
        Statement statement=connection.createStatement();
        //執行sql語句,處理結果
        String sql="update user set password ='654321' where nickname ='趙六'";
        int rows = statement.executeUpdate(sql);
        System.out.println("受影響的行數:"+rows);
        //釋放資源
        if (statement !=null) {
            statement.close();
        }
        if (connection !=null) {
            connection.close();
        }
    }

刪除資料:

@Test
    public void delete() throws Exception {
        //載入驅動
        Class.forName("com.mysql.jdbc.Driver");
        //獲得連線
        String url = "jdbc:mysql://localhost:3306/zw02";
        String user = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, user, password);
        //建立執行sql語句物件
        Statement statement = connection.createStatement();
        //執行sql語句,處理結果
        String sql = "delete from user where id=3";
        int rows = statement.executeUpdate(sql);
        System.out.println("受影響的行數:"+rows);
        //釋放資源
        if (connection != null) {
            connection.close();
        }
        if (statement != null) {
            statement.close();
        }
    }

查詢資料:

 @Test
    public void select() throws Exception {
        //載入驅動
        Class.forName("com.mysql.jdbc.Driver");
        //獲得連線
        String url = "jdbc:mysql://localhost:3306/zw02";
        String user = "root";
        String password = "root";
        Connection connection = DriverManager.getConnection(url, user, password);
        //建立執行sql語句的物件
        Statement statement = connection.createStatement();
        //執行sql語句,獲得結果
        String sql = "select * from user";
        ResultSet resultSet = statement.executeQuery(sql);
        User use = null;
        while (resultSet.next()) {
            use = new User();

            //封裝資料
            use.setId(resultSet.getInt("id"));
            use.setUsername(resultSet.getString("username"));
            use.setPassword(resultSet.getString("password"));
            use.setNickname(resultSet.getString("nickname"));
        }
        //釋放資源
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
        if (use == null) {
            System.out.println("失敗");
        }else{
            System.out.println("成功");
        }
    }