idea连接MySQL数据库实现增删改


前言

上次写了idea连接jdbc,并执行了查询方法,这次实现增删改方法
说点什么才能让你们点这个链接转到上一个文章

一、添加实现代码

上次是直接写在main函数中的,这次改变一下,直接上代码

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

/**
 * 类名:Test1
 * 读书破万卷,下笔如有神
 * 代码反行之,算法记于心
 * 作者:劫恋李
 * 日期:2021/6/14 12:40
 * 版本:V1.0
 */
public class Test1 {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Test1 test1 = new Test1();
        test1.add();
    }
    public void add() throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("1.完成注册");
        //建立连接,
        String url="jdbc:mysql://localhost:3306/ljl";
        //主要协议:次要协议://主机名:端口号/数据库名
        Connection connection = DriverManager.getConnection(url, "root", "root");
        System.out.println("2.完成连接");
        String sql="insert into users values(null,'乙方','123',20,'男')";
       	PreparedStatement preparedStatement=connection.prepareStatement(sql);
        System.out.println("3.创建成功");
        //注意和查询用的不同!
        preparedStatement.executeUpdate();
        System.out.println("4.执行sql语句成功");
        preparedStatement.close();
        connection.close();
        System.out.println("5.关闭连接");
    }
}

这里有一点注意:因为涉及到对数据库的修改,所以要用executeUpdate();函数
这点和查询不同!

二、 添加代码优化

不知道你们发现这样写有问题吗,反正添加进去了,我不想添加不变的数据,我想自己指定数据(那你想填啥写啥不就行了)。。添加方法我是用来调用的,我不能每次都上方法里面修改数据!太麻烦,且不能一次添加多条数据!我们只需要修改一个地方就行了!

1.添加优化部分

//      String sql="insert into users values(null,'乙方','123',20,'男')";
		String sql="insert into users values(null,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,uname);
        preparedStatement.setString(2,upass);
        preparedStatement.setInt(3,uage);
        preparedStatement.setString(4,usex);

一个 ? 代表一个 位置,表中有几个数据就有几个问号,我用户id是自增的,所以设置了空

2.完整优化代码

代码如下(示例):

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

/**
 * 类名:Test1
 * 读书破万卷,下笔如有神
 * 代码反行之,算法记于心
 * 作者:劫恋李
 * 日期:2021/6/14 12:40
 * 版本:V1.0
 */
public class Test1 {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Test1 test1 = new Test1();
        test1.add("丙方","1234","女",34);
        test1.add("乙方","123","男",23);
    }


    public void add(String uname,String upass,String usex,int uage) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("1.完成注册");
        //建立连接,
        String url="jdbc:mysql://localhost:3306/ljl";
        //主要协议:次要协议://主机名:端口号/数据库名
        Connection connection = DriverManager.getConnection(url, "root", "root");
        System.out.println("2.完成连接");
//        String sql="insert into users values(null,'乙方','123',20,'男')";
        String sql="insert into users values(null,?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,uname);
        preparedStatement.setString(2,upass);
        preparedStatement.setInt(3,uage);
        preparedStatement.setString(4,usex);
        System.out.println("3.创建成功");

        preparedStatement.executeUpdate();
        System.out.println("4.执行sql语句成功");

        preparedStatement.close();
        connection.close();
        System.out.println("5.关闭连接");
    }
   //修改删除代码跟在这个后面,都在Test1 里面,别把代码复制别的地方去。
}

运行结果
在这里插入图片描述

在这里插入图片描述

二、 修改删除

	public  void update(String uname,String upass,String usex,int uage,int uid) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("1.用户注册成功");
        //通过驱动管理器获取数据库链接对象
        // 2.建立连接
        String url="jdbc:mysql://localhost:3306/ljl";
        Connection connection = DriverManager.getConnection(url, "root", "root");
        System.out.println("2.数据库链接成功");

        //3.通过连接对象创建预编译陈述对象
        String sql="update users set uname=?,upassword=?,usex=?,uage=? where uid=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,uname);
        preparedStatement.setString(2,upass);
        preparedStatement.setInt(3,uage);
        preparedStatement.setString(4, usex);
        preparedStatement.setInt(5,uid);
        System.out.println("3.创建预编译陈述对象成功");
        //4通过陈述对象执行sql语句


        preparedStatement.executeUpdate();
        System.out.println("4.执行sql语句成功");
        //关闭数据库资源
        preparedStatement.close();
        connection.close();
        System.out.println("5.关闭数据库资源成功!");
    }


    //删除
    public  void delete(int uid) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        System.out.println("1.用户注册成功");
        //通过驱动管理器获取数据库链接对象
        // 2.建立连接
        String url="jdbc:mysql://localhost:3306/ljl";
        Connection connection = DriverManager.getConnection(url, "root", "root");
        System.out.println("2.数据库链接成功");

        //3.通过连接对象创建预编译陈述对象
        String sql="delete  from users where uid=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,uid);
        System.out.println("3.创建预编译陈述对象成功");
        //4通过陈述对象执行sql语句


        preparedStatement.executeUpdate();
        System.out.println("4.执行sql语句成功");
        //关闭数据库资源
        preparedStatement.close();
        connection.close();
        System.out.println("5.关闭数据库资源成功!");
    }

   public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Test1 test1 = new Test1();
//        test1.add("丙方","1234","女",34);
//        test1.add("乙方","123","男",23);
//       调用方法
         test1.update("123","123","女",21,5);
         test1.delete(1);
    }

不运行了,就sql语句不一样,没啥大问题,相信你们能学会!。
你们可以尝试将查询语句写成方法形式,在main函数中调用!链接在上方!

总结

本次主要对数据库进行修改,注意的是调用的是:preparedStatement中的executeUpdate方法,就这里和查询调用的不同
其他的就是基本的sql语句了。

在这里插入图片描述

  • 8
    点赞
  • 89
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
首先,您需要在IDEA中导入MySQL连接器,以便能够连接数据库。然后,您需要打开Navicat并连接到您的MySQL数据库。 接下来,您可以使用以下代码示例将数据插入到数据库中: ```java try { // 连接数据库 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建要插入的SQL语句 String sql = "INSERT INTO mytable (column1, column2, column3) VALUES (?, ?, ?)"; // 创建预编译的语句 PreparedStatement statement = connection.prepareStatement(sql); // 设置参数 statement.setString(1, "value1"); statement.setString(2, "value2"); statement.setString(3, "value3"); // 执行语句 statement.executeUpdate(); // 关闭连接 connection.close(); } catch (SQLException e) { e.printStackTrace(); } ``` 要更新数据,您可以使用以下代码示例: ```java try { // 连接数据库 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建要更新的SQL语句 String sql = "UPDATE mytable SET column1 = ? WHERE column2 = ?"; // 创建预编译的语句 PreparedStatement statement = connection.prepareStatement(sql); // 设置参数 statement.setString(1, "new value"); statement.setString(2, "value2"); // 执行语句 statement.executeUpdate(); // 关闭连接 connection.close(); } catch (SQLException e) { e.printStackTrace(); } ``` 要删除数据,您可以使用以下代码示例: ```java try { // 连接数据库 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password"); // 创建要删除的SQL语句 String sql = "DELETE FROM mytable WHERE column1 = ?"; // 创建预编译的语句 PreparedStatement statement = connection.prepareStatement(sql); // 设置参数 statement.setString(1, "value1"); // 执行语句 statement.executeUpdate(); // 关闭连接 connection.close(); } catch (SQLException e) { e.printStackTrace(); } ``` 请注意,您需要将上面的代码替换为您自己的数据库和表名称,以及您的用户名和密码。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值