JDBC增、删、改、查(简单案例)

目录

增加案例

删除案例

修改案例

查询案例


增加案例

package com.yy.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author 杨 
 * @CreateDate 2023-04-12 11:20
 **/
public class InsertTest {
    static String url="jdbc:mysql://localhost:3306/db01?serverTimezone=UTC";
    static String user="root";
    static String pwd="123sa";
    static Connection conn= null;
    static PreparedStatement ps= null;
    public static void main(String[] args) {
        String sql="insert into user() values(null,?,?,?,?,?)";
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection(url, user, pwd);
            //预加载Statement对象,数据库的操作对象
            ps = conn.prepareStatement(sql);
            //预处理加载 ? 问号
            ps.setString(1, "付老板");
            ps.setString(2, "12345");
            ps.setInt(3, 23);
            ps.setString(4, "男");
            ps.setString(5, "长治");

            //执行sql
            int i = ps.executeUpdate();
            if (i>0){
                System.out.println("增加成功...");
            }else {
                System.out.println("增加失败...");
            }
        } catch (Exception e) {
            System.out.println("异常信息:"+e);
        }finally {
            try {
                //关流
                ps.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e);
            }
        }

    }
}

删除案例

package com.yy.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author 杨 
 * @CreateDate 2023-04-12 11:20
 **/
public class DeleteTest {
    static String url="jdbc:mysql://localhost:3306/db01?serverTimezone=UTC";
    static String user="root";
    static String pwd="123sa";
    static Connection conn= null;
    static PreparedStatement ps= null;
    public static void main(String[] args) {
        String sql="delete from user where u_id=?";
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection(url, user, pwd);
            //预加载Statement对象,数据库的操作对象
            ps = conn.prepareStatement(sql);
            //预处理加载 ? 问号
            ps.setInt(1, 5);
            //执行sql
            int i = ps.executeUpdate();
            if (i>0){
                System.out.println("删除成功...");
            }else {
                System.out.println("删除失败...");
            }

        } catch (Exception e) {
            System.out.println("异常信息:"+e);
        }finally {
            try {
                //关流
                ps.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e);
            }
        }

    }
}

修改案例

package com.yy.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author 杨 
 * @CreateDate 2023-04-12 11:20
 **/
public class UpdateTest {
    static String url="jdbc:mysql://localhost:3306/db01?serverTimezone=UTC";
    static String user="root";
    static String pwd="123sa";
    static Connection conn= null;
    static PreparedStatement ps= null;
    public static void main(String[] args) {
        String sql="update user set u_name=?,u_address=? where u_id=?";
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection(url, user, pwd);
            //预加载Statement对象,数据库的操作对象
            ps = conn.prepareStatement(sql);
            //预处理加载 ? 问号
            ps.setString(1, "杨111");
            ps.setString(2, "店上村");
            ps.setInt(3, 4);
            //执行sql
            int i = ps.executeUpdate();
            if (i>0){
                System.out.println("修改成功...");
            }else {
                System.out.println("修改失败...");
            }

        } catch (Exception e) {
            System.out.println("异常信息:"+e);
        }finally {
            try {
                //关流
                ps.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e);
            }
        }

    }
}

查询案例

package com.yy.test;
import java.sql.*;

/**
 * @Author 杨 
 * @CreateDate 2023-04-12 11:20
 **/
public class SelectTest {
    static String url="jdbc:mysql://localhost:3306/db01?serverTimezone=UTC";
    static String user="root";
    static String pwd="123sa";
    static Connection conn= null;
    static PreparedStatement ps= null;
    static ResultSet rs= null;
    public static void main(String[] args) {
        String sql="select * from user";
        try {
            //加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection(url, user, pwd);
            //预加载Statement对象,数据库的操作对象
            ps = conn.prepareStatement(sql);
            //执行sql,返回结果集
            rs = ps.executeQuery();
            while (rs.next()){
                int id = rs.getInt("u_id");
                String name = rs.getString("u_name");
                String pwd = rs.getString("u_pwd");
                int age = rs.getInt("u_age");
                String sex = rs.getString("u_sex");
                String address = rs.getString("u_address");

                System.out.println("编号:"+id+"\t"+
                        "姓名:"+name+"\t"+
                        "密码:"+pwd+"\t"+
                        "年龄:"+age+"\t"+
                        "性别:"+sex+"\t"+
                        "地址:"+address);
            }

        } catch (Exception e) {
            System.out.println("异常信息:"+e);
        }finally {
            try {
                //关流
                rs.close();
                ps.close();
                conn.close();
            } catch (SQLException e) {
                System.out.println(e);
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值