JDBC 操作步骤


prtyaa
prtyaa 2023-12-27 16:35:40 60009 赞同 0 反对 0
分类: 资源
JDBC可以做三件事: 与数据库建立连接、发送、操作数据库的语句并处理结果;

JDBC访问数据的步骤分为6步

1:加载一个Driver驱动(需要添加mysql或者oralce的驱动包)

反射 Class.forName (驱动地址)
Class.forName("com.mysql.jdbc.Driver");

2:创建数据库连接(Connection)

// 2:创建数据库连接(Connection)
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/slayer","root","root");
路径解释 "jdbc:数据库名称://ip地址:端口号/数据库名称","用户名","密码"
"jdbc:mysql://localhost:3306/slayer","root","root"

3:创建SQL命令发送器Statement (prepareStatement)

Statement statement  = connection.createStatement();

4:通过Statement发送SQL命令并得到结果(insert/delete/update/select/语句)

// 4:通过Statement发送SQL命令并得到结果(insert/delete/update/select/语句)
        String sql = "insert into emp values(8888,'张三','CLERK','7788','2018-08-08','1000','2000',10)";
        int n = statement.executeUpdate(sql);

5:处理SQL结果

if (n>0){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }

6:关闭数据库资源(ResultSet)

connection.close();
statement.close();

完整代码

package com.lin;


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

public class jdbcTest {

    //向Departments 表中添加一个数据
    public void insertDepartments(String department_name,int location_id)  {
        Connection connection = null;
        Statement statement = null;
        try {
            //1.注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //创建链接 useUnicode=true 开启字符编码集 characterEncoding=utf-8 使用utf-8编码  目的:防止乱码
             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                    "root", "chaoge");
            //编辑sql语句
            String sql ="insert into departments values(default,'"+department_name+"','"+location_id+"')";
            //创建Statement对象用于发送数据
             statement = connection.createStatement();
            int flag = statement.executeUpdate(sql);
             if (falg ==1){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        } catch (Exception 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();
             }
         }
        }

    }

    //更新departments 表中的department_di 为6 的数据 部门名称修改为java研发部 location_id改为6
    public void updateDepartments( String department_id,String department_name, int location_id){
        Connection connection = null;
        Statement statement = null;

        try {
            //注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //创建链接对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/learn?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                    "root", "chaoge");
            //创建Statement对象用于发送数据
            statement =connection.createStatement();
            //创建SQL语句
            String sql =  "update departments d set d.department_name = '"+department_name+"',d.location_id = "+location_id+" where d.department_id ="+department_id;
            int falg = statement.executeUpdate(sql);
            if (falg ==1){
                System.out.println("修改成功");
            }else {
                System.out.println("修改失败");
            }
        } catch (Exception 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();
                }
            }
        }


    }




    public static void main(String[] args) {
        jdbcTest jdbcTest = new jdbcTest();
       // jdbcTest.insertDepartments("销售部",10);
        jdbcTest.updateDepartments("6","java研发部",6);

    }
}

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

评价 0 条
prtyaaL2
粉丝 1 资源 1949 + 关注 私信
最近热门资源
分享如何统信UOS系统在屏蔽mysql显性的用户名称以及密码  614
分享免费开源高速下载器  577
分享如何在银河麒麟高级服务器操作系统V10SP3中需要启用内核审计功能。  572
通过shell脚本在统信UOS/麒麟系统中安装nginx  504
分享如何查看网卡中断的数量  422
分享查询网卡所在PCI插槽链路能力及当前链路状态  420
麒麟系统资源下载合集(适配各类cpu)  413
麒麟系统进行内存清理  413
统信UOS常见问题小总结  411
winrar绿色无广告版分享  393
最近下载排行榜
分享如何统信UOS系统在屏蔽mysql显性的用户名称以及密码 0
分享免费开源高速下载器 0
分享如何在银河麒麟高级服务器操作系统V10SP3中需要启用内核审计功能。 0
通过shell脚本在统信UOS/麒麟系统中安装nginx 0
分享如何查看网卡中断的数量 0
分享查询网卡所在PCI插槽链路能力及当前链路状态 0
麒麟系统资源下载合集(适配各类cpu) 0
麒麟系统进行内存清理 0
统信UOS常见问题小总结 0
winrar绿色无广告版分享 0
作者收入月榜
1

prtyaa 收益395.97元

2

zlj141319 收益228.92元

3

IT-feng 收益215.02元

4

1843880570 收益214.2元

5

风晓 收益208.24元

6

777 收益173.02元

7

哆啦漫漫喵 收益131.6元

8

Fhawking 收益106.6元

9

信创来了 收益105.97元

10

克里斯蒂亚诺诺 收益91.08元

请使用微信扫码

加入交流群

请使用微信扫一扫!