JDBC_1原理及增刪改查

JBDC


数据的持久化:把数据保存到磁盘上。
JDBC是java访问数据库的基石,JDO,Hibernate,Mybatis等都是基于JDBC
JDBC是一个独立于特定数据库的管理系统,通用的SQL数据库存取和操作的公共接口
在这里插入图片描述
在这里插入图片描述
配置文件:jdbc.properties

user=root
password=abc123
url=jdbc:mysql://localhost:3306/test
driverClass=com.mysql.jdbc.Driver

获取Connection

public static void main(String[] args) throws Exception {
        //方式一
        Driver driver = new com.mysql.jdbc.Driver();
        String url = "jdbc:mysql://localhost1:3306/test";
        Properties info = new Properties();
        info.setProperty("user","root");
        info.setProperty("password","123456");
        Connection connect = driver.connect(url, info);


        //方式二:反射实现获取驱动对象 不用出现第三方的类
        Class clazz = Class.forName("com.mysql.jdbc.Driver");
        //权限要够
        Driver driver1 = (Driver) clazz.newInstance();


        //方式三:使用DriverManager替换Driver
        //获取驱动实现类对象
        Class clazz1 = Class.forName("com.mysql.jdbc.Driver");
        Driver driver2 = (Driver) clazz1.newInstance();
        String url1 = "jdbc:mysql://localhost1:3306/test";
        String user1 = "root";
        String password1 = "123456";
        //注册驱动
        DriverManager.registerDriver(driver2);
        //获取连接
        Connection connection = DriverManager.getConnection(url, user1, password1);
        System.out.println(connection);


        //方式4:优化方式四
        Class.forName("com.mysql.jdbc.Driver");
        //注册驱动
        DriverManager.registerDriver(driver2);
        //获取连接
        Connection connection1 = DriverManager.getConnection(url, user1, password1);
        System.out.println(connection1);

        //方式5:读取配置文件
        InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url2 = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");
        Class.forName(driverClass);
        Connection connection2 = DriverManager.getConnection(url, user, password);

    }

操作和访问数据库

在这里插入图片描述
PrepareStatement相比Statement能够解决SQL注入,拼串,能操作Blob数据,实现更高效的批量操作,

添加

public static void main(String[] args) throws Exception{
        InputStream resourceAsStream = ConnectionTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driverClass = properties.getProperty("driverClass");
        Class.forName(driverClass);
        Connection connection = DriverManager.getConnection(url, user, password);
        
        //添加数据
        //预编译sql语句,PreparedStatement实例
        String sql = "insert into customers(name,email,birth) values(?,?,?)";
        java.sql.PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //填充占位符
        preparedStatement.setString(1,"哪吒");
        preparedStatement.setString(2,"nezha@gmail.com");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse("1000-01-01");
        preparedStatement.setDate(3, (java.sql.Date) new Date(date.getTime()));
        //执行sql
        preparedStatement.execute();
        preparedStatement.close();
        connection.close();
    }

修改

        //预编译sql语句,PreparedStatement实例
        String sql = "update customers set name = ? where id = ?";
        java.sql.PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //填充占位符
        preparedStatement.setString(1,"莫扎特");
        preparedStatement.setObject(2,28);
        //执行sql
        preparedStatement.execute();
        preparedStatement.close();
        connection.close();

查找

//预编译sql语句,PreparedStatement实例
        String sql = "select id,name,email,birth from customers where id = ?";
        java.sql.PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //填充占位符

        //执行sql
        ResultSet resultSet = preparedStatement.executeQuery();
        //获取结果集的元数据:ResultSetMetaData
        ResultSetMetaData rsmd = preparedStatement.getMetaData();
        int columnCount = rsmd.getColumnCount();
        while (resultSet.next()){
            Customer cust = new Customer();
            for(int i = 0;i < columnCount;i ++){
                Object object = resultSet.getObject(i + 1);

                //获取每个列的列明
                String columnName = rsmd.getColumnName(i + 1);

                Field declaredField = Customer.class.getDeclaredField(columnName);
                declaredField.setAccessible(true);
                declaredField.set(cust,object);
            }
        }

        preparedStatement.close();
        connection.close();
        resultSet.close();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值