JDBC Statement对象增删改查


prtyaa
prtyaa 2023-12-27 16:33:29 52644 赞同 0 反对 0
分类: 资源
JDBC查询操作 知识点: 查询操作区别于增删改(DML) 区别于增删改的: executeUpdate 使用 executeQuery() 注意: ()内是要跟SQL语句的 这里重点是要注意一下,查询的方式,其他的增删改基本一致 查询略微复杂 重写getInt方法中建议使用String参数的那一个,因为int类型,如果表格修改,容易出错 注意ResultSet中封装的并不是我们查询到的所有的结果集,而是返回了查询到的结果 集的数据库游标。通过ResultSet中的next(方法操作游标的位置获取结果集。(详细操作可以看后面的查询原始版那个是使用指针获取信息的)
package com.lin;


import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.Statement;


public class jdbcCRUD {

    //添加数据
    public void  insertJDBC(String department_name,int location_id){
        Connection connection =null;
        Statement statement = null;

        try {
            connection = jdbcUtil.getConnection();
            String sql = "insert into departments values(default,'" + department_name + "','" + location_id + "')";
            statement = connection.createStatement();
            int flag = statement.executeUpdate(sql);
            if (flag==1){
                System.out.println("添加成功");
            }else {
                System.out.println("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeResource(statement,connection,null);
        }

    }

    //删除数据
    public void deleteJDBCById(int department_id){
        Connection connection = null;
        Statement statement = null;
        try {
            connection = jdbcUtil.getConnection();
            String sql = "delete from departments where department_id="+department_id+"";
            statement =connection.createStatement();
            int flag = statement.executeUpdate(sql);
            if (flag==1){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeResource(statement, connection,null);
        }
    }
    //修改数据
    public void updateJDBCByid(int department_id,String department_name){
        Connection connection = null;
        Statement statement = null;

        try {
            connection = jdbcUtil.getConnection();
            String sql ="update departments set department_name = '"+department_name+"' where department_id="+department_id;
            statement = connection.createStatement();
            int flag = statement.executeUpdate(sql);
            if (flag==1){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
    }


    //查询数据
    public void  selectJDBCById(int department_id){
        Connection connection=null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            connection = jdbcUtil.getConnection();
            String sql = "select * from departments where department_id ="+department_id;
            statement = connection.createStatement();
            //执行查询返回结果
             resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                int department_id1 = resultSet.getInt("department_id");
                String department_name = resultSet.getString("department_name");
                int anInt = resultSet.getInt(3);//不建议使用
                System.out.println("部门编号:"+department_id1+"  部门名称:"+department_name+"  位置编号:"+anInt);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeResource(statement,connection,resultSet);
        }
    }


    public static void main(String[] args) {
        jdbcCRUD jdbcCRUD = new jdbcCRUD();
        // jdbcCRUD.insertJDBC("事业部",8);
        // jdbcCRUD.deleteJDBCById(16);
         jdbcCRUD.selectJDBCById(1);
       // jdbcCRUD.updateJDBCByid(1,"人事部");

    }
}

增删改原始版

package com.lin.liang;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUPDATE {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement =null;
        //声明变量
        try {
//      加载驱动
            Class.forName("com.mysql.jdbc.Driver");
//      连接数据库
            String  rul = "jdbc:mysql://127.0.0.1:3306/slayer";
            String  user = "root";
            String  pwd  = "root";
            connection = DriverManager.getConnection(rul,user,pwd);
//      创建Statement
             statement = connection.createStatement();
//      通过statement 发送SQL语句 得到结果
//            添加
            String sql ="insert into test1 values('测试5',123)";
//            删除
            String sql2 ="delete from test1 where name='测试6'";
//            修改
            String sql3 ="update test1 set pwd = 111 where name='测试1'";

            int n1 = statement.executeUpdate(sql);
            int n2 = statement.executeUpdate(sql2);
            int n3 = statement.executeUpdate(sql3);
//      处理结果
            if(n1>0&&n2>0&&n3>0){
                System.out.println("操作成功");
            }else {
                System.out.println("操作失败");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (statement !=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(connection!=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

查询原始版略微复杂

package com.lin.liang;

import java.sql.*;

public class JDBCExamine {
    public static void main(String[] args) {
//   申明
        Connection connection =null;
        Statement statement = null;
        ResultSet resultSet =null;
        try {
//   加载驱动
            Class.forName("com.mysql.jdbc.Driver");
//   建立连接
            String url ="jdbc:mysql://127.0.0.1:3306/slayer";
            String user="root";
            String pwd ="root";
            connection = DriverManager.getConnection(url,user,pwd);
//   创建Statement发送器
           statement =connection.createStatement();
//   创建SQL语句使用Statement 发送处理结果
            String sql ="select * from emp";
//   返回结果集  resultSet 结果集
            resultSet = statement.executeQuery(sql);
            while(resultSet.next()){
                //next 指针位置,初始位置在表格开头每次运行下移一位
//  1,2,3,4,5,6,7,8 代表列 的顺序
                int empno = resultSet.getInt(1);
                String ename = resultSet.getString(2);
                String  job = resultSet.getString(3);
                int mgr = resultSet.getInt(4);
                Date hiredate = resultSet.getDate("hiredate");
                double sal = resultSet.getDouble(6);
                double comm = resultSet.getDouble(7);
                int deptno = resultSet.getInt(8);
                System.out.println(empno+"--"+ename+"--"+job+"--"+mgr+"--"+hiredate+"--"+sal+"--"+comm+"--"+deptno);
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (statement !=null){
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection !=null){
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!

评价 0 条
prtyaaL2
粉丝 1 资源 1949 + 关注 私信
最近热门资源
银河麒麟桌面操作系统备份用户数据  125
统信桌面专业版【全盘安装UOS系统】介绍  120
银河麒麟桌面操作系统安装佳能打印机驱动方法  111
银河麒麟桌面操作系统 V10-SP1用户密码修改  105
最近下载排行榜
银河麒麟桌面操作系统备份用户数据 0
统信桌面专业版【全盘安装UOS系统】介绍 0
银河麒麟桌面操作系统安装佳能打印机驱动方法 0
银河麒麟桌面操作系统 V10-SP1用户密码修改 0
作者收入月榜
1

prtyaa 收益393.62元

2

zlj141319 收益218元

3

1843880570 收益214.2元

4

IT-feng 收益209.03元

5

风晓 收益208.24元

6

777 收益172.71元

7

Fhawking 收益106.6元

8

信创来了 收益105.84元

9

克里斯蒂亚诺诺 收益91.08元

10

技术-小陈 收益79.5元

请使用微信扫码

加入交流群

请使用微信扫一扫!