JDBC

package study.jdbc;

import com.mysql.jdbc.Driver;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 1、导入驱动jar包
 * 复制相应的jar包到项目中
 * 将jar所在的目录添加为库(Iltellij IDE 选择jar所在目录右键添加为库)
 *
 * 2、注册驱动
 *  DriverManager:
 *      驱动管理对象
 *      功能:
 *          1、注册驱动
 *          2、获取数据库连接
 *  Connection:
 *      数据库连接对象
 *      功能:
 *          1、获取执行sql的对象
 *              * Statement ceateStatement()
 *              * PreparedStatement prepareStatement(string sql)
 *          2、管理事务
 *              * 开启事务:setAutoCommit(boolean autoCommit) 设置 false 开启事务
 *              * 提交事务:commit()
 *              * 回滚事务:rollback()
 *  Statement:
 *      用于执行静态SQL语句并返回其生成的结果的对象
 *      功能:
 *          1、执行sql语句
 *              * execute(string sql) 执行任意的SQL语句
 *              * int executeUpdate(string sql) 执行 DML(insert、update、delete) 语句、DDL(create、alter、drop)语句 ,该方法返值是影响行数
 *              * ResultSet executeQuery(string sql) 执行 DQL(select)语句
 *  ResultSet:
 *      结果集对象 表示数据库结果集的数据表,通常通过执行查询数据库的语句生成
 *
 */
public class Jdbc {
    public static void main(String[] args)  {
        /*
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取数据库连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
        //定义sql语句
        String sql = "select * from emp";
        //获取SQL对象 Statement
        Statement stmt = conn.createStatement();
        //执行语句
        ResultSet result = stmt.executeQuery(sql);
        System.out.println(result);
        //释放资源
        stmt.close();*/

        Connection conn = null;
        Statement stmt = null;
        ResultSet res = null;
        //注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
            //获取Connection连接对象
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", "root");
            //获取执行SQL对象
            stmt = conn.createStatement();

            //定义SQL
            //增
            String insert = "insert into emp(id,ename,job_id,mgr,joindate,salary,bonus,dept_id) values(1,'乔峰',2,1010,'2001-09-28',29750,1000,20)";
            //删
            String delete = "delete from emp where id = 1";
            //改
            String update = "update emp set id = 2 where id = 1001";
            //查
            String select = "select * from emp";

            //执行语句
            /*int i1 = stmt.executeUpdate(insert);//影响行数
            int i2 = stmt.executeUpdate(delete);//影响行数
            int i3 = stmt.executeUpdate(update);//影响行数
            System.out.println(i1);
            System.out.println(i2);
            System.out.println(i3);*/

            //执行查询语句
            //获取单个数据
            res = stmt.executeQuery(select);
            /*res.next();//指针向下移动一行 返回false表示已经到最后
            System.out.println(res.getInt(1));
            System.out.println(res.getString("ename"));
            System.out.println(res.getInt("job_id"));*/


            //遍历 将查询结果集封装为对象
            Emp emp = null;
            List<Emp> list = new ArrayList<>();
            while (res.next()){
                emp = new Emp();
                emp.setId(res.getInt("id"));
                emp.setEname(res.getString("ename"));
                emp.setId(res.getInt("job_id"));
                emp.setMgr(res.getInt("mgr"));
                emp.setJoindate(res.getDate("joindate"));
                emp.setSalary(res.getDouble("salary"));
                emp.setBonus(res.getDouble("bonus"));
                emp.setDept_id(res.getInt("dept_id"));

                list.add(emp);

            }

            Iterator<Emp> it = list.iterator();
            while (it.hasNext()) {
                System.out.println(it.next());
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

            if (res != null) {
                try {
                    res.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }

    }

}

jdbc0.png

仅有一条评论

添加新评论