java连接oracle和mysql数据库,并进行增删改查

记得导连接数据的依赖包
jdbc依赖包下载地址
不多说,直接附上代码

package com.fang.jdbc;

import java.sql.*;

/**
 * @program: blog
 * @description: 连接数据库并进行简单的操作
 * @author: Fang
 * @create: 2020-05-11 11:14
 **/
public class JDBC {

    /**
     * 取得与数据库的连接
     *
     * @return Connection
     */
    public Connection getConnection() {
        //数据库账号
        String user = "root";
        //数据库密码
        String pwd = "666";
        // 定义连接接口
        Connection con = null;
        try {
            /*------oracle数据库----*/
            //加载驱动器
            Class.forName("oracle.jdbc.driver.OracleDriver");
            //连接协议
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";

            /*---------mysql--------*/
            //加载驱动器
            // Class.forName("com.mysql.jdbc.Driver");
            //连接协议
            //String url="jdbc:mysql://localhost:3306/mysql里面你所创建的数据库的名字";
            //如果是mysql高于8.0版本包括8.0:com.mysql.jdbc.Driver改为com.mysql.cj.jdbc.Driver
            //且url改为jdbc:mysql://localhost:3306/mysql里面你所创建的数据库的名字?useSSL=false&serverTimezone=UTC
            con = DriverManager.getConnection(url, user, pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    /**
     * 添加数据
     *
     * @return
     */
    public boolean insert() {
        //取得连接
        Connection connection = getConnection();

        //创建Sql语句预执行器--防止恶意的sql注入
        PreparedStatement ps = null;
        String sql = "insert into tb_test (name,age,sex) values(?,?,?)";

        boolean flag = false;
        try {
            //预编译
            ps = connection.prepareStatement(sql);
            ps.setString(1, "fang");
            ps.setString(2, "666");
            ps.setString(3, "男");

            //开始执行
            flag = ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }

    /**
     * 删除
     *
     * @return
     */
    public boolean delete(String name) {
        //取得连接
        Connection connection = getConnection();

        //创建Sql语句预执行器--防止恶意的sql注入
        PreparedStatement ps = null;
        String sql = "delete from tb_test where name=?";

        boolean flag = false;
        try {
            //预编译
            ps = connection.prepareStatement(sql);
            ps.setString(1, name);
            //开始执行
            flag = ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }

    /**
     * 修改数据
     *
     * @return
     */
    public boolean update(String name, int age) {
        //取得连接
        Connection connection = getConnection();

        //创建Sql语句预执行器--防止恶意的sql注入
        PreparedStatement ps = null;
        String sql = "update tb_test set age=? where name=?";

        boolean flag = false;
        try {
            //预编译
            ps = connection.prepareStatement(sql);
            ps.setInt(1, age);
            ps.setString(2, name);
            //开始执行
            flag = ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return flag;
    }

    /**
     * 根据名字查询数据
     *
     * @return
     */
    public void query(String name) {
        //取得连接
        Connection connection = getConnection();

        //创建Sql语句预执行器--防止恶意的sql注入
        PreparedStatement ps = null;
        String sql = "select * from tb_test where name=?";

        ResultSet rs = null;
        try {
            //预编译
            ps = connection.prepareStatement(sql);
            ps.setString(1, name);
            //开始执行
            rs = ps.executeQuery();
            while (rs.next()) {
                /*通过数据库表中的字段名取数据*/
                String uname = rs.getString("name");
                int age = rs.getInt("age");
                String sex = rs.getString("sex");

                System.out.println("name="+ uname);
                System.out.println("age="+ age);
                System.out.println("sex="+ sex);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值