JDBC的增删改查

创建一个DBUtil类

 package com.zhongruan;

import java.io.Closeable;
import java.io.RandomAccessFile;
import java.sql.*;

public class DBUtil {
    public static Connection getconnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/zjgm?user=root&password=123456&characterEncoding=utf-8&useSSL=true");
        return connection;
    }
    public static void close(ResultSet resultSet, Statement statement,Connection connection) throws SQLException {
        if(resultSet!=null) {
            resultSet.close();
        }
        statement.close();
        connection.close();
    }

1.增加数据

package com.zhongruan;
    import javax.xml.transform.Result;
    import java.sql.*;
    public class insert {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            Connection connection = DBUtil.getconnection();
            String sql="insert into stu (name,age) value ('hualili',16)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.executeUpdate();
            DBUtil.close(null,statement,connection);
        }

2.删除数据

 package com.zhongruan;
    import javax.xml.transform.Result;
    import java.sql.*;
    public class delete{
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            Connection connection = DBUtil.getconnection();
            String sql="delete from stu where id=3";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.executeUpdate();
            DBUtil.close(null,statement,connection);
        }
    }

3.修改数据

package com.zhongruan;
    import javax.xml.transform.Result;
    import java.sql.*;
    public class update {
        public static void main(String[] args) throws ClassNotFoundException, SQLException {
            Connection connection = DBUtil.getconnection();
            String sql="update stu set name='hualili',age=16 where id=1";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.executeUpdate();
            DBUtil.close(null,statement,connection);
        }
    }

4.查看数据

package com.zhongruan;
    import javax.xml.transform.Result;
    import java.sql.*;
    public class select {
            public static void main(String[] args) throws SQLException, ClassNotFoundException {
                Connection connection = DBUtil.getconnection();
                String sql="select * from stu";
                PreparedStatement statement = connection.prepareStatement(sql);
                ResultSet resultSet = statement.executeQuery();
                while (resultSet.next()) {
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    int age = resultSet.getInt(3);
                    System.out.println(id + name + age);
                }
              DBUtil.close(resultSet,statement,connection);
            }
        }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
JDBC(Java Database Connectivity)是一种用于执行SQL语句的Java API,它提供了一种连接到数据库的标准方式。下面是JDBC增删改查的详细步骤: 1. 加载JDBC驱动程序 在使用JDBC之前,必须加载适合于数据库的JDBC驱动程序。不同的数据库有不同的JDBC驱动程序,因此你需要根据你使用的数据库加载相应的驱动程序。例如: ``` Class.forName("com.mysql.jdbc.Driver"); ``` 2. 建立数据库连接 在加载完JDBC驱动程序之后,就可以建立与数据库之间的连接了。连接的过程需要提供数据库的URL、用户名和密码。例如: ``` String url = "jdbc:mysql://localhost:3306/test?useSSL=false"; String user = "root"; String password = "123456"; Connection connection = DriverManager.getConnection(url, user, password); ``` 3. 创建Statement对象 在建立好数据库连接之后,就可以创建一个Statement对象。Statement对象用于执行SQL语句并返回结果。例如: ``` Statement statement = connection.createStatement(); ``` 4. 执行SQL语句 接下来,可以使用Statement对象执行SQL语句。例如: ``` String sql = "SELECT * FROM user"; ResultSet resultSet = statement.executeQuery(sql); ``` 如果要执行增、删、改操作,则可以使用Statement对象的executeUpdate()方法。例如: ``` String sql = "INSERT INTO user(name, age) VALUES('张三', 20)"; statement.executeUpdate(sql); ``` 5. 处理结果集 如果执行的是查询操作,则需要处理查询结果集。查询结果集以ResultSet对象的形式返回。例如: ``` while (resultSet.next()) { String name = resultSet.getString("name"); int age = resultSet.getInt("age"); System.out.println("name: " + name + ", age: " + age); } ``` 6. 关闭连接 在完成JDBC操作之后,需要关闭与数据库的连接。例如: ``` resultSet.close(); statement.close(); connection.close(); ``` 以上就是JDBC增删改查的详细步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值