原生JDBC数据库连接

mysql数据库连接介绍

mysql数据库是用于管理数据的,后台是用户根据业务做逻辑处理的,前台显示数据并进行交互。

mysql数据库特点

1.MySQL性能卓越、服务稳定,很少出现异常宕机。

2.MySQL开放源代码且无版权制约,自主性及使用成本低。

3.MySQL历史悠久,用户使用活跃,遇到问题可以寻求帮助。

4.MySQL体积小,安装方便,易于维护。

5.MySQL口碑效应好,是的企业无需考虑就用之,LAMP、LNMP 流行架构。

6.MySQL支持多种操作系统,提供多种API接口,支持多种开发语言,特别是PHP。

关键词

DriverManager(管理JDBC驱动)
Statement(接口,用于SQL的编译及执行)
PreparedStatement(子接口,预编译)
CallableStatement(子接口,执行存储过程)
ResultSet(结果集)

jdbc连接步骤

1、关联数据库驱动包(不同的数据库使用的驱动包是不一样的)也是jar包。
在这里插入图片描述
2、注册驱动(通过 反射进行驱动的注册)。
3、连接数据库获取数据库连接对象(登录—选择数据库的过程)。
4、执行SQL语句。
5、处理结果集。
6、关闭数据库连接对象。

代码:

package com.li.entry;

import java.sql.*;

/**
 * 本类是jdbc测试类
 * 目的:测试对数据库的连接和操作
 * 异常处理:全部的异常都是抛出去,不做处理
 * */
public class JDBCApplyTest {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        //插入员工操作
        insertMethod();
        //删除员工操作
        //delete();
        //修改员工操作
        //update();
        //查询员工方法
        //query();
    }

    /**
     * 插入数据库方法
     * */
    public static void insertMethod()  {
        Statement statement=null;
        Connection conn=null;
        try {
            //1、注册驱动
            Class.forName("com.mysql.jdbc.Driver");
            //2、创建数据库连接
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hongzehu","root","123456");
            //3、编译及执行SQL
            String sql="insert into t_emp values(11,'哈哈','男','123456789','中国','3','3500')";
            //创建执行状态对象
            statement=conn.createStatement();
            int result=statement.executeUpdate(sql);
            System.out.println(result);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //4、关闭数据库连接
            try {
                if(statement!=null){
                    statement.close();
                }
                if(conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                System.out.println("数据库关闭异常");
                e.printStackTrace();
            }
        }
    }

    /**
     * 删除操作
     * */
    public static void delete()  {
        //1、注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("注册驱动失败");
            e.printStackTrace();
        }
        //2、创建连接
        Connection connection= null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hongzehu","root","123456");
        } catch (SQLException e) {
            System.out.println("创建连接失败");
            e.printStackTrace();
        }
        //3、编译及执行SQL语句
        String sql="delete from t_emp where eno=11";
        Statement statement= null;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            System.out.println("编译及查询失败");
            e.printStackTrace();
        }
        //执行完毕会返回操作记录的条数
        int result= 0;
        try {
            result = statement.executeUpdate(sql);
        } catch (SQLException e) {
            System.out.println("返回结果失败");
            e.printStackTrace();
        }
        System.out.println(result);
        //4、关闭数据库连接
        try {
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭数据库失败");
            e.printStackTrace();
        }
    }

    /**
     * 修改操作
     * */
    public static void update()  {
        //1、注册驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("注册驱动出现问题");
            e.printStackTrace();
        }
        //2、创建连接
        Connection connection= null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hongzehu","root","123456");
        } catch (SQLException e) {
            System.out.println("创建连接失败");
            e.printStackTrace();
        }
        //3、编译执行SQL语句
        Statement statement= null;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            System.out.println("编译失败失败");
            e.printStackTrace();
        }
        String sql="update t_emp set sex='女' where eno=10";
        int result= 0;
        try {
            result = statement.executeUpdate(sql);
        } catch (SQLException e) {
            System.out.println("执行sql语句失败");
            e.printStackTrace();
        }
        System.out.println(result);
        //4、关闭数据库连接
        try {
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 数据库查询操作
     * */
    public static void query() {
        //1、加载数据库驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("加载数据库驱动");
            e.printStackTrace();
        }
        //2、连接数据库
        Connection connection= null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/hongzehu","root","123456");
        } catch (SQLException e) {
            System.out.println("连接数据库");
            e.printStackTrace();
        }
        //3、编译以及执行SQL语句
        String result="select * from t_emp";
        Statement statement= null;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            System.out.println("编译失败");
            e.printStackTrace();
        }
        ResultSet resultSet= null;
        try {
            resultSet = statement.executeQuery(result);
        } catch (SQLException e) {
            System.out.println("返回结果失败");
            e.printStackTrace();
        }
        //显示查询结果,通过遍历
        try {
            while (resultSet.next()){
                int id=resultSet.getInt("eno");
                String name =resultSet.getString("ename");
                String sex=resultSet.getString("sex");
                String phone=resultSet.getString("phone");
                String address=resultSet.getString("deptno");
                String offer=resultSet.getString("offer");
                System.out.println(id+"|"+name+"|"+sex+"|"+phone+"|"+address+"|"+offer);
            }
        } catch (SQLException e) {
            System.out.println("遍历失败");
            e.printStackTrace();
        }
        try {
            //关闭数据库连接
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

数据库

在这里插入图片描述t_emp表
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值