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("成功");
        }
    }

 

posted @ 2020-10-13 19:07  化蛹  阅读(166)  评论(0编辑  收藏  举报