JDBC基本实现原理

来源:互联网 发布:哪些网站可以注册域名 编辑:程序博客网 时间:2024/05/21 08:39

什么是JDBC?
JDBC是Java Data Base Connectivity的缩写,Java数据库连接技术的简称。
是一种用于执行SQL语句的JavaAPI,提供连接各种常用数据库的能力。
2.JDBC API:主要功能是与数据库建立连接、执行SQL 语句、处理结果。
提供者:Sun公司
内容:供程序员调用的接口与类,集成在java.sql和javax.sql包中,如:
DriverManager类:依据数据库的不同,管理JDBC驱动
Connection接口:负责连接数据库并担任传送数据的任务
Statement接口:由 Connection 产生、负责执行SQL语句
ResultSet接口:负责保存Statement执行后所产生的查询结果
3.DriverManager:
提供者:Sun公司
作用:管理各种不同的JDBC驱动
3.JDBC 驱动
提供者:数据库厂商
作用:负责连接各种不同的数据库
4.JDBC连接数据库的步骤:
1>加载驱动——-2>获取连接对象—–3>创建命令—–4>执行sql语句,并且返回结果集——5>处理结果集—–6>关闭连接
1.加载驱动
Class.forname(“com.mysql.jdbc.Driver”);
2.获取链接对象
DriverManager.getConnection(url,user,password);
3.创建命令对象
connection.createStatement();
4.执行命令返回结果
executeQuery(sql); 查询
executeUpdate(sql); 增删改
5.处理结果
while(resultSet.next()){
//类型和数据库中表字段的属性一样,并且后面表示字段名
String str = resultSet.getString(“str”);
}
6.关闭所有资源
关闭结果集———>关闭命令——>关闭连接对象

package com.itqf.demo;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Test1 {    public static void main(String[] args) {        Connection connection=null;         Statement statement=null;         ResultSet resultSet=null;        try {            // 1.加载驱动            Class.forName("com.mysql.jdbc.Driver");            //2.获取链接对象             connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "root");            // 3.创建编译命令对象             statement = connection.createStatement();             // 4.执行命令            resultSet = statement.executeQuery("select * from tb1");             // 5.处理结果集             while(resultSet.next()) {                 int id=resultSet.getInt("id");                 String email = resultSet.getString("email");                 String uname = resultSet.getString("uname");                 System.out.println("id:"+id+",email:"+email+",uname:"+uname);             }        } catch (Exception e) {            e.printStackTrace();        }finally {            // 关闭结果集            if(resultSet!=null) {                try {                    resultSet.close();                } catch (SQLException e) {        e.printStackTrace();                }            }            // 关闭命令            if(statement!=null) {                try {                    statement.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            // 关闭链接            if(connection!=null) {                try {                    connection.close();                } catch (SQLException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }            }        }    }}

JDBC应用
1.对宠物和主人信息进行管理
2.宠物和主人信息存储在MySQL数据库中
3.通过JDBC对宠物和主人进行增、删、改、查
这里写图片描述

第一步:创建狗和主人的表
第二步:新建一个工程,创建一个狗的实体类,主人的实体类。
第三步:在Dao层写宠物和主人的增删改查的方法。

package com.qf.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import com.qf.bean.Dog;public class DogDao {    //插入的方法    public int insert(Dog dog) {        Connection connection = null;        Statement statement = null;        try {            //1.            Class.forName("com.mysql.jdbc.Driver");            //2.            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/pet","root","root");            //3.            statement = connection.createStatement();            //4.            int result = statement.executeUpdate("insert into dog(name,health,love,kind) values('"+dog.getName()+",'"+dog.getHealth()+",'"+dog.getLove()+",'"+dog.getKind()+"''')");            return result;        } catch (Exception e) {            e.printStackTrace();        }finally {            if(statement!=null) {                try {                    statement.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if(connection!=null) {                try {                    connection.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }        return 0;    }    //删除的方法    public int delete(int id) {        Connection connection = null;        Statement statement = null;        try {            Class.forName("com.mysql.jdbc.Driver");            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/pet", "root", "root");            statement = connection.createStatement();            int result = statement.executeUpdate("delete from dog where id="+id);            return result;        }catch (Exception e) {            e.printStackTrace();        } finally {            if(statement!=null) {                try {                    statement.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if(connection!=null) {                try {                    connection.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }        return 0;    }    public int update(Dog dog) {        Connection connection = null;        Statement statement = null;        try {            // 加载驱动            Class.forName("com.mysql.jdbc.Driver");            // 获取链接对象            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/pet", "root", "root");            // 创建命令对象            statement = connection.createStatement();            // 执行命令,返回结果            int result = statement.executeUpdate("update dog set name='"+dog.getName()+"',health="+dog.getHealth()+",love="+dog.getLove()+",kind='"+dog.getKind()+"' where id="+dog.getId()+"");            return result;        } catch (Exception e) {            // TODO: handle exception            e.printStackTrace();        }finally {            if(statement!=null) {                try {                    statement.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if(connection!=null) {                try {                    connection.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }        return 0;    }    //查询    public List<Dog> find(){        Connection connection=null;        Statement statement = null;        ResultSet resultSet = null;        List<Dog> list = new ArrayList<Dog>();        try {            Class.forName("com.mysql.jdbc.Driver");            connection  = DriverManager.getConnection("jdbc:mysql://localhost:3306/pet", "root", "root");            statement = connection.createStatement();            resultSet = statement.executeQuery("select * from dog");            //处理结果集            while(resultSet.next()) {                int id = resultSet.getInt("id");                String name = resultSet.getString("name");                int health = resultSet.getInt("health");                int love = resultSet.getInt("love");                String kind = resultSet.getString("kind");                list.add(new Dog(id, name, health, love, kind));            }            return list;        } catch (Exception e) {            e.printStackTrace();        }finally {            if(resultSet!=null) {                try {                    resultSet.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if(statement!=null) {                try {                    statement.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }            if(connection!=null) {                try {                    connection.close();                } catch (SQLException e) {                    e.printStackTrace();                }            }        }        return null;    }}

这就是JDBC最原始的代码,这里有很多的重复代码,性能也很差,消耗资源也很大,所以我们需要JDBC连接池。

原创粉丝点击