JDBC链接数据库,并实现简单的增删改查

准备工作:

数据库是:Mysql 8.0

数据库图形化工具是:navicat

java 编译器是:IDEA

框架是:maven

第一步:数据库的初始化

第二步:创建maven工程,导入JDBC的依赖

  <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.0.7</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>5.0.7.RELEASE</version>
            </dependency>

第三步:编写代码

部分代码截屏:

源代码:

package com.snd.s;

import java.sql.*;

public class text {
    public static void main(String[] args)throws Exception {
        //获取链接对象
        Connection connection = getConnection();
        //增添数据
        zeng(connection);
        //删除数据
        delete(connection);
        //修改表中的数据
        alter(connection);
        //查看表中所有的数据
        viewingADatabase(connection);
    }

    /**
     * 查看数据库中对应表的所有数据
     * @param connection 链接对象
     * @throws SQLException 异常上抛
     */
    public static void viewingADatabase(Connection connection) throws SQLException {
        //获取执行者对象
        Statement statement = connection.createStatement();
        //查询语句
        String sql = "SELECT * FROM user";
        //执行语句,并返回结果
        ResultSet resultSet2 = statement.executeQuery(sql);
        //处理结果
        while (resultSet2.next()){
            System.out.println(resultSet2.getLong("id")+"\t"+resultSet2.getString("name"));
        }
        //关闭资源
        resultSet2.close();
        statement.close();
        connection.close();
    }

    /**
     * 修改表中的数据
     * @param connection 链接对象
     * @throws SQLException 异常上抛
     */
    public static void alter(Connection connection) throws SQLException {
        String sql = "update user set name = '花木兰' where id = 15";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.executeUpdate();
    }
    /**
     * 删除条件对应的数据
     * @param connection 链接对象
     * @throws SQLException 异常上抛
     */
    public static void delete(Connection connection) throws SQLException {
        String sql = "delete from user where id=10";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.executeUpdate();
    }
    /**
     *  向表中插入数据
     * @param connection 链接对象
     * @throws SQLException 异常上抛
     */
    public static void zeng(Connection connection) throws SQLException {
        String sql = "INSERT INTO user(id,name)VALUES(15,'snd')";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.executeUpdate();
    }
    /**
     *
     * @return 链接对象
     * @throws SQLException 异常上抛
     * @throws ClassNotFoundException 异常上抛
     */
    public static Connection getConnection() throws SQLException, ClassNotFoundException {
        Class.forName("com.mysql.jdbc.Driver");
        //?useUnicode=true&characterEncoding=utf-8  设置字符集避免出现乱码
        //10.10.196.92   链接数据库的ip地址
        //3306   数据库服务的端口号
        //mybatis_plus  链接数据库的库名
        //root    用户名
        //***********  密码(自己设置的,一定要记好)
        Connection connection = DriverManager.getConnection("jdbc:mysql://10.10.196.92:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8", "root", "***********");
        return connection;
    }
}

实现JDBC链接数据库,并实现增删改查

测试结果:

 注意:每次运行完后,记得关闭资源,避免资源的浪费;我的代码中,是以查看表中所有的数据的viewingADatabase()方法结尾,所以在viewingADatabase()方法运行完后关闭资源就可以啦,在运行的途中关闭资源会报错。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为衣渐宽终不悔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值