JDBC增删改查

1  添加数据

```java
    private static void add() {
        //1.加载启动类反射核心: Class
        Connection conn =null;
        Statement statement =null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接对象Connection通过DriverManager的静态方法getConnection()
            //jdbc:mysql jdbc连接的数据库是mysql
            // localhost:3306   mysql数据库默认端口
            // ygy  选择连接的数据库名字(存储在mysql数据库中的)
            String url="jdbc:mysql://localhost:3306/ygy?useUnicode=true&characterEncoding=utf8&useSSL=false";
            conn = DriverManager.getConnection(url, "root", "123456");
            System.out.println(conn);
            System.out.println("与数据库服务器连接上了...");

            //3.创建Statement对象
            statement = conn.createStatement();

            //4.编写sqL语句
            int empno=520;
            String ename="lmz";
            String job="clerk";
            int mgr=1314;
            String hiredate= "2004-02-04";
            double sal=4000;
            double comm=500;
            int deptno=20;
            String sql="insert into emp values("+empno+",'"+ename+"','"+job+"',"+
                    mgr+",'"+hiredate+"',"+sal+","+comm+","+deptno+")";

            //5.执行之执 行增删改sql:Statement的int executeUpdate(sql)
            //执行查询sql: Statement 的ResultSet executeQuery(sql)
            int rs=statement.executeUpdate(sql);
            if (rs>0){
                System.out.println("添加成功");
            }
        } catch (ClassNotFoundException e) {
            //Driver类找不到
            //出现原因: 1.驱动jar没到导入 2.驱动类包,类名字符串写错
            e.printStackTrace();
        } catch (SQLException throwables) {
            出现原因: 1.数据库服务没有开启 2.url写错 3.用户名错误 4.密码错误
            throwables.printStackTrace();
        }finally {
            //关闭资源
            try {
                if (conn!=null)  conn.close();
                if (statement!=null) statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }
```

2 删除数据

```java
    public static void main(String[] args) {
        //1.加载启动类反射核心: Class
        Connection conn =null;
        Statement statement =null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接对象Connection通过DriverManager的静态方法getConnection()
            //jdbc:mysql jdbc连接的数据库是mysql
            // localhost:3306   mysql数据库默认端口
            // ygy  选择连接的数据库名字(存储在mysql数据库中的)
            String url="jdbc:mysql://localhost:3306/ygy?                            useUnicode=true&characterEncoding=utf8&useSSL=false";
            conn = DriverManager.getConnection(url, "root", "123456");
            System.out.println(conn);
            System.out.println("与数据库服务器连接上了...");

            //3.创建Statement对象
            statement = conn.createStatement();

            //4.编写sqL语句
            System.out.println("请输入要删除的员工编号:");
            Scanner input=new Scanner(System.in);
            int empno=input.nextInt();
            String sql="delete from emp where empno="+empno;

            //5.执行之执 行增删改sql:Statement的int executeUpdate(sql)
            //执行查询sql: Statement 的ResultSet executeQuery(sql)
            int rs=statement.executeUpdate(sql);
            if (rs>0){
                System.out.println("删除成功");
            }
        } catch (ClassNotFoundException e) {
            //Driver类找不到
            //出现原因: 1.驱动jar没到导入 2.驱动类包,类名字符串写错
            e.printStackTrace();
        } catch (SQLException throwables) {
            //出现原因: 1.数据库服务没有开启 2.url写错 3.用户名错误 4.密码错误
            throwables.printStackTrace();
        }finally {
            //6.关闭资源
                try {
                    if (conn!=null)  conn.close();
                    if (statement!=null) statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
        }
    }
```

3  修改数据

