jdbc技术实现插入数据,更新和删除操作,查询操作

1、插入数据

package com.csdn.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//1.向数据库插入数据
/**
 * Sun公司制定标准(规范)    -- 只设计接口
 * 各大数据库厂商实现这些标准   -- 根据接口写实现类
 */
public class InsertData {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2.通过驱动管理器获取连接对象
        //Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/fruitdb", "root", "123456");
        Connection conn = DriverManager.getConnection("jdbc:mysql:///fruitdb", "root", "123456");

        //System.out.println(conn == null ? "连接失败!" : "连接成功!");
        //3.编写SQL语句
        String sql = "insert into t_fruit values(null,?,?,?,?)"; //?表示占位符

        //4.创建预处理命令对象
        PreparedStatement psmt = conn.prepareStatement(sql);

        //5.填充参数
        psmt.setString(1, "西瓜");
        psmt.setInt(2, 9);
        psmt.setInt(3, 200);
        psmt.setString(4, "西瓜很好吃!");

        //6.执行更新,返回影响行数
        int count = psmt.executeUpdate();
        System.out.println(count);//1
    }
}

2、修改数据

package com.csdn.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//2.演示修改操作
public class ModifyData {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        //1.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //2.通过驱动管理器获取连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql:///fruitdb", "root", "123456");

//        System.out.println(conn == null ? "连接失败" : "连接成功");

        //3.编写SQL语句
        String sql = "update t_fruit set price=?,fcount=? where fid=?";

        //创建预处理命令对象
        PreparedStatement psmt = conn.prepareStatement(sql);

        psmt.setInt(1, 8);
        psmt.setInt(2, 150);
        psmt.setInt(3,1);

        int count = psmt.executeUpdate();//增删改都称为执行更新
        System.out.println(count > 0 ? "更新成功!" : "更新失败!");

        psmt.close();
        conn.close();
    }
}

3、删除数据

package com.csdn.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//3.演示删除操作
public class DeleteData {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");

        //通过驱动管理器获取连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql:///fruitdb", "root", "123456");

        //编写SQL语句
        String sql = "delete from t_fruit where fid=?";

        //创建预处理命令对象
        PreparedStatement psmt = conn.prepareStatement(sql);

        psmt.setInt(1,1);

        int count = psmt.executeUpdate();
        
        System.out.println(count > 0 ? "删除成功!" : "删除失败!");
    }
}

4、查询操作

package com.csdn.jdbc;
import java.sql.*;
//4.演示查询所有数据
public class SelectData {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //通过驱动管理器获取连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql:///fruitdb", "root", "123456");
        //编写SQL语句
        String sql = "select * from t_fruit";
        //创建预处理命令对象
        PreparedStatement psmt = conn.prepareStatement(sql);

        //执行查询
        ResultSet rs = psmt.executeQuery();
        while (rs.next()) {
            Integer fid = rs.getInt(1);
            String fname = rs.getString(2);
            Integer price = rs.getInt(3);
            Integer fcount = rs.getInt("fcount");
            String remark = rs.getString("remark");
            System.out.println(fid + "_" + fname + "_" + price + "_" + fcount + "_" + remark);
        }
        //是否资源
        rs.close();
        psmt.close();
        conn.close();
    }
}

5、查询特定的数据

package com.csdn.jdbc;
import java.sql.*;
//5.演示查询特定名称的库存记录
public class SelectSpecifyData {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //通过驱动管理器获取连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql:///fruitdb", "root", "123456");
        //编写SQL语句
        String sql = "select * from t_fruit where fname like ?";
        //创建预处理命令对象
        PreparedStatement psmt = conn.prepareStatement(sql);
        psmt.setString(1,"西瓜");

        //执行查询
        ResultSet rs = psmt.executeQuery();
        if (rs.next()) {
            Integer fid = rs.getInt(1);
            String fname = rs.getString(2);
            Integer price = rs.getInt(3);
            Integer fcount = rs.getInt("fcount");
            String remark = rs.getString("remark");
            System.out.println(fid + "_" + fname + "_" + price + "_" + fcount + "_" + remark);
        }
        //是否资源
        rs.close();
        psmt.close();
        conn.close();
    }
}

你可能感兴趣的:(#,JDBC,1024程序员节,java,反射,jdbc)