JDBC案例

文章目录

        • 案例1 :修改数据库中的数据
        • 案例2:查询数据库中的数据
        • 案例3:查询数据库中账户表数据,并将其封装成Account对象,,存储到ArrayList集合当中

案例1 :修改数据库中的数据

JDBC案例_第1张图片
JDBC案例_第2张图片

package src.com.itheima.jdbc;


import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

//jdbc快速入门
public class JDBCDemo {
    public static void main(String[] args) throws Exception {

        //1. 注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/db1";
        String usename = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, usename, password);

        //3.定义sql
        String sql = "UPDATE account SET money=1500 WHERE id =2;";

        // 4.获取执行sql的对象statement
        Statement stmt = conn.createStatement();

        //5.执行sql
        int count = stmt.executeUpdate(sql);  //受影响的行

        //6.处理结果
        System.out.println(count);

        //7.释放资源
        stmt.close();
        conn.close();


    }
}

案例2:查询数据库中的数据

JDBC案例_第3张图片

package src.com.itheima.jdbc;


import com.mysql.jdbc.Driver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

//jdbc快速入门
public class JDBCDemo {
    public static void main(String[] args) throws Exception {

        //1. 注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/db1";
        String usename = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, usename, password);

        //3.定义sql
        String sql = "select * from account;";

        // 4.获取执行sql的对象statement
        Statement stmt = conn.createStatement();

        //5.执行sql
        //int count = stmt.executeUpdate(sql);  //修改
        ResultSet resultSet = stmt.executeQuery(sql);  //查询

        //6.处理结果
        while (resultSet.next()){
            //获取数据
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            double money = resultSet.getDouble(3);

            System.out.println(id);
            System.out.println(name);
            System.out.println(money);

            System.out.println("------------------------");
        }

        //7.释放资源
        resultSet.close();
        stmt.close();
        conn.close();


    }
}

JDBC案例_第4张图片

案例3:查询数据库中账户表数据,并将其封装成Account对象,,存储到ArrayList集合当中

JDBC案例_第5张图片
2.在java中创建account对象

public class Account {
//这里采用private修饰实现封装,隐藏内部细节防止外部直接访问修改类的私有变量,提高代码的安全型和可维护性;
//如果外部类想访问私有变量,要使用get和set、方法
    private int id;
    private String name;
    private double money;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

3. 从mysql中读取数据给对象存入ArrayList中并查看

package src.com.itheima.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class SqlAccount {
    public static void main(String[] args) throws Exception {

        //1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        //2.获取连接
        Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db1", "root", "123456");

        //3.定义sql
        String sql = "SELECT * FROM account";

        //4.获取sql的执行对象
        Statement state = conn.createStatement();

        //5.执行sql
        ResultSet resultSet = state.executeQuery(sql);

        //6.处理结果
        System.out.println(resultSet);   //com.mysql.jdbc.JDBC42ResultSet@6093dd95
        System.out.println(resultSet.getClass().getName());  //查看结果类型 :com.mysql.jdbc.JDBC42ResultSet


        ArrayList<Account> accounts = new ArrayList<>();

        while (resultSet.next()){
           //对象创建
            Account account = new Account();

            //获取数据
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            double money = resultSet.getDouble("money");

            //给对象赋值
            account.setId(id);
            account.setName(name);
            account.setMoney(money);

            //给对象存入集合
            accounts.add(account);

        }

        System.out.println(accounts);

        //7.释放资源
        resultSet.close();
        state.close();
        conn.close();
    }
}

JDBC案例_第6张图片

你可能感兴趣的:(oracle,数据库)