```java
 private static void alter() {
        //1.加载启动类反射核心: Class
        Connection conn =null;
        Statement statement =null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接对象Connection通过DriverManager的静态方法getConnection()
            //jdbc:mysql jdbc连接的数据库是mysql
            // localhost:3306   mysql数据库默认端口
            // ygy  选择连接的数据库名字(存储在mysql数据库中的)
            String url="jdbc:mysql://localhost:3306/ygy?useUnicode=true&characterEncoding=utf8&useSSL=false";
            conn = DriverManager.getConnection(url, "root", "123456");
            System.out.println(conn);
            System.out.println("与数据库服务器连接上了...");

            //3.创建Statement对象
            statement = conn.createStatement();

            //4.编写sqL语句
            System.out.println("请输入要加薪的员工编号:");
            Scanner input=new Scanner(System.in);
            int empno=input.nextInt();
            String sql="update emp set sal=sal+500 where empno="+empno;

            //5.执行之执 行增删改sql:Statement的int executeUpdate(sql)
            //执行查询sql: Statement 的ResultSet executeQuery(sql)
            int rs=statement.executeUpdate(sql);
            if (rs>0){
                System.out.println("加薪成功");
            }
        } catch (ClassNotFoundException e) {
            //Driver类找不到
            //出现原因: 1.驱动jar没到导入 2.驱动类包,类名字符串写错
            e.printStackTrace();
        } catch (SQLException throwables) {
            出现原因: 1.数据库服务没有开启 2.url写错 3.用户名错误 4.密码错误
            throwables.printStackTrace();
        }finally {
            //关闭资源
            try {
                if (conn!=null)  conn.close();
                if (statement!=null) statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }
```

4  查询数据

```java
    private static void query() {
        //1.加载启动类反射核心: Class
        Connection conn =null;
        Statement statement =null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //2.得到连接对象Connection通过DriverManager的静态方法getConnection()
            //jdbc:mysql jdbc连接的数据库是mysql
            // localhost:3306   mysql数据库默认端口
            // ygy  选择连接的数据库名字(存储在mysql数据库中的)
            String url="jdbc:mysql://localhost:3306/ygy?useUnicode=true&characterEncoding=utf8&useSSL=false";
            conn = DriverManager.getConnection(url, "root", "123456");
            System.out.println(conn);
            System.out.println("与数据库服务器连接上了...");

            //3.创建Statement对象
            statement = conn.createStatement();

            //4.编写sqL语句
            System.out.println("请输入要查询员工姓名:");
            Scanner input=new Scanner(System.in);
            String name=input.next();
            String sql="select * from emp where ename="+"'"+name+"'";

            //5.执行之执 行增删改sql:Statement的int executeUpdate(sql)
            //执行查询sql: Statement 的ResultSet executeQuery(sql)
            rs = statement.executeQuery(sql);
            //boolean next()
            //判断是否有下一行
            //如果有:返回true
            //往下移动指针
            //如果没有:返回false
            while (rs.next()){
                //循环获取数据,一行一行的获取
                //一定要先调用next()
                //获取这一行的单元格中数据
                // getXxx(int 列序号):从1开始
                int empno=rs.getInt(1);
                String ename=rs.getString(2);
                String job=rs.getString(3);
                int mgr=rs.getInt(4);
                String hiredate= String.valueOf(rs.getDate(5));
                double sal=rs.getDouble(6);
                double comm=rs.getDouble(7);
                int deptno=rs.getInt(8);
                System.out.println(empno+" | "+ename+" | "+job+" | "+mgr+" | "+hiredate+" | "+sal+" | "+comm+" | "+deptno);
            }
            if (rs!=null){
                System.out.println("查询成功");
            }
        } catch (ClassNotFoundException e) {
            //Driver类找不到
            //出现原因: 1.驱动jar没到导入 2.驱动类包,类名字符串写错
            e.printStackTrace();
        } catch (SQLException throwables) {
            出现原因: 1.数据库服务没有开启 2.url写错 3.用户名错误 4.密码错误
            throwables.printStackTrace();
        }finally {
            //关闭资源
            try {
                if (rs!=null)  rs.close();
                if (conn!=null)  conn.close();
                if (statement!=null) statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值